LeetCode之Remove Duplicates from Sorted Array
1、题目
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.
2、实现
public class Solution {public int removeDuplicates(int[] a) {if (null == a) {return 0;}int length = a.length;// if (length > 0)// a[0] = a[0];int newLen = 1;for (int i = 1; i < length; ++i) {if (a[i] != a[i - 1]) {a[newLen++] = a[i];}}return newLen;}
}
代码二实现:
public int removeDuplicates1(int[] a) {if (a == null || a.length == 0) {return 0;}int length = a.length;for (int i = 0; i < length - 1; ++i) {if (a[i] == a[i + 1]) {for (int j = i + 1; j < length - 1; j++) {a[j] = a[j + 1];}i--;length--;}}return length;}
3、总结
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
题目: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两个位置,如果都相等,说明出 ...
最新文章
- 袁隆平院士在湖南农业大学新生典礼上的致辞
- Pandas简明教程-适用于竞赛、研究以及办公自动化
- Spring框架简介
- ip pim spare 源树 和 共享树_iPhone通过内置应用与电脑传输共享文件指南
- Python基础教程:为元组中的每一个元素命名
- 贵州·中国南方数据中心示范基地获批
- 两个list关联合并_算法分享---两个有序链表的合并(C语言)
- 女士细线毛衣起多少针_从起针到缝合,教你织毛衣的各种要点(详细教程)
- 聋人工学院计算机老师,聋人工学院12位手语老师用双手传递声音
- Error:java: 无效的源发行版: 9 解决方案论述
- mr图像翻转的原因_MR成像技术讲解
- JBoss WildFly 7 连接到 ActiveMQ 5.9
- EHCache的使用教程
- 三维重建方法--激光or视觉
- 关于外卖骑手被困在算法里的问题解决方法浅析
- 关于软件产品化的几点思考【转】
- java怎么捕捉游戏窗口_捕捉桌面上的窗口信息
- 微信小程序全栈开发实践 第三章 微信小程序开发常用的API介绍及使用 -- 3.7 网络接口简介(七)学习EventChannel对象
- dep和rpm的安装
- 关于c中的%x及其它格式化符