前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode: Remove Element

Leetcode: Remove Element

作者头像
卡尔曼和玻尔兹曼谁曼
发布2019-01-22 16:01:54
7820
发布2019-01-22 16:01:54
举报

题目: 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.

这道题比较简单直接看代码,这里多写几个版本的作参考! C++版本

代码语言:javascript
复制
class Solution
{
public:
    int removeElement(int A[], int n, int elem)
    {
        int pt = 0;
        for (int i = 0; i < n; i++)
        {
            if (A[i] != elem) A[pt++] = A[i];
        }
        return pt;
    }
};

C#版本:

代码语言:javascript
复制
public class Solution
{
    public int RemoveElement(int[] A, int elem)
    {
        int pt = 0;
        for (int i = 0; i < A.Length; i++)
        {
            if (A[i] != elem) A[pt++] = A[i];
        }
        return pt;
    }
}

Python版本:

代码语言:javascript
复制
class Solution:
    # @param    A       a list of integers
    # @param    elem    an integer, value need to be removed
    # @return an integer
    def removeElement(self, A, elem):
        pt = 0
        for i in range(len(A)):
            if A[i] != elem:
              A[pt] = A[i]
              pt += 1
        return pt

Java版本:

代码语言:javascript
复制
public class Solution
{
    public int removeElement(int[] A, int elem) {
        int pt = 0;
        for (int i = 0; i < A.length; i++)
        {
            if (A[i] != elem) A[pt++] = A[i];
        }
        return pt;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年03月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档