forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution1.java
44 lines (35 loc) · 1.25 KB
/
Solution1.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
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
/// Author : liuyubobobo
/// Time : 2017-11-13
// Brute Force
// Time Complexity: O(n^2)
// Space Complexity: O(1)
public class Solution1 {
public int[] twoSum(int[] numbers, int target) {
if(numbers.length < 2 /*|| !isSorted(numbers)*/)
throw new IllegalArgumentException("Illegal argument numbers");
for(int i = 0 ; i < numbers.length ; i ++)
for(int j = i+1 ; j < numbers.length ; j ++)
if(numbers[i] + numbers[j] == target){
int[] res = {i+1, j+1};
return res;
}
throw new IllegalStateException("The input has no solution");
}
private boolean isSorted(int[] numbers){
for(int i = 1 ; i < numbers.length ; i ++)
if(numbers[i] < numbers[i-1])
return false;
return true;
}
private static void printArr(int[] nums){
for(int num: nums)
System.out.print(num + " ");
System.out.println();
}
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
printArr((new Solution1()).twoSum(nums, target));
}
}