lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字
题目:
给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。
不要使用额外的数组空间,必须在原地没有额外空间的条件下完成。
样例
给出数组A =[1,1,2],你的函数应该返回长度2,此时A=[1,2]。
解题:
用Python直接搞
Python程序:
class Solution:
"""
@param A: a list of integers
@return an integer
"""
def removeDuplicates(self, A):
# write your code here
ALen =len(A)
i = 0
while i<ALen-1:
if A[i]==A[i+1]:
del A[i+1]
ALen-=1
else:
i+=1
return ALen
总耗时: 755 ms
Java程序:
public class Solution {
/**
* @param A: a array of integers
* @return : return an integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
int i = 0;
int numsLen = nums.length;
while(i<numsLen-1){
if(nums[i]==nums[i+1]){
for(int j=i+1;j<numsLen-1;j++)
nums[j]=nums[j+1];
numsLen-=1;
}else
i+=1;
}
return numsLen;
}
}
总耗时: 2393 ms
时间复杂度:O(n2)
这里原始数据是有序的,时间复杂度降到O(n)
java程序:
public class Solution {
/**
* @param A: a array of integers
* @return : return an integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
if (nums == null || nums.length == 0) {
return 0;
} int size = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != nums[size]) {
nums[++size] = nums[i];
}
}
return size + 1;
}
}
总耗时: 1824 ms
lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字的更多相关文章
- 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...
- [LeetCode]26. Remove Duplicates from Sorted Array删除排序数组中的重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- LeetCode#26 | Remove Duplicates from Sorted Array 删除有序数组中的重复元素
一.题目 Description Given a sorted array, remove the duplicates in-place such that each element appear ...
- lintcode :Remove Duplicates from Sorted List 删除排序链表中的重复元素
题目: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素每个元素只留下一个. 您在真实的面试中是否遇到过这个题? 样例 给出1->1->2->null,返回 1-& ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- 026 Remove Duplicates from Sorted Array 从排序数组中删除重复项
给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度.不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点.示例:给定数组: nums ...
随机推荐
- Ubuntu16.04.1 安装Redis-Cluster
Redis在3.0版正式引入了集群这个特性.Redis集群是一个分布式(distributed).容错(fault-tolerant)的 Redis内存K/V服务, 集群可以使用的功能是普通单机 Re ...
- Android:自定义控件样式(Selector)
前言 在开发一个应用程序过程中不可避免的要去修改组件的样式,比如按钮.输入框等.现在就看下如何通过Seletor实现样式的自定义.先看下简单的效果对比
- ipc telnet 攻击
ping %1 -n 2net use \\%1sc \\%1 config tlntsvr start= autosc \\%1 start tlntsvrtelnet %1
- 简单的优化处理 By LINQ TO SQL
最近在做关于新浪微博授权的一些minisite,数据库并不复杂,所以在数据打交道这块采用了linqtosql,开发起来更快更简单...但是随着用户访问逐渐增多,用户上传的图片也越来越多,因为首页是一个 ...
- Basic Vlan Configure
Basic Vlan CLI Configure Switch>en Switch#conf t Enter configuration commands, one per line. End ...
- str_replace使用array替换
<?php //替换采集等通过url参数传值 function admin_ff_url_repalce($xmlurl,$order='asc'){ if($order=='asc'){ re ...
- ASP.NET MVC 学习第一天
今天开始第一天学习asp.net mvc,写的不是很好,高手不要喷,希望大家能一起进步学习. 好了,开始学习 新建项目,选择mvc 4应用程序 接下来选择基本,视图引擎当然要选择Razor,如果在选择 ...
- JDBC增删改查
/* db.properties的配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/day14 username=root ...
- margin-top相对谁的问题
根据规范,一个盒子如果没有上补白(padding-top)和上边框(border-top),那么这个盒子的上边距会和其内部文档流中的第一个子元素的上边距重叠.意思便是:如果你只想margin相对于父标 ...
- ifame 跨域高度自适应
代码如下:var iframeids = ['memberIndexIframe','inquiryCenterIframe','everychinaBbsIframe']; var iframehi ...