File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 8
8
| :---| :---| :---|
9
9
1|[ Two Sum] ( ./0001-two-sum.js ) |Easy|
10
10
4|[ Median of Two Sorted Arrays] ( ./0004-median-of-two-sorted-arrays.js ) |Hard|
11
+ 27|[ Remove Element] ( ./0027-remove-element.js ) |Easy|
11
12
31|[ Next Permutation] ( ./0031-next-permutation.js ) |Medium|
12
13
36|[ Valid Sudoku] ( ./0036-valid-sudoku.js ) |Medium|
13
14
43|[ Multiply Strings] ( ./0043-multiply-strings.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 27. Remove Element
3
+ * https://leetcode.com/problems/remove-element/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given an integer array nums and an integer val, remove all occurrences of val in nums in-place.
7
+ * The relative order of the elements may be changed.
8
+ *
9
+ * Since it is impossible to change the length of the array in some languages, you must instead
10
+ * have the result be placed in the first part of the array nums. More formally, if there are
11
+ * k elements after removing the duplicates, then the first k elements of nums should hold the
12
+ * final result. It does not matter what you leave beyond the first k elements.
13
+ *
14
+ * Return k after placing the final result in the first k slots of nums.
15
+ *
16
+ * Do not allocate extra space for another array. You must do this by modifying the input
17
+ * array in-place with O(1) extra memory.
18
+ */
19
+
20
+ /**
21
+ * @param {number[] } nums
22
+ * @param {number } val
23
+ * @return {number }
24
+ */
25
+ var removeElement = function ( nums , val ) {
26
+ for ( let i = nums . length - 1 ; i > - 1 ; i -- ) {
27
+ if ( nums [ i ] === val ) nums . splice ( i , 1 ) ;
28
+ }
29
+ } ;
You can’t perform that action at this time.
0 commit comments