题目:

删除元素

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

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

 样例

给出一个数组 [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. Winform webBrowser 不跳转网页

    private void webBrowser1_NewWindow(object sender, CancelEventArgs e) { string url = ((WebBrowser)sen ...

  2. hbase meta表的结构

    下面看下hbase:meta 表的结构,hbase:meta表中,保存了每个表的region地址,还有一些其他信息,例如region的名字,HRegionInfo,服务器的信息.hbase:meta表 ...

  3. PHP弱类型安全问题笔记

    一.类型转换问题    intval(); var_dump(intval('1asdfasd'));  //1 var_dump(intval('awqw12'));  //0 var_dump(i ...

  4. WPF 使用定时器

    WPF 使用定时器:<ProgressBar Height="10" HorizontalAlignment="Left" Margin="28 ...

  5. docker 感性体验

    Docker 1.0正式发布!1.0 版本包含很多新特性,这也是 Docker 的首个产品级的版本.从今天开始,你将会一直听到一个新的概念 —— Docker as a platform ,其组件包括 ...

  6. ios上取得设备唯一标志的解决方案

    iOS 7中苹果再一次无情的封杀mac地址,现在已经不能获取ios7设备的物理地址.那么在开发中如何才能标识设备的唯一性呢?apple公司提供的方法是通过keychain来存一些标志信息,然后通过存的 ...

  7. 谁是最好的Coder

    谁是最好的Coder 时间限制:1000 ms  |  内存限制:65535 KB 难度:0   描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所以他选了帅 ...

  8. JPA学习---第八节:使用JPQL语句进行查询

    1.JPQL 语句查询,代码如下: @Test public void query(){ EntityManagerFactory factory = Persistence.createEntity ...

  9. Notes of the scrum meeting(2013/10/27)

    软工项目组buaa_smile确定自由项目主题及实现功能的scrum meeting meeting time:1:00~2:00p.m.,October 27th,2013 meeting plac ...

  10. C语言基础:两个变量交换值的方法

    学习任何语言基础时,两个数值得交换是必须掌握的,下面是3种不同的方式(c语言) 方法一:利用数学的计算技巧 #include <stdio.h> int main() { , b = ; ...