LeetCode:Remove Element

来源:互联网 发布:什么布吸水性最好 知乎 编辑:程序博客网 时间:2024/05/20 19:29

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.

// Source : https://oj.leetcode.com/problems/remove-element/// Author : Chao Zeng// Date   : 2014-12-21//输出还要保证数组已经删除了elemclass Solution {public:    int removeElement(int A[], int n, int elem) {        int ans = n;        for (int i = 0; i < n; i++){            if (A[i] == elem){                ans--;            }        }        int k = 0;        for (int i = 0; i < n; i++){            if (A[i] != elem)                A[k++] = A[i];        }        return ans;    }};


0 0
原创粉丝点击