题目:

删除元素

给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度。

元素的顺序可以改变,并且对新的数组不会有影响。

 样例

给出一个数组 [0,4,4,0,0,2,4,4],和值 4

返回 4 并且4个元素的新数组为[0,0,0,2]

解题:

Java程序:

public class Solution {
/**
*@param A: A list of integers
*@param elem: An integer
*@return: The new length after remove
*/
public int removeElement(int[] A, int elem) {
// write your code here
int count = A.length;
// for(int i=0;i<count;i++)
int i=0;
while(i<count){
if(A[i]==elem){ for(int j=i;j<count-1;j++)
A[j] = A[j+1];
count--;
}else
i++;
}
return count;
}
}

总耗时: 1782 ms

这样竟然也可以通过,这里时间复杂度是O(n2),同时你会发现,只是把数组移位了,没有真正的删除,但是这里的测试结果是输出正确情况下的长度。。。。
还有个问题就是,测试用例太少了。。。。

官当答案也是这样搞的。。。

Python程序:

class Solution:
"""
@param A: A list of integers
@param elem: An integer
@return: The new length after remove
"""
def removeElement(self, A, elem):
# write your code here
i = 0
ALen = len(A)
while i<ALen:
if A[i]==elem:
ALen-=1
del A[i]
else:
i+=1
return ALen

总耗时: 286 ms

Python list 可以直接删除指定index下标的元素

lintcode:Remove Element 删除元素的更多相关文章

  1. leetCode 27.Remove Element (删除元素) 解题思路和方法

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  2. [leetcode]27. Remove Element删除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  3. [Leetcode] remove element 删除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  4. LeetCode Remove Element删除元素

    class Solution { public: int removeElement(int A[], int n, int elem) { ]; int i,num=n; ;i<n;i++){ ...

  5. 在Python的列表中利用remove()方法删除元素的教程

    在Python的列表中利用remove()方法删除元素的教程 这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区 ...

  6. python——remove,del,pop三种删除元素方式的区别

    记性不好,整理出来以作保存 1.remove ①直接删除元素,remove(obj),顺序删除第一个遇到的,所以想要全部删除 ,需要遍历 aList = [123, 'xyz', 'zara', 'a ...

  7. java:Conllection中的List,ArrayList添加元素,删除元素,输出元素

    java:Conllection中的List,ArrayList添加元素,删除元素,输出元素 //为list接口实例化 List<String> addlist = new ArrayLi ...

  8. angular.element方法汇总以及AngularJS 动态添加元素和删除元素

    addClass()-为每个匹配的元素添加指定的样式类名after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点append()-在每个匹配元素里面的末尾处插入参数内容att ...

  9. angular.element 动态添加和删除元素

    addClass()-为每个匹配的元素添加指定的样式类名after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点append()-在每个匹配元素里面的末尾处插入参数内容att ...

随机推荐

  1. php判断是否为手机客户端

    function isWap() {        $http_agent = isset ( $_SERVER ["HTTP_USER_AGENT"] ) ? $_SERVER ...

  2. Nginx+Tomcat动静分离

    需求:nginx处理用户请求的静态页面,tomcat处理用户请求jsp页面,来实现动态分离,nginx处理静态页面效率远高于tomcat,这样一来就能更好的提高并发,处理性能. 准备软件: 下载jdk ...

  3. nginx+keepalived双主高可用负载均衡

    实验环境及软件版本:CentOS版本: 6.6(2.6.32.-504.el6.x86_64)nginx版本: nginx-1.6.3keepalived版本:keepalived-1.2.7 主LB ...

  4. hexo-github 博客搭建

    安装nodejs 从官网下载系统对应的源码 wget -qO- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | ...

  5. C# 缓存学习总结

    昨天整理了一下缓存的基本用法,和缓存依赖类 CacheDependency类的使用,今天整理一下缓存的数据库依赖类SqlCacheDependency 1.数据库依赖类SqlCacheDependen ...

  6. 把数组排成最小的数/1038. Recover the Smallest Number

    题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323.   Give ...

  7. ALTER TABLE causes auto_increment resulting key 'PRIMARY'

    修改表为主键的自动增长值时,报出以下错误:mysql> ALTER TABLE YOON CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT ...

  8. (ACM)C++ STL 训练(第一天)

    因为老师说ACM考的是纯C++,所以打算抛弃VS的VC++不用了,针对纯C++的编译器有Intel Compiler(不过要钱),MinGw(个人用的),当然还有微软的VC++ 编译器,IDE你们可以 ...

  9. 网络笔记01-2 scoket

    scoket: 1.socket /** 第一个参数(domain): 表示用什么协议 AF_INET 为IPV4开发 第二个参数(type): 表示scoket为什么类型SOCK_STREAM为TC ...

  10. 常用汉字的Unicode码表

    \u96d5\u864e\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u77 ...