[LeetCode] 027. Remove Element (Easy) (C++)
來源:程序員人生 發布時間:2015-03-19 08:29:20 閱讀次數:3262次
索引:[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
題意:
刪除1個數組里值為 elem 的所有數。
分析:
用兩個指針,1個為可放位置的指針,1個為掃描指針。
由于不難,Java 和 Python 的做法都和 C++ 1樣,這里就不給出了。
代碼:
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;
}
};
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈