-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
52 lines (51 loc) · 1.71 KB
/
Copy pathsolution.java
File metadata and controls
52 lines (51 loc) · 1.71 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
52
// 1. Two Sum
// https://leetcode.com/problems/two-sum/
// Easy | Java | Accepted 2026-08-12
// Runtime 2 ms | Memory 47.3 MB
//3Sum Intuition
/*
class Solution {
public int[] twoSum(int[] nums, int target) {
int[][] pairs = new int[nums.length][2];
for(int i = 0; i<nums.length; i++)
{
pairs[i] = new int[]{nums[i], i};
}
Arrays.sort(pairs, (a, b) -> Integer.compare(a[0], b[0]));
int i = 0;
int j = nums.length-1;
while(i<j)
{
int add = pairs[i][0] + pairs[j][0];
if(add==target)
{
return new int[]{pairs[i][1], pairs[j][1]};
}
if(add>target)
{
j--;
}
if(add<target)
{
i++;
}
}
return new int[0];
}
}
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>(); //Make a map that stores the complements of each num in nums
for(int i = 0; i<nums.length; i++) //Go through nums
{
int complement = target - nums[i]; //Find the complement or other number that adds up to target
if(map.containsKey(complement)) //If this number is in the map, then these are the 2 numbers that add up to target
{
return new int[]{map.get(complement), i}; //Return the array with the complements index and the current index
}
map.put(nums[i], i); //Add the current num in with its matching index, don't put the complement because this num could be the complement of another number down the road
}
return new int[0];
}
}