Remove Duplicates from Sorted Array 解答
Question
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
Solution -- Two Pointers
Time complexity O(n), space cost O(1)
public class Solution {
public int removeDuplicates(int[] nums) {
int length = nums.length;
if (length <= 1)
return length;
int p1 = 0, p2 = 1;
while (p2 < length) {
if (nums[p2] != nums[p1]) {
p1++;
nums[p1 + 1] = nums[p2];
p2++;
} else {
p2++;
}
}
return p1 + 1;
}
}
Remove Duplicates from Sorted Array 解答的更多相关文章
- LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑
Description Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- leetCode练题——26. Remove Duplicates from Sorted Array
1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
随机推荐
- MFC 消息的分类
来源:孙鑫 c++ 第6集
- ECharts API
From:http://echarts.baidu.com/echarts2/doc/doc.html 简介 名词解析 图表类型 line bar scatter k pie radar chord ...
- (转)25个增强iOS应用程序性能的提示和技巧--初级篇
在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的,如果你的程序运行迟钝或缓慢,会招致用户的差评.然而由于iOS设备的局限性,有时候要想获得良好的性能,是很困难的.在开发过程 ...
- java匿名内部类,多态,接口练习
1多态以及接口调用方法: public class Interface { public static void main(String[] args) { Al x = new Al(); jian ...
- ubuntu 配置jdk
shawn@e014-anle-lnx:~$ sudo su # chmod 777 jdk-6u27-linux-i586.bin # ./jdk-6u27-linux-i586.bin # mv ...
- 某APK中使用了动态注册BroadcastReceiver,Launcher中动态加载此APK出现java.lang.SecurityException异常的解决方法
在某APK中,通过如下方法动态注册了一个BroadcastReceiver,代码参考如下: @Override protected void onAttachedToWindow() { super. ...
- EffectiveC#7--选择恒定的原子值类型数据
1.恒定类型就是一但它们被创建,它们(的值)就是固定的. 恒定类型可以很好的在基于哈希代码的集合上工作.以Object.GetHashCode()方法返回的值,对同一个实例是必须相同的 2.一个客户类 ...
- IE浏览器中hasLayout的介绍
haslayout是Windows Internet Explorer渲染引擎的一个内部组成部分.在InternetExplorer中,一个元素要么对自身的内容进行计算大小和组织,要么依赖于父元素来计 ...
- ado.net数据库操作(2)
5.1使用SQLDataReader进行数据库查询 <%@ Import Namespace="System.Data" %> <%@ Import NameSp ...
- Android 使用图标字体库
首先,在assets资源文件下放入图标字体库. 我这儿采用的是fontawesome-webfont.ttf 然后, 在安卓中加载这个资源文件 Typeface fontFace = Typeface ...