博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode:Remove Element
阅读量:4474 次
发布时间:2019-06-08

本文共 637 字,大约阅读时间需要 2 分钟。

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.

class Solution {

public:
int removeElement(int A[], int n, int elem) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int sumElem=0;
int num=n-1;
for( int i= 0; i <= num ; i ++ )
{
if(A[i]==elem)
{
sumElem++;
while( i < num && A[num] == elem)
{
sumElem++;
num--;
}
if(i==num)
{
return n-sumElem;
}
A[i] = A[num];
num--;
}
}
return n-sumElem;
}
};

转载于:https://www.cnblogs.com/litana/archive/2013/05/18/3085726.html

你可能感兴趣的文章
程序网格生成
查看>>
《Linux命令行与shell脚本编程大全 第3版》Linux命令行---35
查看>>
注册vue组件的几种方式
查看>>
一步一步学Silverlight 2系列(22):在Silverlight中如何用JavaScript调用.NET代码
查看>>
用vs2010创建带有AssociationForm的工作流
查看>>
流程平台:报错排错 - 未将对象引用设置到对象的实例
查看>>
报表查询:收费汇总表
查看>>
使用PHP QR Code生成二维码
查看>>
集合(一)
查看>>
Python Day 14 函数(内置函数,匿名函数(lambda表达式))
查看>>
8.30——9.4日随笔之一(面向对象)
查看>>
mysql中的data下的数据文件(.FRM、.MYD、.MYI)恢复为数据
查看>>
洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)...
查看>>
silverlight中实现页面传值
查看>>
1-bag of words 视觉词袋模型-1
查看>>
客户端使用自定义代理类访问WCF服务
查看>>
敏捷开发一千零一问系列之九:总体架构什么时机进行?(上)
查看>>
Java模板模式
查看>>
知识拓展-位的概念
查看>>
mysql将表数据导出为txt或csv文件
查看>>