Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ccdf554

Browse files
[新增] 新增题目 27. 移除元素
1 parent 3e6c875 commit ccdf554

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

‎src/27_removeElement.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
27. 移除元素
3+
难度:简单
4+
题目链接:https://leetcode.cn/problems/remove-element/
5+
6+
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
7+
8+
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
9+
10+
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
11+
12+
说明:
13+
为什么返回数值是整数,但输出的答案是数组呢?
14+
请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
15+
16+
你可以想象内部操作如下:
17+
18+
// nums 是以"引用"方式传递的。也就是说,不对实参作任何拷贝
19+
int len = removeElement(nums, val);
20+
21+
// 在函数里修改输入数组对于调用者是可见的。
22+
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
23+
for (int i = 0; i < len; i++) {
24+
print(nums[i]);
25+
}
26+
27+
示例 1:
28+
输入:nums = [3,2,2,3], val = 3
29+
输出:2, nums = [2,2]
30+
解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
31+
32+
示例 2:
33+
输入:nums = [0,1,2,2,3,0,4,2], val = 2
34+
输出:5, nums = [0,1,4,0,3]
35+
解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
36+
37+
提示:
38+
0 <= nums.length <= 100
39+
0 <= nums[i] <= 50
40+
0 <= val <= 100
41+
*/
42+
43+
import { fileURLToPath } from 'url'
44+
import testCases from '../tests/testCases/27_removeElement.js'
45+
46+
/**
47+
* 移除指定元素并返回移除元素后的数组长度
48+
* - LeetCode 入口
49+
* @param {number[]} nums - 原数组
50+
* @param {number} val - 指定移除的值
51+
* @returns {number} 移除元素后的数组长度
52+
*/
53+
export function removeElement (nums: number[], val: number): number {
54+
// 定义快慢指针,快指针每次遍历时都向后移一位
55+
let fastIndex = 0
56+
// 慢指针在快指针的值为要删除的值时,慢指针位置不变
57+
let slowIndex = 0
58+
59+
// 遍历数组,将要删除的值用它之后的元素覆盖
60+
while (fastIndex < nums.length) {
61+
// 每一次遍历时判断快指针所对应的值是否不为要删除的值
62+
if (nums[fastIndex] !== val) {
63+
// 将慢指针所在位置设置为快指针所对应的值,且慢指针向后移一位
64+
nums[slowIndex] = nums[fastIndex]
65+
slowIndex++
66+
}
67+
// 每次遍历将快指针向后移一位
68+
fastIndex++
69+
}
70+
71+
// 完成遍历移动数组元素后,慢指针就是删除元素后数组的长度
72+
return slowIndex
73+
}
74+
75+
// Debug
76+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
77+
const { input, expected } = testCases[0]
78+
const newLength = removeElement(input.nums, input.val)
79+
const output = input.nums.splice(0, newLength)
80+
console.log({ input, output, expected })
81+
}

‎tests/27_removeElement.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, test, expect } from '@jest/globals'
2+
import testCases from './testCases/27_removeElement.js'
3+
import { removeElement } from '../src/27_removeElement.js'
4+
5+
describe('27. Remove Element', () => {
6+
testCases.forEach(({ input, expected }, index) => {
7+
test(`Test case index: ${index}`, () => {
8+
const newLength = removeElement(input.nums, input.val)
9+
expect(input.nums.splice(0, newLength)).toEqual(expected)
10+
})
11+
})
12+
})

‎tests/testCases/27_removeElement.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default [
2+
{ input: { nums: [3, 2, 2, 3], val: 3 }, expected: [2, 2] },
3+
{ input: { nums: [0, 1, 2, 2, 3, 0, 4, 2], val: 2 }, expected: [0, 1, 3, 0, 4] }
4+
]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /