码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode27——Remove Element

时间:2015-02-11 16:44:55      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   

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.

难度系数:

容易

实现

int removeElement(int A[], int n, int elem) {
    int cnt = 0;
    for (int i = 0; i < n - cnt; ++i) {
        if (elem == A[i]) {
            A[i] = A[n- 1 - cnt];
            cnt++;
            i--;
        }
    }
    return n - cnt;
}

LeetCode27——Remove Element

标签:c++   leetcode   

原文地址:http://blog.csdn.net/booirror/article/details/43734533

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!