-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.两数之和.cs
50 lines (45 loc) · 1.47 KB
/
1.两数之和.cs
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
/*
* @lc app=leetcode.cn id=1 lang=csharp
*
* [1] 两数之和
*/
// @lc code=start
// 暴力法,效率最低
// public class Solution {
// public int[] TwoSum(int[] nums, int target) {
// for (int i = 0; i < nums.Length; i++)
// {
// for (int j = i+1; j < nums.Length; j++)
// {
// if(nums[i]+nums[j] == target) return new int[]{ i , j };
// }
// }
// return new int[] { 0, 0 };
// }
// }
// 使用哈希表,原理就是查找target-num[i]是不是在哈希表里,如果在就直接return这一组,如果不在就把num[i]写入到哈希表,下一次遇到的时候就可以找到了
//57/57 cases passed (136 ms)
//Your runtime beats 95.92 % of csharp submissions
//Your memory usage beats 31.54 % of csharp submissions (43.6 MB)
public class Solution {
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> s = new Dictionary<int, int>();
for(int i = 0;i < nums.Length;i++)
{
int rest = target - nums[i];
if(s.ContainsKey(rest)) // 如果表里有另外一个int的话
{
return new int[] {s[rest], i};
}
else
{
if(!s.ContainsKey(rest))
{
s[nums[i]] = i;
}
}
}
return new int[] { 0 , 0 };
}
}
// @lc code=end