LeetCode OJ 26. Remove Duplicates from Sorted Array
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.
Subscribe to see which companies asked this question
【思路】
1. 举个例子[1,1,1,2,2,2,3,4,5],我的想法就是找到每一节重复数字的长度,然后把后面的数字向前移动,直到遍历到数组最后。
上述例子中,我们从头开始发现有1重复出现了3次,因此我们把1后面的数字向前移动2,变为[1,2,2,2,3,4,5],然后把数组的len变为len-2,重复上面的结果直到遍历到最后。但是这个方法的效率并不高,每发现一个重复的元素都要把后面的数字向前移动,有没有更好的思路呢?
2. 一个更好的方法是:我们维持两个变量,i 用来遍历数组,j 用来指示数组中不重复的那部分的最后一个值的下标。在遍历数组的过程中,如果当前值和前一个值不同,则nums[++j] = nums[i],否则的话继续向前遍历。形象化的过程如下:
- j = 0; i = 1;
- nums[i] 等于 nums[i-1];
- nums[i] 不等于 nums[i-1]; nums[++j] = nums[i];
- 省略若干步
【java代码1】
- public class Solution {
- public int removeDuplicates(int[] nums) {
- if(nums==null || nums.length==0) return 0;
- int len = nums.length;
- int duplen = 0;
- for(int i = 0; i < len - 1; i++){
- duplen = 0;
- for(int j = i + 1; j < len; j++){
- if(nums[j] == nums[i]) duplen++;
- else break;
- }
- if(duplen > 0){
- for(int k = i + duplen + 1; k < len; k++){
- nums[k-duplen] = nums[k];
- }
- len = len - duplen;
- }
- }
- return len;
- }
- }
【java代码2】
- public class Solution {
- public int removeDuplicates(int[] nums) {
- if (nums.length == 0)
- return 0;
- int j = 0;
- for(int i=1; i<nums.length; i++) {
- if (nums[i-1] != nums[i])
- nums[++j] = nums[i];
- }
- return j;
- }
- }
LeetCode OJ 26. Remove Duplicates from Sorted Array的更多相关文章
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode OJ】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 重读The C programming Lanuage 笔记四:c预处理
C预处理器执行宏替换.条件编译以及包含指定的文件.以#开头的命令行就是与处理器的对象.这些命令行的语法独立于语言的其他部分,它们可以出现在任何地方,其作用可延续到所在编译单元的末尾(与作用域无关).行 ...
- A- Bear and Five Cards(codeforces ROUND356 DIV2)
A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 数据库及SQL优化
一.数据库结构的设计 如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实施之前,完备的数据库模型的设计是必须的. ...
- wordpress 删除底部"自豪地采用 WordPress"
找到footer.php文件,删除以下代码即可: <footer id="colophon" class="site-footer" role=" ...
- magento删除数据
1.删除一条数据: $delete = Mage::getSingleton("core/resource")->getConnection("core_wr ...
- about hibernate lazy load and solution
about hibernate lazy load is that used when loaded again.it can increase efficienty and sava memory. ...
- load和get
参考:hibernate延迟加载(get和load的区别) Hibernate之load和get的区别 get方法的doc解释: /** * Return the persistent instanc ...
- GlusterFS无法启动原因及处理方案
启动结果: Redirecting to /bin/systemctl status glusterd.serviceglusterd.service - GlusterFS, a clustere ...
- openwrt启动过程(脚本)
来源: http://wiki.openwrt.org/doc/techref/preinit_mount#first.boot 基本的openwrt启动顺序为: 1.boot loader loa ...
- Rancher安装使用
官网 http://docs.rancher.com/rancher/latest/en/quick-start-guide/#add-hosts 安装步骤: 1 Start up a Linux m ...