mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
修正0001.两数之和dart
- 儘量給定類型 - 使用HashMap取代不適合的List
This commit is contained in:
@ -402,16 +402,18 @@ public class Solution {
|
|||||||
### Dart:
|
### Dart:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
List<int> twoSum(List<int> nums, int target) {
|
List<int> twoSum(List<int> nums, int target) {
|
||||||
var tmp = [];
|
HashMap<int, int> hashMap = HashMap();
|
||||||
for (var i = 0; i < nums.length; i++) {
|
for (int i = 0; i < nums.length; i++) {
|
||||||
var rest = target - nums[i];
|
int rest = target - nums[i];
|
||||||
if(tmp.contains(rest)){
|
if (hashMap.containsKey(rest)) {
|
||||||
return [tmp.indexOf(rest), i];
|
return [hashMap[rest]!, i];
|
||||||
}
|
}
|
||||||
tmp.add(nums[i]);
|
hashMap.addEntries({nums[i]: i}.entries);
|
||||||
}
|
}
|
||||||
return [0 , 0];
|
return [];
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user