【算法】LeetCode算法题-Remove Duplicates from Sorted Array
这是悦乐书的第149次更新,第151篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26)。给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其中不重复元素的个数n,并将数组的前n个元素依次赋值为筛选后的不重复元素。不许使用新数组接收数据。例如:
nums = {1,1,2}
输出不重复元素的个数为2
数组前2个元素为1和2,即nums = {1,2,2}
本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。
02 分析并解题
特殊情况:如果数组中元素个数小于等于1,其不重复元素的个数即为数组的长度,也不用重新给前面的元素重新赋值。
获取第一个元素,与第二个元素比较,如果两数不等,count加1,数组的新第2个元素为原数组第2个元素,依次往后循环。
最后count需要加1,因为在第一次循环两数不等的时候,count是从第二个元素开始累加的,还需要算上最开始的第一位元素。
public int removeDuplicates(int[] nums) {
if (nums.length <= 1) {
return nums.length;
}
int count = 0;
for(int i=0; i<nums.length-1; i++){
if (nums[i] != nums[i+1]) {
count++;
nums[count] = nums[i+1];
}
}
return count+1;
}
03 小结
此题比较简单,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
【算法】LeetCode算法题-Remove Duplicates from Sorted Array的更多相关文章
- 乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array
乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array 一.前言 我们这次的实验是去除重复的有序数组元素,有大体两种算法. 二.Remo ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- leetcode笔记:Remove Duplicates from Sorted Array II
一.题目描写叙述 二.解题技巧 这道题和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 ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 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
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
随机推荐
- Hyperledger Fabric之模型
本文主要介绍Hyperledger Fabric的主要设计特点,为了满足功能丰富.可定制.企业化区块链解决方案. Assets - 资产定义,使得任何形式的资产,从食物到汽车到货币都可以进行自由的交换 ...
- 杭电ACM2002--计算球体积
计算球体积 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- c# 解密微信encryptedData字段
参考链接:https://www.cnblogs.com/jetz/p/6384809.html 我写了一个工具方法,直接照搬链接中的方法,还有一个工具类. public class Encrypt ...
- 从零开始学安全(三十)●使用sqlmap对网站一步步渗透
常规注入步骤第一步注入点-u "url" 判断是否是注入点 有就判断用户的权限第二步获取数据库 所有的 -u "url" --dbs第三步 查看应用程序所有数据 ...
- C# 未能加载文件或程序集“ Newtonsoft.Json” Json格式错误
原因:版本不一致,所使用的dll和配置文件中的版本不一致.解决: (1)查看所使用的Newtonsoft.Json.dll版本 ,然后把对应的版本修改在配置文件中如下,比如版本为“4.5.0.0” 修 ...
- MyEclipse中jsp编码设置
window->preference->Myeclipse->files and editors->jsp,设置编码为utf-8 2.window->preference ...
- Idea 15 激活
https://www.cnblogs.com/moko/p/5012006.html 1.把补丁下载到自己的电脑上 2.打开idea,help->edit custom VM options ...
- 如何用ftp上传静态网站到虚拟空间
ftp是一种网络传输协议,你要上传网站到空间首先你要安装一个FTP软件,你申请的空间有一个网址.账号.密码之类的,你打开FTP输入这些链接就可以准备上传网站了,软件打开有一个本地界面,还有一个空间界面 ...
- 我从Angular 2转向Vue.js, 也没有选择React
译者按: 通过使用Angular的经历,作者已经完全转为Vue粉了!我们Fundebug目前还是用AngularJS 1,坦白说,学习曲线蛮陡的. 原文: Why we moved from Angu ...
- GA中的术语及经常分析的指标
GA中的术语 跳出客流:只浏览了网站的一个页面,并且没有进一步动作的访客目标转化:通常缩写为目标或转化,这是网站上面的一个预期或动作,通常被认为比标准网页更有价值,例如:"确认购买" ...