Skip to content

Commit ab5660f

Browse files
committedSep 18, 2021
Add solution #27
1 parent 2c47dc8 commit ab5660f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
|:---|:---|:---|
99
1|[Two Sum](./0001-two-sum.js)|Easy|
1010
4|[Median of Two Sorted Arrays](./0004-median-of-two-sorted-arrays.js)|Hard|
11+
27|[Remove Element](./0027-remove-element.js)|Easy|
1112
31|[Next Permutation](./0031-next-permutation.js)|Medium|
1213
36|[Valid Sudoku](./0036-valid-sudoku.js)|Medium|
1314
43|[Multiply Strings](./0043-multiply-strings.js)|Medium|

‎solutions/0027-remove-element.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.