Leetcode OJ: Remove Duplicates from Sorted Array I/II
删除排序数组重复元素,先来个简单的。
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]
.
简单粗暴,重复一个则偏移量加1,遍历一次令A[i-k]=A[i]就可以了。看代码:
1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 if (n <= 1) 5 return n; 6 int k = 0; 7 for (int i = 1; i < n; ++i) { 8 if (A[i] == A[i - 1]) { 9 ++k; 10 } else if (k > 0) { 11 A[i-k] = A[i]; 12 } 13 } 14 return n - k; 15 } 16 };
题目加些条件:
Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?For example,
Given sorted array A =[1,1,1,2,2,3]
,Your function should return length =
5
, and A is now[1,1,2,2,3]
.
允许重复出现两次。
LZ比较实在,只是老实的把以上代码的A[i]==A[i-1]的条件变成了i > 1 && A[i] == A[i-1] && A[i] == A[i-2]
然后果断受教育
Input:[1,1,1,2,2,3]
Output:[1,1,2,3]
Expected:[1,1,2,2,3]
Input:[1,1,1,1]Output:[1,1,1]
Expected:[1,1]
1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 int k = 0; 5 int count = 1; 6 for (int i = 1; i < n; ++i) { 7 if (A[i] == A[i - 1]) {8 count++;9 if (count > 2) { 10 k++; 11 continue; 12 } 13 } else { 14 count = 1; 15 } 16 if (k > 0) 17 A[i - k] = A[i]; 18 } 19 return n - k; 20 } 21 };
转载于:https://www.cnblogs.com/flowerkzj/p/3619490.html
Leetcode OJ: Remove Duplicates from Sorted Array I/II相关推荐
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- LeetCode 80. Remove Duplicates from Sorted Array II
80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...
- 【leetcode】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode之Remove Duplicates from Sorted Array II
1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- LeetCode之Remove Duplicates from Sorted Array
1.题目 Given a sorted array, remove the duplicates in place such that each element appear only once an ...
- 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:2680 Remove Duplicates from Sorted Array 删除数组中的重复元素
leetcode:26 对数组元素进行去重,使得原数组重复元素最多保留1个 限制: 我们不可以额外分配数组,必须保持空间复杂度为O(1) 这个并不难实现: class Solution(object) ...
- leetCode #26 Remove Duplicates from Sorted Array
删除相同数字 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int coun ...
- 【Leetcode】Remove Duplicates from Sorted Array II
题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...
最新文章
- 将矩阵转为一行_矩阵与矩阵乘积简介
- 怎样建设WEB Cache
- Spring Security——org.springframework.security.oauth:spring-security-oauth2项目已过时解决方案
- 【第五组】头脑风暴+核心竞争力+NABCD+个人(用例+功能+技术说明书) 最后修改时间 2017.07.13...
- linux wptmp文件分析,wordpress上传图片提示“缺少临时文件夹”的解决方法
- linux之/usr/local/bin和/usr/bin区别
- java map集合 事务控制_对象回收过程?线程池执行过程? map原理?集合类关系?synchronized 和 volatile ? 同一个类的方法事务传播控制还有作用吗?java 锁...
- REST API 概念的简单介绍
- microservices kubernetes
- python官网的软件-Python编程软件 V3.9.0 官方最新版
- BZOJ5312 冒险 势能分析、线段树
- Navicat Premium 12.1.16.0 安装与激活(图文教程)
- 一张图解释清楚大数据技术架构,堪称阿里的核心机密
- 微软量子计算“天使梦”破碎,扬言的巨大胜利终究是一个“错误”
- win7 计算机名IPDNS修改,怎样改ip地址_怎样更换电脑ip地址-win7之家
- 机房消防报警系统及气体灭火防护的设计方法
- 单片机之时钟工作原理
- Django框架之MVT(1)
- flutter报错: [!] Automatically assigning platform `iOS` with version `8.0` on target `Runner` becaus
- 手赚网站搭建新手教程
热门文章
- 5.7 matlab数据插值与曲线拟合的比较
- 判断一个数组是否是另一个数组的子集
- 【Kaggle-MNIST之路】CNN结构再改进+交叉熵损失函数(六)
- Python Setuptools 升级(Upgrade)
- 爬虫为什么用Chrome?
- 讲讲排序(C++描述)
- for循环的一些困惑解决(字符串)
- P1865 A % B Problem (素数筛法,前缀和)
- dell r220服务器配置oracle linux 阵列卡,如何在Dell服务器PERC5/6阵列卡配置RAID
- python爬虫——利用BeautifulSoup4爬取糗事百科的段子