Remove Element
来源:互联网 发布:java api 1.8中文 编辑:程序博客网 时间:2024/05/15 18:26
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) { // Note: The Solution object is instantiated only once and is reused by each test case. int cnt = 0; for(int i = 0; i < n; ++i) { if(elem == A[i]) { ++cnt; } else { A[i-cnt] = A[i]; } } return n - cnt; }};
1 2 1 1 3 1 1 4
这样的话, 直接遍历一次就可以了, 每个数字只移动一次
a[i]移动的距离, 就是a[i]前面出现elem的次数
0 0
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Add Two Numbers
- Django admin进阶
- Remove Duplicates from Sorted List
- 我重写的Excel 净现值和内部回报率的公式
- windows下搭建爬虫框架scrapy
- Remove Element
- mac中安装python, pydev, beautifulsoup, lxml, scrapy
- 10.9苹果安装,首先写入懒人版10.9正式版,
- 在mac上配置cocos2d-x开发环境
- 戴尔笔记本安装10.7.5纪实,CPU:i5 430m,显卡gt330m,内存8G网卡:博通4311声卡矽玛特
- 一些简单的编程练习题
- 用PHP生成随机数的函数
- android Pc端截屏方法
- php 随机显示图片的函数(实例分享)