Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions dart/two_sum.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.
*/

class Solution {
List<int> twoSum(List<int> nums, int target) {
final Map<int, int> seen = {};

for (int i = 0; i < nums.length; i++) {
final int complement = target - nums[i];
if (seen.containsKey(complement)) {
return [seen[complement]!, i];
}

seen[nums[i]] = i;
}

return [];
}
}