leetCode-数组:Remove Duplicates from Sorted Array
Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素
考察数组的基本操作:
class Solution {public int removeDuplicates(int[] nums) {if (nums==null || nums.length==0)return 0;int index = 1;for(int i =1; i<nums.length; i++){if(nums[i]!=nums[i-1]){nums[index] = nums[i];index++;}}return index;}public static void main(String[] args) {int[] arr = { 1, 2, 2, 3, 3 };arr = removeDuplicates(arr);System.out.println(arr.length); } }
Remove Duplicates from Sorted Array II (Java)
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].
最多允许两个重复,输出结果数组。
解法1:当counter是2时,就直接跳过即可,否则说明元素出现次数没有超,继续放入结果数组,若遇到新元素则重置counter。总体算法只需要扫描一次数组,所以时间上是O(n),空间上只需要维护一个index和counter,所以是O(1)。
public int removeDuplicates(int[] A) {if(A==null || A.length==0)return 0;int idx = 0;int count = 0;for(int i=1;i<A.length;i++){if(A[i]==A[i-1]){count++;if(count>=3)continue;}else{count = 1;}A[idx++]=A[i];}return idx; }
解法二:
public class Solution {public int removeDuplicates(int[] A) {if (A.length <= 2)return A.length;int prev = 1; // point to previousint curr = 2; // point to currentwhile (curr < A.length) {if (A[curr] == A[prev] && A[curr] == A[prev - 1]) {curr++;} else {prev++;A[prev] = A[curr];curr++;}}return prev + 1;} }
转载于:https://www.cnblogs.com/DLlearning/p/7879711.html
leetCode-数组:Remove Duplicates from Sorted Array相关推荐
- 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 OJ: Remove Duplicates from Sorted Array I/II
删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...
- 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
1.题目 Given a sorted array, remove the duplicates in place such that each element appear only once an ...
- 【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 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两个位置,如果都相等,说明出 ...
最新文章
- idea配置echache.xml报错Cannot resolve file 'ehcache.xsd'
- Python 自动化-pywinauto库定位树结构控件里的树节点实例演示
- [hypervisor]-AArch64 (hypervisor)Virtualization学习笔记
- window10最全win键组合技巧(win10快捷键)
- 2019牛客暑期多校训练营(第六场)
- 控制反转-依赖倒置-依赖注入
- java学习(99):车站卖票问题
- MySQL关键字EXPLAIN的用法及其案例
- dj鲜生-22-模板抽离-列表页详情页模板的抽离-base_detail_list
- [转]Oracle_ProC编程
- 电气自动化c语言用什么软件,电气自动化需要哪些高级语言?
- 自己的包增加为第三方包,使用Eclipse环境报Unresolved import错误(pycharm可用正常引用)...
- JAVA实现生成GIF动态图加文字(完整版无License带锯齿优化处理)
- 详解:什么是VXLAN?
- 【报名开启】2021年博客之星总评选,属于你的年终表彰
- Qt QStringLiteral
- java实现俄罗斯方块项目
- 构建整车七自由度模型
- 【PostgreSQL】数据表的增删改查
- 徕卡DNA03水准仪USB驱动