索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github: https://github.com/illuz/leetcode
027. Remove Element (Easy)
链接:
题目:https://oj.leetcode.com/problems/remove-element/
代码(github):https://github.com/illuz/leetcode
题意:
删除一个数组里值为 elem 的所有数。
分析:
用两个指针,一个为可放位置的指针,一个为扫描指针。
因为不难,Java 和 Python 的做法都和 C++ 一样,这里就不给出了。
代码:
C++:
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int ret = 0;
for (int i = 0; i < n; i++)
if (A[i] != elem)
A[ret++] = A[i];
return ret;
}
};