-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0001.Two_Sum.py
More file actions
129 lines (103 loc) ยท 3.43 KB
/
0001.Two_Sum.py
File metadata and controls
129 lines (103 loc) ยท 3.43 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
*/
//version 1: brute force
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n - 1):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return [-1, -1]
// version 2: two pointer, ๅฆๆ่ฆ่ฟๅindex๏ผๅฐฑๆ้ฎ้ข~
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
nums.sort()
i, j = 0, len(nums) - 1
while i < j:
sum = nums[i] + nums[j]
if sum == target:
return [i, j]
elif sum > target:
j -= 1
else:
i += 1
return [-1, -1]
//version 3 : hashmap
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_idx = defaultdict(int) #store [num, index]
for idx, num in enumerate(nums):
if target - num in num_idx:
return [num_idx[target - num], idx]
else:
num_idx[num] = idx
return [-1, -1]
/*****************************************Java version**************************************/
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap();
for(int i = 0; i < nums.length; i++){
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] {i, map.get(complement)};
}
map.put(nums[i], i);
}
return null;
}
}
//version 2 : two pointers
class Solution {
class Pair implements Comparable<Pair> {
int number, index;
public Pair(int number, int index) {
this.number = number;
this.index = index;
}
public int compareTo(Pair other) {
return number - other.number;
}
}
public int[] twoSum(int[] nums, int target) {
int[] result = {-1, -1};
if (nums == null || nums.length == 0) {
return null;
}
Pair[] pairs = getSortedPairs(nums);
int left = 0, right = nums.length - 1;
while (left < right) {
if (pairs[left].number + pairs[right].number == target) {
result[0] = Math.min(pairs[left].index, pairs[right].index);
result[1] = Math.max(pairs[left].index, pairs[right].index);
return result;
} else if (pairs[left].number + pairs[right].number > target) {
right--;
} else {
left++;
}
}
return null;
}
Pair[] getSortedPairs(int[] nums) {
Pair[] pairs = new Pair[nums.length];
for (int i = 0; i < nums.length; i++) {
pairs[i] = new Pair(nums[i], i);
}
Arrays.sort(pairs);
return pairs;
}
}