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 39dd8e3 commit 3627b22Copy full SHA for 3627b22
two_sum/main.js
@@ -1,4 +1,13 @@
1
function twoSum(numbers, target) {
2
+ /// This for loop has a time complexity of O(n).
3
+ const seen = new Map();
4
+ for(let i = 0; i < numbers.length; i++) {
5
+ let value = numbers[i], sub = target - numbers[i];
6
+ if(seen.has(sub)) return [seen.get(sub), i];
7
+ seen.set(value, i);
8
+ }
9
+
10
+ /// This for loop has a time complexity of O(n^2).
11
for(let i = 0; i < numbers.length - 1; i++) {
12
let sub = target - numbers[i];
13
for(let j = i + 1; j < numbers.length; j++) {
0 commit comments