LeetCode-Easy刷题(7) Remove Duplicates from Sorted Array
来源:互联网 发布:恒扬数据副总经理 编辑:程序博客网 时间:2024/05/20 07:50
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 by modifying the input array in-place with O(1) extra memory.
Example:
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 new length.
给定一个排好序的数组后,删除重复的副本,这样每个元素只出现一次,并返回新的长度。 不要为另一个数组分配额外的空间,您必须通过修改带有O(1)额外内存的输入数组来实现这一点。
public static int removeDuplicates(int[] nums) { if(nums ==null || nums.length <1){ 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; }
阅读全文
0 0
- LeetCode-Easy刷题(7) Remove Duplicates from Sorted Array
- [LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)
- [Leetcode 26, easy] Remove Duplicates from Sorted Array
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (cpp)
- Leetcode 26. Remove Duplicates from Sorted Array (Easy) (java)
- [leetcode刷题] Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array [easy]
- 【Easy】26. Remove Duplicates from Sorted Array
- leetcode26[easy]--- Remove Duplicates from Sorted Array
- Easy 7 Remove Duplicates from Sorted Array(26)
- LeetCode刷题(C++)——Remove Duplicates from Sorted Array(Easy)
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- LeetCode刷题【Array】 Remove Duplicates from Sorted Array II
- LeetCode-Easy刷题(17) Remove Duplicates from Sorted List
- LeetCode: Remove Duplicates from Sorted Array
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array
- 软件工程师必备的知识-网络协议;
- 冒泡
- 6.使用github
- 鲜为人知的 Linux 命令(4)
- 著名开源网络取证工具Xplico远程未授权RCE漏洞
- LeetCode-Easy刷题(7) Remove Duplicates from Sorted Array
- 二叉树后序遍历迭代器(仅支持++)
- 边坡计算与模型
- 安装pcre-8.39和Lighttpd 在进行make的时候出现的问题
- java国际化
- centOS6.5 Nginx无法解析php文件
- seo是否是前途未卜?
- 对PostgreSQL数据库结构的宏观理解
- LeetCode-Easy刷题(8) Remove Element