We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bb0db1a commit 3810e50Copy full SHA for 3810e50
two-sum/kimyoung.js
@@ -0,0 +1,14 @@
1
+var twoSum = function (nums, target) {
2
+ let map = new Map(); // create a map to store the number and index of each element
3
+ for(let i = 0; i < nums.length; i++) {
4
+ let diff = target - nums[i];
5
+ if(map.has(diff)) { // once the differnece is found, return both index
6
+ return [i, map.get(diff)];
7
+ } else { // otherwise add to map
8
+ map.set(nums[i], i)
9
+ }
10
11
+};
12
+
13
+// time - O(n) at worst, iterate through the entire nums array
14
+// space - O(n) at worst, map the entire nums array
0 commit comments