File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 6
6
7
7
| #| Title| Difficulty|
8
8
| :---| :---| :---|
9
+ 1|[ Two Sum] ( ./0001-two-sum.js ) |Easy|
9
10
4|[ Median of Two Sorted Arrays] ( ./0004-median-of-two-sorted-arrays.js ) |Hard|
10
11
31|[ Next Permutation] ( ./0031-next-permutation.js ) |Medium|
11
12
36|[ Valid Sudoku] ( ./0036-valid-sudoku.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1. Two Sum
3
+ * https://leetcode.com/problems/two-sum/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`.
7
+ *
8
+ * You may assume that each input would have exactly one solution, and you may not use the same element twice.
9
+ *
10
+ * You can return the answer in any order.
11
+ */
12
+
13
+ /**
14
+ * @param {number[] } nums
15
+ * @param {number } target
16
+ * @return {number[] }
17
+ */
18
+ var twoSum = function ( nums , target ) {
19
+ const map = new Map ( ) ;
20
+
21
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
22
+ const diff = target - nums [ i ] ;
23
+
24
+ if ( map . has ( diff ) ) {
25
+ return [ map . get ( diff ) , i ] ;
26
+ }
27
+
28
+ map . set ( nums [ i ] , i ) ;
29
+ }
30
+ } ;
You can’t perform that action at this time.
0 commit comments