commit bbb1b06f9a1f3cd45aac5f615ae19a78c1148366
parent 8f78322147243074914168617fe2a440044df57f
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date: Mon, 17 Apr 2023 22:46:25 -0500
Two sum with dart V2
Diffstat:
1 file changed, 32 insertions(+), 0 deletions(-)
diff --git a/two-sums/two-sumV2.dart b/two-sums/two-sumV2.dart
@@ -0,0 +1,32 @@
+//Doing this again to get more comfortable with maps.
+//Also, using ?? -1 means that if the first val is null then return the value
+//of -1
+
+//Runtime: 320ms Beats: 72.49%
+//Memory: 143.7MB Beats 36.55%
+
+void main(){
+ Solution sol = new Solution();
+ List<int> l1 = [0,2,2,3,4,5,6,7,8,65,3,2,3,4,5,3,2,5,78,7,4,3,2,4,6,7,8,8,8,989,9,9,6,642,6,456,456,45,6,3456,45,6,3465];
+ int target = 10;
+ print(sol.twoSum(l1,target));
+}
+
+class Solution {
+ List<int> twoSum(List<int> nums, int target) {
+
+ Map<int, int> vals = {};
+ for(int i = 0 ; i < nums.length ; ++i){
+ int diff = target - nums[i];
+ if(vals.containsKey(diff)){
+ return [vals[diff]?? -1, i];
+ }
+ else{
+ vals[nums[i]] = i;
+ }
+ }
+ return [];
+ }
+
+
+}