当前位置: 首页 > news >正文

删除排序数组中的重复项Remove Duplicates from Sorted Array

删除排序数组中的重复项Remove Duplicates from Sorted Array

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.

代码

class Solution {
    public int removeDuplicates(int[] nums) {
        // 使用双指针
        if(nums==null || nums.length == 1){
            return nums.length;
        }
        int i = 0,j =1;
        while(j<nums.length){
            if(nums[i]==nums[j]){
                j++;
            }else{
                i++;
                nums[i]=nums[j];
                j++;
            }
        }
        return i+1;
    }
}

分析

可以设置两个指针,慢指针i,快指针j。

第一步

数相等,j++

ij
01234
11123

第二步

数相等,j++

ij
01234
11123

第三步

数不等

ij
01234
11123

i++

ij
01234
11123

移动nums[i]=nums[j]

ij
01234
11123
2

j++

ij
01234
11123
2

第四步

这个时候,可以看到数不等

ij
01234
11123
2

还是那三步。
i++

ij
01234
11123
2

移动nums[i]=nums[j]

ij
01234
11123
23

j++

ij
01234
11123
23

第五步

这个时候循环条件while(j<nums.length)不满足,j>length,跳出循环,
返回i+1

完整代码

package remove_duplicates_from_sorted_array;

public 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;
	}
	
	//打印数组
	private static void printArry(int len,int[] nums) {
		System.out.println();
		String ss = "[";
		for (int i = 0; i < len; i++) {
			if(i+1==len) {
				ss+=nums[i];
			}else {
				ss+=nums[i]+",";
			}
		}
		ss+="]";
		System.out.println(ss);
	}
	
	public static void main(String[] args) {
		int[] nums = {1,1,2};
		Solution solu = new Solution();
		printArry(nums.length,nums);
		int len = solu.removeDuplicates(nums);
		printArry(len,nums);
	}
	
}

相关文章:

  • 字符串转换整数 (string-to-integer-atoi)
  • 最长公共前缀(longest-common-prefix)
  • 罗马数字转整数(roman-to-integer)
  • 删除链表的倒数第N个节点(remove-nth-node-from-end-of-list)
  • 为什么说合数它一定能够被某个素数整除?
  • 实现 strStr()采用kmp算法
  • translate-shell的使用方法
  • ksnapshot使用
  • 报数count-and-say
  • 递归需要遵守的重要规则
  • 组合总和(combination-sum)
  • 组合总和combination-sum
  • 如何查看隐藏的密码(限chrome浏览器)
  • 最大子序和(maximum-subarray)——动态规划和贪心双解法
  • Deepin系统安装SSH服务
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • vagrant 添加本地 box 安装 laravel homestead
  • 数据可视化之 Sankey 桑基图的实现
  • 译有关态射的一切
  • Android开发者必备:推荐一款助力开发的开源APP
  • python最赚钱的4个方向,你最心动的是哪个?
  • 长三角G60科创走廊智能驾驶产业联盟揭牌成立,近80家企业助力智能驾驶行业发展 ...
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • ​iOS安全加固方法及实现
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (9)STL算法之逆转旋转
  • (C语言)球球大作战
  • (function(){})()的分步解析
  • (附源码)php投票系统 毕业设计 121500
  • (七)Knockout 创建自定义绑定
  • (四)库存超卖案例实战——优化redis分布式锁
  • (未解决)macOS matplotlib 中文是方框
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)Oracle 9i 数据库设计指引全集(1)
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .NET 读取 JSON格式的数据
  • .NET大文件上传知识整理
  • .php结尾的域名,【php】php正则截取url中域名后的内容
  • .pop ----remove 删除
  • /etc/fstab和/etc/mtab的区别
  • @31省区市高考时间表来了,祝考试成功
  • [ 代码审计篇 ] 代码审计案例详解(一) SQL注入代码审计案例
  • [100天算法】-二叉树剪枝(day 48)
  • [383] 赎金信 js
  • [C#] 基于 yield 语句的迭代器逻辑懒执行
  • [codevs 1288] 埃及分数 [IDdfs 迭代加深搜索 ]
  • [C语言]——分支和循环(4)
  • [Docker]十一.Docker Swarm集群raft算法,Docker Swarm Web管理工具
  • [ERROR]-Error: failure: repodata/filelists.xml.gz from addons: [Errno 256] No more mirrors to try.
  • [flink总结]什么是flink背压 ,有什么危害? 如何解决flink背压?flink如何保证端到端一致性?
  • [Linux] PXE批量装机
  • [linux学习]apt-get参数解析
  • [Node.js]连接mongodb
  • [Python]`threading.local`创建线程本地数据