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 ...
随机推荐
- flex基础学习
Flex是Adobe开发的一种RIA,富互联网应用,用Flex开发的东西都可以使用Flash做出来,但是Flex主要是面向的程序开发人员,前台使用ActionScript和MXML. 上面介绍了fle ...
- C#简单的加密类
1.加密 public class EncryptHepler { // 验值 static string saltValue = "XXXX"; // 密码值 static st ...
- JavaScript 中的 replace 方法
定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replaceme ...
- 查看linux系统版本命令
一.查看内核版本命令: 1) [root@SOR_SYS ~]# cat /proc/version Linux version 2.6.18-238.el5 (mockbuild@x86-012.b ...
- Webserver issues | PHP manager for IIS
4 down vote accepted In order to successfully install the PHP manager for IIS 8, you need the .NET 3 ...
- Protocol Buffer使用
Protocol Buffer使用简介 字数2630 阅读5067 评论1 喜欢12 我们项目中使用protocol buffer来进行服务器和客户端的消息交互,服务器使用C++,所以本文主要描述pr ...
- using System.Threading.Tasks;
using System.Threading.Tasks; .Net并行库介绍——Task1
- matlab实现贝塞尔曲线绘图pdf查看
贝塞尔曲线绘图方法: %Program 3.7 Freehand Draw Program Using Bezier Splines %Click in Matlab figure window to ...
- 编译时IOS Device 无法选择的情况
问题描述:当你项目开发环境Xocode版本高于你本地Xocode 编译版本时,在本地运行会出现如下错误: 解决: 重写调整Deloyment Target 的版本 注:还有一种情况会出现如上错误,并 ...
- cocos2dx中的假动作,又称动作回调函数
1.动作与动画的区别 动作是:定时器+属性的改变,是帧循环的累积效应 动画是:帧图片的播放效果,我们知道电影的播放就是快速播放的胶片,这就是动画的原理 2.假动作:又称动作回调函数 四大类假动作: c ...