Remove Duplicates From Sorted Array leetcode java
算法描述:
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.
算法分析:一个有序数组,出去其中出现次数大于1的数,返回剩余元素个数
代码:设置两个指针,i 遍历原数组,j 是去除重复后的下标
- public int removeDuplicates(int[] nums) {
- if(nums == null || nums.length == 0)
- return 0;
- int len = nums.length;
- int j = 1; //数组第一个元素是不需要变的,所以长度至少为1
- for(int i = 1; i < len; i++){ //数组遍历从下标1开始
- if(nums[i] == nums[i - 1])
- continue;
- else {
- nums[j++] = nums[i];
- }
- }
- return j;
- }
Remove Duplicates From Sorted Array leetcode java的更多相关文章
- Remove Duplicates from Sorted Array [LeetCode]
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates from Sorted List leetcode java
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
随机推荐
- PKM(个人知识管理)类软件收集(偶尔更新列表)
evernote(印象笔记) Wiz 有道云 麦库 leanote GoogleKeep OneNote SimpleNote(wp家的,免费) pocket(稍后读的软件,同类的还有Instapap ...
- 题解——P1133 教主的花园DP
直接设二维状态具有后效性,会爆零 然后需要加一维 然后70 看了题解之后发现没有考虑1和n的关系 考虑之后,四十 然后懵逼 突然发现自己的ans更新写错了,导致每次ans都是第一个取30的情况的解 然 ...
- 取消开机logo,改成代码刷屏
将开机logo改成开始时代码刷屏,这样就能很方便看到开始时的一些问题 首先 sudo chmod 666 /etc/default/grub 然后将 GRUB_CMDLINE_LINUX_DEFAUL ...
- HDU 5754 Life Winner Bo(各类博弈大杂合)
http://acm.hdu.edu.cn/showproblem.php?pid=5754 题意: 给一个国际象棋的棋盘,起点为(1,1),终点为(n,m),现在每个棋子只能往右下方走,并且有4种不 ...
- C#——LINQ语句
委托: //delegate 返回值 委托名(参数); //委托不能在方法中定义 ////实例化委托,并赋值 //委托名 实例名 = new 委托名(函数名).lambda表达式; //使用委托实例, ...
- NPOI导入导出EXCEL通用类,可直接使用在WinForm项目中
由于XSSFWorkbook类型的Write方法限制,Write完成后就自动关闭流数据,所以无法很好的支持的Web模式,网上目前也未找到好的解决方案. 注意:若直接使用在WinForm项目中,必需先下 ...
- 将.db文件导入SQLServer2008数据库
最近要做一个项目,需要连接数据库,给我的数据文件是sqlite,我需要将数据导入到SQLServer数据库 需要借助一个软件:DBDBMigration 页面最上方的选择框内,先选择数据文件类型,这里 ...
- 阿里云CentOS Linux服务器上搭建邮件服务器遇到的问题
参考文章: 阿里云CentOS Linux服务器上用postfix搭建邮件服务器 Linux系统下邮件服务器的搭建(Postfix+Dovecot) 本来想自己搭建邮件服务器,但是看到一篇资料表示阿里 ...
- Servlet中web.xml的配置
引言:这是一个采用原生Servlet开发的项目的一个简要配置,在这里记录一下,以便以后用到了 可以直接copy,如又侵权,请联系本博主. <?xml version="1.0" ...
- Java技术——你真的了解String类的intern()方法吗
0.引言 什么都先不说,先看下面这个引入的例子: String str1 = new String("SEU")+ new String("Calvin") ...