-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSumII.java
60 lines (45 loc) · 1.1 KB
/
TwoSumII.java
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
52
53
54
55
56
57
58
59
60
package com.leetcode.algorithms.easy.twopointers;
import java.util.Arrays;
import java.util.Scanner;
import com.leetcode.util.InputUtil;
public class TwoSumII {
public static void main(String...strings) {
// Input
Scanner scanner = new Scanner(System.in);
String[] inputs = InputUtil.inputArr(scanner.next());
int[] numbers = InputUtil.integerArr(inputs);
int target = scanner.nextInt();
// Output[index1, index2], index1 < index2
int[] output = twoSum(numbers, target);
// Print output
System.out.println(Arrays.toString(output));
scanner.close();
}
private static int[] twoSum(int[] numbers, int target) {
int length = numbers.length;
int[] result = new int[2];
boolean isBreak = false;
int i = 0;
while(i < length) {
int index1 = numbers[i];
int j = 0;
while(j < length) {
if(j != i) {
int index2 = numbers[j];
if(index1 + index2 == target) {
result[0] = i + 1;
result[1] = j + 1;
isBreak = true;
break;
}
}
++j;
}
if(isBreak) {
break;
}
++i;
}
return result;
}
}