LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
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.
思路:
也是指针思想,利用新array任意元素一定不会比旧array该元素所在对应位置index大的原理(也可以看作栈),通过两个指针表征新旧array。
遍历一遍array,只有当遇到新元素时,压入新array。
AC代码:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()==)
return ;
int p=,n=nums.size();
for(int i=;i<n;i++){
if (nums[i]!=nums[p]){
nums[++p]=nums[i];
}
}
return p+;
}
};
LeetCode(26)题解:Remove Duplicates from Sorted Array的更多相关文章
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- leetcode笔记:Remove Duplicates from Sorted Array II
一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...
- 【LeetCode算法-26】Remove Duplicates from Sorted Array
LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- leetcode第26题--Remove Duplicates from Sorted Array
problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- LeetCode(26) Remove Duplicates from Sorted Array
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
随机推荐
- 【编程工具】Sublime Text3 之 Emmet 插件的详细使用的方法
这篇关于 Emmet 插件使用的博文之前就想写了,今天刚好闲暇时间,就花了一些时间进行了总结. 我们都这道 Emmet 这款插件在前端设计里被称为神器,确实,神器称号名不虚传.因为这款插件可以帮助我们 ...
- ubuntu ssh连接服务器保持长时间不断
方法: ssh -o serveraliveinterval=60 username@ip
- 【bzoj1109】[POI2007]堆积木Klo 动态规划+树状数组
题目描述 Mary在她的生日礼物中有一些积木.那些积木都是相同大小的立方体.每个积木上面都有一个数.Mary用他的所有积木垒了一个高塔.妈妈告诉Mary游戏的目的是建一个塔,使得最多的积木在正确的位置 ...
- BZOJ 4161 Shlw loves matrixI ——特征多项式
矩阵乘法递推的新姿势. 叉姐论文里有讲到 利用特征多项式进行递推,然后可以做到k^2logn #include <cstdio> #include <cstring> #inc ...
- request,response,session,cookie,application
A:request 客服端向服务器端请求 JAVA讲究封装,所以Request也是某个东西的封装,到底是什么东西呢? 按字面意思:请求! 从Http协议说起,当你发一个请求到服务端的时候,你会把一些信 ...
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
- golang 进程、线程、协程 简介
https://www.cnblogs.com/shenguanpu/archive/2013/05/05/3060616.html https://studygolang.com/articles/ ...
- Oracle 12c在SQL Devolper中添加cdb和pdb连接
Oracle 12c如果按默认流程安装的话会有一个叫orcl的cdb容器和一个叫pdborcld的pdb容器 一.连接名为orcl的cdb容器 连接名:localorcl 用户名:SYS 口令:Ora ...
- 74.Search in a 2D Matrix
/* * 74.Search in a 2D Matrix * 12.5 by Mingyang * 这里面的对应挺巧的: * 这个就是将2D矩阵转化成1行数组的对应表.所以对于二分查找法的初始值为: ...
- IOS -- base64编码
在iOS7以后可以用NSData自带的base64EncodedStringWithOptions进行编解码: 方法如下: - (NSString *)encodeToBase64String:(UI ...