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

Leet -- 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.


就是在不使用额外空间的情况下,直接修改nums数组,移除重复元素,返回新长度。


思路:
使用两个变量:
front 用于遍历nums数组,从下标0开始,一直往前走
now 仅当nums[now]与nums[front]不等时,now+1,并把nums[now]=nums[front]


最后返回nums[now+1]


实现代码:



public class Solution {
    public int RemoveDuplicates(int[] nums) {
            if(nums == null || nums.Length == 0){
		return 0;
	}
	
	int now = 0;
	int front = 1;
	for(;front < nums.Length; ){
		if(nums[now] != nums[front])
		{
			now ++;
			nums[now] = nums[front];
		}
		front++;
	}
	
	return now + 1;
    }
}


相关文章:

  • LeetCode -- Best Time to Buy and Sell Stock II
  • 海闊天空 信樂團
  • Contains Duplicate III
  • LeetCode -- Combination Sum
  • MySQL添加用户
  • LeetCode -- Candy
  • Leet -- Plus One
  • Leet -- Generate Parentheses
  • LeetCode -- Distinct Subsequences
  • LeetCode -- SpiralOrder
  • Windows 2003下成功配置IIS+Php+Mysql+Zend Optimizer+GD库+Phpmyadmin
  • LeetCode -- WordBreak II
  • Azure 证书配置错误: The service configuration file does not provide the certificate identification
  • Linux精彩一句话最新版
  • LeetCode -- Count Complete Tree Node
  • [NodeJS] 关于Buffer
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • android 一些 utils
  • Brief introduction of how to 'Call, Apply and Bind'
  • css系列之关于字体的事
  • eclipse(luna)创建web工程
  • iOS小技巧之UIImagePickerController实现头像选择
  • js继承的实现方法
  • PaddlePaddle-GitHub的正确打开姿势
  • PHP那些事儿
  • rc-form之最单纯情况
  • 算法-插入排序
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 微信小程序设置上一页数据
  • 一道面试题引发的“血案”
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • #QT(智能家居界面-界面切换)
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • (2)Java 简介
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (function(){})()的分步解析
  • (深度全面解析)ChatGPT的重大更新给创业者带来了哪些红利机会
  • (转)重识new
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • ******IT公司面试题汇总+优秀技术博客汇总
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .Net Remoting常用部署结构
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .NET连接数据库方式
  • .w文件怎么转成html文件,使用pandoc进行Word与Markdown文件转化
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • @Resource和@Autowired的区别
  • [ C++ ] STL---string类的使用指南
  • [ CTF ] WriteUp- 2022年第三届“网鼎杯”网络安全大赛(朱雀组)
  • [ABP实战开源项目]---ABP实时服务-通知系统.发布模式
  • [Android] Upload package to device fails #2720
  • [AutoSar]BSW_Com02 PDU详解
  • [AutoSar]工程中的cpuload陷阱(三)测试