forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1.java
More file actions
36 lines (28 loc) · 927 Bytes
/
Solution1.java
File metadata and controls
36 lines (28 loc) · 927 Bytes
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
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
import java.util.HashMap;
/// Brute Force
/// Time Complexity: O(n^2)
/// Space Complexity: O(1)
public class Solution1 {
public int[] twoSum(int[] nums, int target) {
for(int i = 0 ; i < nums.length; i ++)
for(int j = 0 ; j < nums.length ; j ++)
if(nums[i] + nums[j] == target){
int[] res = {i, j};
return res;
}
throw new IllegalStateException("the input has no solution");
}
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 = {0, 4, 3, 0};
int target = 0;
printArr((new Solution1()).twoSum(nums, target));
}
}