File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 17
17
27|[ Remove Element] ( ./0027-remove-element.js ) |Easy|
18
18
31|[ Next Permutation] ( ./0031-next-permutation.js ) |Medium|
19
19
36|[ Valid Sudoku] ( ./0036-valid-sudoku.js ) |Medium|
20
+ 41|[ First Missing Positive] ( ./0041-first-missing-positive.js ) |Hard|
20
21
43|[ Multiply Strings] ( ./0043-multiply-strings.js ) |Medium|
21
22
54|[ Spiral Matrix] ( ./0054-spiral-matrix.js ) |Medium|
22
23
58|[ Length of Last Word] ( ./0058-length-of-last-word.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 41. First Missing Positive
3
+ * https://leetcode.com/problems/first-missing-positive/
4
+ * Difficulty: Hard
5
+ *
6
+ * Given an unsorted integer array nums, return the smallest missing positive integer.
7
+ *
8
+ * You must implement an algorithm that runs in O(n) time and uses constant extra space.
9
+ */
10
+
11
+ /**
12
+ * @param {number[] } nums
13
+ * @return {number }
14
+ */
15
+ var firstMissingPositive = function ( nums ) {
16
+ let i = 0 ;
17
+
18
+ while ( i < nums . length ) {
19
+ if ( nums [ i ] > 0 && nums [ i ] <= nums . length && nums [ nums [ i ] - 1 ] !== nums [ i ] ) {
20
+ [ nums [ nums [ i ] - 1 ] , nums [ i ] ] = [ nums [ i ] , nums [ nums [ i ] - 1 ] ] ;
21
+ } else {
22
+ i ++ ;
23
+ }
24
+ }
25
+
26
+ for ( i = 0 ; i < nums . length ; i ++ ) {
27
+ if ( nums [ i ] !== i + 1 ) {
28
+ return i + 1 ;
29
+ }
30
+ }
31
+
32
+ return i + 1 ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments