This commit is contained in:
krahets
2023-11-27 02:32:06 +08:00
parent 32d5bd97aa
commit a4a23e2488
31 changed files with 179 additions and 213 deletions

View File

@ -75,10 +75,10 @@ comments: true
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
return new int[] { i, j };
return [i, j];
}
}
return Array.Empty<int>();
return [];
}
```
@ -309,15 +309,15 @@ comments: true
int[] TwoSumHashTable(int[] nums, int target) {
int size = nums.Length;
// 辅助哈希表,空间复杂度 O(n)
Dictionary<int, int> dic = new();
Dictionary<int, int> dic = [];
// 单层循环,时间复杂度 O(n)
for (int i = 0; i < size; i++) {
if (dic.ContainsKey(target - nums[i])) {
return new int[] { dic[target - nums[i]], i };
return [dic[target - nums[i]], i];
}
dic.Add(nums[i], i);
}
return Array.Empty<int>();
return [];
}
```