lintcode:Remove Element 删除元素
题目:
给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度。
元素的顺序可以改变,并且对新的数组不会有影响。
样例
给出一个数组 [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 删除元素的更多相关文章
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- [leetcode]27. Remove Element删除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- [Leetcode] remove element 删除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- LeetCode Remove Element删除元素
class Solution { public: int removeElement(int A[], int n, int elem) { ]; int i,num=n; ;i<n;i++){ ...
- 在Python的列表中利用remove()方法删除元素的教程
在Python的列表中利用remove()方法删除元素的教程 这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区 ...
- python——remove,del,pop三种删除元素方式的区别
记性不好,整理出来以作保存 1.remove ①直接删除元素,remove(obj),顺序删除第一个遇到的,所以想要全部删除 ,需要遍历 aList = [123, 'xyz', 'zara', 'a ...
- java:Conllection中的List,ArrayList添加元素,删除元素,输出元素
java:Conllection中的List,ArrayList添加元素,删除元素,输出元素 //为list接口实例化 List<String> addlist = new ArrayLi ...
- angular.element方法汇总以及AngularJS 动态添加元素和删除元素
addClass()-为每个匹配的元素添加指定的样式类名after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点append()-在每个匹配元素里面的末尾处插入参数内容att ...
- angular.element 动态添加和删除元素
addClass()-为每个匹配的元素添加指定的样式类名after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点append()-在每个匹配元素里面的末尾处插入参数内容att ...
随机推荐
- IE8浏览器跨域接口访问异常的解决办法
IE8版本以下浏览器绝对是一个神奇的存在,忙碌好久,万事具备,居然在ajax调用接口的时候直接爆炸 陈述一下问题 首先是有这样一个接口,请求类型POST,入参JSON,出参JSON,jQuery aj ...
- 例题6-8 Tree Uva548
这道题我一直尝试用scanf来进行输入,不过一直没有成功,因此先搁置一下,以后积累些知识再进行尝试. 这道题有两种解决方案: 即先建树,再遍历和边建树边遍历.这两种方案经过实践证实效率相差不太多.应该 ...
- PHP 判断客户端请求是 Android 还是 IOS
<?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad ...
- 使用mysql关键字做类字段名报的错,花了我一个钟,坑啊
com.modelsystem.po.ProjectPlan@701faaedHibernate: insert into ld.project_plan (addTime, describe, ex ...
- Android L Ripple的使用
声明:Demo并不是有本人所写,本人只是总结在这里 工程源码: RippleDemo.zip ---------------------------------------------------- ...
- libcurl
一.LibCurl基本编程框架 二.一些基本的函数 三.curl_easy_setopt函数部分选项介绍 四.curl_easy_perform 函数说明(error 状态码) 五.libcurl使用 ...
- 【android】uiselectoer 自动化测试
转:http://blog.csdn.net/sasoritattoo/article/details/17579739 Android自动化测试主要分为Monkeyrunner.Rubotium.U ...
- C#转换日期类型
日期1999-5-31 11:20转换成 /Date(928120800000+0800)/ 其中928120800000实际上是一个1970 年 1 月 1 日 00:00:00至这个DateTim ...
- 浅析JVM内存结构和6大区域(转)
其实对于我们一般理解的计算机内存,它算是CPU与计算机打交道最频繁的区域,所有数据都是先经过硬盘至内存,然后由CPU再从内存中获取数据进行处理,又将数据保存到内存,通过分页或分片技术将内存中的数据再f ...
- 1562: [NOI2009]变换序列 - BZOJ
Description Input Output Sample Input 5 1 1 2 2 1 Sample Output 1 2 4 0 3 HINT 30%的数据中N≤50:60%的数据中N≤ ...