[LeetCode] Remove Element [20]

題目

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

原題鏈接(點我)

解題思路

給一個數組和一個數字,移除該數字在數組中所有出現的地方。
這是一個非常簡單的題目,應該不用多說,讀懂題目,一次AC。

代碼實現

class Solution {
public:
    int removeElement(int A[], int n, int elem) {
        if(A==NULL || n<=0) return 0;
        int start = 0;
        for(int i=0; i<n; ++i){
            if(A[i] != elem){
                A[start++] = A[i];
            }
        }
        return start;
    }
};

如果你覺得本篇對你有收穫,請幫頂。

另外,我開通了微信公衆號--分享技術之美,我會不定期的分享一些我學習的東西.
你可以搜索公衆號:swalge 或者掃描下方二維碼關注我

(轉載文章請註明出處: http://blog.csdn.net/swagle/article/details/29214323 )
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章