Skip to content

Commit 9923096

Browse files
committed
two sum solution
1 parent f291ae8 commit 9923096

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

two-sum/john9803.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class john9803 {
2+
public int[] twoSum(int[] nums, int target) {
3+
int[] result = n2Solve(nums, target);
4+
return result;
5+
}
6+
7+
public int[] n2Solve(int[] nums, int target){
8+
// 일반적인 N^2 풀이
9+
int alpha_target = 0;
10+
int beta_target = 0;
11+
12+
int[] truth_arr = new int[2];
13+
14+
gnd: for(int i =0; i<nums.length; i++){
15+
alpha_target = nums[i];
16+
for( int j=i+1; j<nums.length; j++){
17+
beta_target = nums[j];
18+
if(alpha_target+beta_target==target){
19+
truth_arr[0] = i;
20+
truth_arr[1] = j;
21+
break gnd;
22+
}
23+
}
24+
}
25+
return truth_arr;
26+
}
27+
28+
// n2미만으로도 풀이해보기
29+
// public int[] 2nSolve(int[] nums, int target){
30+
// return [];
31+
// }
32+
}

0 commit comments

Comments
 (0)