LeetCode算法入门- Remove Duplicates from Sorted Array -day21

  1. 题目描述
    Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.

Example 1:

Given 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 returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn’t matter what values are set beyond the returned length.

  1. 题目分析
    将一个数组重复的元素去掉,并返回数组长度。

  2. Java实现
    定义两个指针,i指针从0开始,j从1开始,如果nums[i]与nums[j]相等,则j向后移动,移到nums[i]与nums[j]不相等,这是才将
    nums[++i] = nums[j],最后返回i+1.

class Solution {public int removeDuplicates(int[] nums) {if(nums.length == 0) return 0;int i = 0;for(int j = 1; j < nums.length; j++){if(nums[j] != nums[i]){i++;nums[i] = nums[j];}}return i+1;}
}

LeetCode算法入门- Remove Duplicates from Sorted Array -day21相关推荐

  1. 【算法】LeetCode算法题-Remove Duplicates from Sorted Array

    这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...

  2. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  3. LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array

    LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...

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

  5. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

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

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

  7. LeetCode 80. Remove Duplicates from Sorted Array II

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

  8. 2016.6.17——Remove Duplicates from Sorted Array

    Remove Duplicates from Sorted Array 本题收获: 1."删除"数组中元素 2.数组输出 题目: Given a sorted array, rem ...

  9. LeetCode算法入门- Remove Element -day20

    LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...

最新文章

  1. 深思:外卖背后的人工智能算法揭秘
  2. Python中将dict转换为kwargs
  3. php百合网,来百合网直播,做最真实的自己
  4. python 键盘输入_跟我一起学python | 探究07
  5. SpringBoot进阶教程 | 第四篇:整合Mybatis实现多数据源
  6. Struts2 Result 类型和对应的用法详解 2
  7. asp如何将图片文件上传到mysql数据库中_怎样才能利用ASP把图片上传到数据库
  8. mac机器下远程仓库添加完毕之后,却无法上传应有的内容。
  9. java程序员_java程序员这个职业赚钱吗,看一线程序员怎么说
  10. c/c++常见关键字
  11. 简单三步,教你搭建一个私有云盘
  12. 3dmax中slice plane切割平面的作用
  13. 默认关闭idea2020的注释doc的rendered view模式
  14. jenkins+Xcode+蒲公英实现ipa自动打包发布全攻略
  15. 花样16流水灯c语言程序,8个花样流水灯c程序
  16. 从全拼音中得到汉字拼音
  17. 真无线蓝牙耳机哪款适合女孩子?高颜值佩戴舒适,这五款蓝牙耳机可以考虑
  18. Qt snippets
  19. 思科配置成网站服务器,思科路由器的配置NTP服务器的基本方法
  20. AE基础教程(7)——第7章 区域显示,透明网格

热门文章

  1. 45 张图深度解析 Netty 架构与原理
  2. 设为首页 和 收藏本站js代码 兼容IE,chrome,ff
  3. php iis redis,iis windows phpstudy安装redis扩展
  4. httpSession的正确理解
  5. 计算机专业英语的理解,计算机专业英语之理解网络地址
  6. webjs求数组的中位数‘_算法:一道常见的数组题,但很多人却写不出来(JAVA)...
  7. matlab 判断鼠标按下_轻巧可爱,支持多设备——雷柏Ralemo Air1乐萌鼠标
  8. python安装rarfile模块_python windows下安装pip及rarfile
  9. Win7旗舰版系统hosts文件位置在哪里
  10. Win7系统搜索功能无法使用的应对办法