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 ...
随机推荐
- (三)SpringIoc之初了解
IoC:Inverse of Control(控制反转) 读作"反转控制",更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控制权,交由Spring框架来 ...
- 微信小程序flex布局
一.flex布局基础 二.相对定位和绝对定位 flex的容器和元素 主轴(左-右),交叉轴(上-下) flex容器属性详解 flex-direction 决定元素的排列方向(默认row ...
- 微信“摇一摇·周边”正式开放
今日,在微信公开课第三季长沙站现场,微信团队宣布“摇一摇·周边”(以下简称“摇周边”)正式对外开放.拥有微信认证的公众帐号商户,均可通过摇周边的商户申请平台(https://zb.weixin.qq. ...
- 田字格布局html div
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- java实现的判断括号是否成对的代码,()[]{}都可以
本来想找找现成的,去,都写的好复杂.自己写一个吧.挺有成就感.哈哈 package com.test.jiexi; import java.util.Stack; public class Check ...
- codeforces_1075_C. The Tower is Going Home
http://codeforces.com/contest/1075/problem/C 题意:一个长宽均为1e9的棋盘,n个垂直障碍在x列无限长,m个水平障碍在第y行从第x1列到x2列.可以水平和垂 ...
- Vim中文编码问题
1.影响中文编码的设置项 encoding(enc):encoding是Vim的内部使用编码,encoding的设置会影响Vim内部的Buffer.消息文字等.在 Unix环境下,encoding的默 ...
- 转:函数调用的区别:_cdecl以及_stdcall
函数调用的几个概念:_stdcall,_cdecl.... 1._stdcall是Pascal程序的缺省调用方式,通常用于Win32 Api中,函数采用从右到左的压栈方式,自己在退出时清空堆栈.VC将 ...
- 网络爬虫之框架(Scrapy)
Scrapy爬虫框架 爬虫框架是实现爬虫功能的一个软件结构和功能组件集合. 爬虫框架是一个半成品,能够帮助用户实现专业网络爬虫. Scrapy爬虫框架结构:
- 解决dom4j加载xml文件性能慢的问题
在代码中使用: 1: DocumentHelper.parseText 2: SAXReader reader = new SAXReader(); Document extdocument = re ...