-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.two-sum.java
More file actions
32 lines (25 loc) · 758 Bytes
/
1.two-sum.java
File metadata and controls
32 lines (25 loc) · 758 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
/*
* @lc app=leetcode id=1 lang=java
*
* [1] Two Sum
*/
// @lc code=start
class Solution {
public int[] twoSum(int[] nums, int target) {
int end = nums.length-1;
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target)return new int[] {i,j};
// System.gc();
else if ( nums[end-i]+nums[end-j] == target) return new int[] {end-i,end-j};
}
}
// System.gc();
return new int[] {};
}
}
// @lc code=end
// O(n²) Not Efficient
/* Hash Map Approach: Efficient Approach
Subtract the current element from the target and query the result in hash map.
*/