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 ...
随机推荐
- 在java中使用dom4j解析xml
创建xml文档并输出到文件 import java.io.File; import java.io.FileOutputStream; import org.dom4j.Document; impor ...
- TOJ 4244: Sum
4244: Sum Time Limit(Common/Java):3000MS/9000MS Memory Limit:65536KByteTotal Submit: 63 ...
- 【Luogu】P2704炮兵阵地(状压DP)
题目链接 话说还真没见过能影响两行的状压.想了半天想出来f数组再多一维就能表示,但是没想到怎么才能不爆空间…… 也是从这道题里学到的一个妙招. 可以把合法状态存到一个数组里,然后用数组下标来映射状态. ...
- PAT天梯赛练习题——L3-003. 社交集群(并查集按秩合并)
L3-003. 社交集群 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在社交网络平台注册时,用户通常会输入自己的兴趣爱好, ...
- P1754 球迷购票问题 (卡特兰数,递推)
题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...
- 怎么用SQL语句查数据库中某一列是否有重复项
SELECT 某一列, COUNT( 某一列 ) FROM 表 GROUP BY 某一列 HAVING COUNT( 某一列 ) 〉1 这样查询出来的结果, 就是 有重复, 而且 重复的数量.
- net8:文本文件的创建及其读写
原文发布时间为:2008-08-06 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- Linux 下MySQL 安装与卸载
这个写的比较好:http://www.cnblogs.com/starof/p/4680083.html 2.卸载系统自带的Mariadb rpm -qa|grep mariadb / ...
- intellij idea 和 myeclipse 转换
原文出处:http://chinaxxren.iteye.com/blog/893970 当只用 intellij idea 建立 工程 1.首先是new project--->create p ...
- Codeforces 757 F Team Rocket Rises Again
Discription It's the turn of the year, so Bash wants to send presents to his friends. There are n ci ...