Skip to content

Commit bb0b4f5

Browse files
author
rchen102
committed
2019/10/15
1 parent 177ed7d commit bb0b4f5

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

总结/Array/Array.MD

Whitespace-only changes.

总结/Array/Leetcode 1.MD

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## 1 Two Sum
2+
3+
### Solution1: Brute force
4+
T: O(n^2) S: O(1)
5+
6+
```java
7+
class Solution {
8+
public int[] twoSum(int[] nums, int target) {
9+
int[] res = new int[2];
10+
for (int i = 0; i < nums.length; i++) {
11+
for (int j = i + 1; j < nums.length; j++) {
12+
if (nums[i] + nums[j] == target) {
13+
res[0] = i;
14+
res[1] = j;
15+
return res;
16+
}
17+
}
18+
}
19+
return res;
20+
}
21+
}
22+
```
23+
24+
### Solution2: HashMap
25+
T: O(n) S: O(n)
26+
27+
```java
28+
class Solution {
29+
public int[] twoSum(int[] nums, int target) {
30+
int[] res = new int[2];
31+
Map<Integer, Integer> map = new HashMap<>();
32+
for (int i = 0; i < nums.length; i++) {
33+
if (map.containsKey(target - nums[i])) {
34+
res[0] = map.get(target - nums[i]);
35+
res[1] = i;
36+
return res;
37+
}
38+
map.put(nums[i], i);
39+
}
40+
return res;
41+
}
42+
}
43+
```
44+
45+
```py
46+
class Solution:
47+
def twoSum(self, nums, target):
48+
dic = {}
49+
for i in range(len(nums)):
50+
if (target - nums[i]) in dic:
51+
return [dic[target - nums[i]], i]
52+
else:
53+
dic[nums[i]] = i
54+
```

总结/Array/Leetcode 15.MD

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 15 3Sum
2+
3+
### Solution1:

0 commit comments

Comments
 (0)