-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
51 lines (44 loc) · 1.13 KB
/
TwoSum.java
File metadata and controls
51 lines (44 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
*
*/
package easy;
/**
* LeetCode Example
* Given an array of integers, return indices of the two numbers such that they add up
* to a specific target.
* You may assume that each input would have exactly one solution,
* and you may not use the same element twice.
*
* @author Rozita Teymourzadeh
*
*/
public class TwoSum {
public int[] twoSum(int[] nums, int target) {
int firstInd = 0;
int temp;
int[] result = new int[2];
for (int i= 0; i < nums.length ; i++){
temp = nums[i] + nums[firstInd];
if ((temp == target) && (i != firstInd)){
result [0]= firstInd;
result [1]= i;
break;
}else if (i== nums.length-1){
firstInd++;
i = 0;
}
}
return result;
}
/**
* @param args
*/
public static void main(String[] args) {
int [] nums = {3, 2, 4};
int [] results;
int target = 6;
TwoSum twoSum = new TwoSum();
results = twoSum.twoSum(nums, target);
System.out.println("result is: " + results[0] + " and " + results[1]);
}
}