Skip to content

Commit 2f45821

Browse files
committed
🎉 #1. Two Sum
1 parent db29448 commit 2f45821

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

Diff for: #1. Two Sum/Solution.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: Goog Tech
3+
* @Date: 2020-07-13 21:36:25
4+
* @Description: #1. Two Sum
5+
* @FilePath: \leetcode-googtech\#1. Two Sum\Solution.java
6+
*/
7+
import java.util.Arrays;
8+
9+
class Solution {
10+
// 暴力法
11+
public int[] twoSum(final int[] nums, final int target) {
12+
for (int i = 0; i < nums.length; i++) {
13+
for (int j = i + 1; j < nums.length; j++) {
14+
if (nums[i] + nums[j] == target) {
15+
return new int[] { i, j };
16+
}
17+
}
18+
}
19+
throw new IllegalArgumentException("no solution");
20+
}
21+
22+
// 测试
23+
public static void main(final String[] args) {
24+
System.out.println(Arrays.toString(new Solution().twoSum(new int[] { 2, 7, 11, 15 }, 9))); // [0, 1]
25+
}
26+
}

Diff for: #1. Two Sum/Solution.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
@Author: Goog Tech
3+
@Date: 2020-07-13 21:49:08
4+
@Description: #1. Two Sum
5+
@FilePath: \leetcode-googtech\#1. Two Sum\Solution.py
6+
'''
7+
class Solution:
8+
''' 暴力法 '''
9+
def twoSum(self, nums, target):
10+
n = len(nums)
11+
for x in range(n):
12+
for y in range(x + 1, n):
13+
if (nums[x] + nums[y] == target):
14+
return x, y
15+
break
16+
else:
17+
continue
18+
19+
''' 测试 '''
20+
print(Solution().twoSum([2, 7, 11, 15], 9)) # (0, 1)

0 commit comments

Comments
 (0)