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、总结

方法一总结:我们不能重新申请空间,在原基础数组改,我们知道只要说到“连续数字”,我么应该马上想到这个数字和前面的数字相同,我们在原始数组上,第一个元素就是新数组的第一个元素,后面如果新元素和前面的元素不一样,我们就把这个后面的元素添加在新数组的末尾。
方法二总结:
记得进行i--和length--

LeetCode之Remove Duplicates from Sorted Array相关推荐

  1. 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++> 给出排序好的 ...

  2. Leetcode OJ: Remove Duplicates from Sorted Array I/II

    删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...

  3. LeetCode 80. Remove Duplicates from Sorted Array II

    80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...

  4. 【leetcode】Remove Duplicates from Sorted Array

    题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  5. LeetCode之Remove Duplicates from Sorted Array II

    1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

  6. LeetCode 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  7. leetcode:2680 Remove Duplicates from Sorted Array 删除数组中的重复元素

    leetcode:26 对数组元素进行去重,使得原数组重复元素最多保留1个 限制: 我们不可以额外分配数组,必须保持空间复杂度为O(1) 这个并不难实现: class Solution(object) ...

  8. leetCode #26 Remove Duplicates from Sorted Array

    删除相同数字 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int coun ...

  9. 【Leetcode】Remove Duplicates from Sorted Array II

    题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...

最新文章

  1. 袁隆平院士在湖南农业大学新生典礼上的致辞
  2. Pandas简明教程-适用于竞赛、研究以及办公自动化
  3. Spring框架简介
  4. ip pim spare 源树 和 共享树_iPhone通过内置应用与电脑传输共享文件指南
  5. Python基础教程:为元组中的每一个元素命名
  6. 贵州·中国南方数据中心示范基地获批
  7. 两个list关联合并_算法分享---两个有序链表的合并(C语言)
  8. 女士细线毛衣起多少针_从起针到缝合,教你织毛衣的各种要点(详细教程)
  9. 聋人工学院计算机老师,聋人工学院12位手语老师用双手传递声音
  10. Error:java: 无效的源发行版: 9 解决方案论述
  11. mr图像翻转的原因_MR成像技术讲解
  12. JBoss WildFly 7 连接到 ActiveMQ 5.9
  13. EHCache的使用教程
  14. 三维重建方法--激光or视觉
  15. 关于外卖骑手被困在算法里的问题解决方法浅析
  16. 关于软件产品化的几点思考【转】
  17. java怎么捕捉游戏窗口_捕捉桌面上的窗口信息
  18. 微信小程序全栈开发实践 第三章 微信小程序开发常用的API介绍及使用 -- 3.7 网络接口简介(七)学习EventChannel对象
  19. dep和rpm的安装
  20. 关于c中的%x及其它格式化符

热门文章

  1. ASP.NET Core 6 Minimal API
  2. 读《中台架构与实现》
  3. 2021年春招Elasticsearch面试题
  4. 在Windows上安装Docker
  5. 灵魂拷问:你和大佬,技术差距有多大?
  6. 如何在 Asp.Net Core 中 管理敏感数据
  7. 第五站 使用winHex利器加深理解数据页
  8. 软硬件协同编程 - C#玩转CPU高速缓存(附示例)
  9. 需求与问题——一个老现象
  10. 2019 .NET China Conf:路一直都在,社区会更好