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 A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
要求:不准额外空间,且时间O(n)
思路:index很重要,是去重复后的新数组的长度,默认值1。对一个已排序的有重复的数组,重复数字肯定是连续的
如1,1,1,1,2,2,2,2,5,5,5,我们需要做的就是把连续的2序列的第一个放到前面去(第一个数字之后,更新A[index]),把连续的5序列的第一个放到前面去,那么条件判断就是看什么时候可以找到前一个值和当前值不相等,那么我我们就要做处理。
代码:
class Solution {
public:
int removeDuplicates(int A[],int n){
int index=;
if(!n) return ;
for (int i=;i<n;++i)
{
if(A[i]!=A[i-]){
A[index]=A[i];
index++;
}
}
return index;
}
};
Remove Duplicates from Sorted Array(参考)的更多相关文章
- 算法题丨Remove Duplicates from Sorted Array II
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- [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 ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
随机推荐
- shutil模块 + shelve模块 二合一版
其他的看我前面的博客 import shutil # 将文件内容拷贝到另一个文件with open('old.xml','r') as read_f,open('new.xml', 'w') as w ...
- hihocoder offer收割编程练习赛11 A hiho字符串
思路: 我用的尺取. 注意题目描述为恰好2个'h',1个'i',1个'o'. 实现: #include <iostream> #include <cstdio> #includ ...
- javascript动态创建div循环列表
动态循环加载列表,实现vue中v-for的效果 效果图: 代码: var noApplicationRecord = document.getElementById('noApplicationRec ...
- 501在全志r16平台tinav3.0系统下调通pwm1的10KHZ波形
501在全志r16平台tinav3.0系统下调通pwm1的10KHZ波形 2018/10/19 19:52 版本:V1.0 开发板:SC3817R SDK:tina v3.0 1.01原始编译全志r1 ...
- php中 mysql 插入特殊字符(手机端的emoji表情)出现异常
今天在用mysql存储从微信服务器拉来的数据,出现插入数据异常,报 Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F 的错误. 最终在网上查了一下,有 ...
- yum升级php版本
查看当前 PHP 版本 1 php -v 查看当前 PHP 相关的安装包,删除之 1 2 3 4 5 yum list installed | grep php yum remove php ...
- CentOS 初体验十四:阿里云安装Gitlab
网址:https://about.gitlab.com/install/#centos-7 https://blog.csdn.net/zhaoyanjun6/article/details/7914 ...
- vue 中动画配置
<transition name="fade"> <router-view ></router-view> </transition& ...
- vue 接口统一管理
在外部创建一个API文件夹,然后创建一个API.js 例如 const filmbanner = 'api/billboard/home?__t=1498823077473'; const film ...
- linux系统查看网络连接情况
netstat命令状态说明: CLOSED 没有使用这个套接字[netstat 无法显示closed状态] LISTEN 套接字正在监听连接[调用listen ...