Skip to content

Commit 1028597

Browse files
committed
two sum challenge
1 parent edb0978 commit 1028597

4 files changed

Lines changed: 62 additions & 1 deletion

File tree

.idea/misc.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

two_sum/src/main.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
// test case1
5+
//Input: nums = [2,7,11,15], target = 9
6+
//Output: [0,1]
7+
//Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
8+
9+
// test case2
10+
//Input: nums = [3,2,4], target = 6
11+
//Output: [1,2]
12+
13+
// test case3
14+
//Input: nums = [3,3], target = 6
15+
//Output: [0,1]
16+
17+
public class main {
18+
public static int[] twoSum(int[] nums, int target) {
19+
int[] r = new int[2];
20+
for(int i=0; i<nums.length; i++){
21+
int j = i+1;
22+
while(j<nums.length && nums[i] + nums[j] != target) {
23+
j++;
24+
}
25+
if (j<nums.length && nums[i] + nums[j] == target) {
26+
r[0] = i;
27+
r[1] = j;
28+
break;
29+
}
30+
}
31+
return r;
32+
}
33+
34+
public static void main(String args[]){
35+
// int[] nums = {2,7,11,15};
36+
// int target = 9;
37+
int[] nums = {3,3};
38+
int target = 6;
39+
// Output: [0,1]
40+
int[] result = twoSum(nums, target);
41+
for(int i: result) {
42+
System.out.println(i);
43+
}
44+
}
45+
46+
}

two_sum/two_sum.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

0 commit comments

Comments
 (0)