[leetcode]Remove Duplicates from Sorted Array(Java实现)
来源:互联网 发布:家电销售数据 编辑:程序博客网 时间:2024/06/05 19:28
本题为leetcode题库28:
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.
牛客网测试地址:https://www.nowcoder.com/questionTerminal/a519784e89fb40199fbe7993786715b1
Java代码如下:
package go.jacob.day523;public class Demo1 {public int removeDuplicates(int[] nums) {//异常输入if(nums==null||nums.length<=1)return nums==null?0:nums.length;int count=1;for(int i=1;i<nums.length;i++){//如果数组当前元素与前一个元素不同,则加入新数组if(nums[i]!=nums[i-1])nums[count++]=nums[i];}return count;}}
阅读全文
0 0
- [leetcode]Remove Duplicates from Sorted Array(Java实现)
- [leetcode]Remove Duplicates from Sorted Array II(Java实现)
- [Leetcode] Remove Duplicates from Sorted Array (Java)
- [LeetCode][Java] Remove Duplicates from Sorted Array
- 【leetcode】Remove Duplicates from Sorted Array【java】
- leetcode:Remove Duplicates from Sorted Array 【Java】
- LeetCode : Remove Duplicates from Sorted Array [java]
- LeetCode 26 — Remove Duplicates from Sorted Array(Java)
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- [leetcode](Remove Duplicates from Sorted Array II C语言实现)
- [Leetcode] Remove Duplicates from Sorted Array II (Java)
- LeetCode – Remove Duplicates from Sorted Array (Java)
- LeetCode – Remove Duplicates from Sorted Array II (Java)
- LeetCode – Remove Duplicates from Sorted Array (Java)
- 【Leetcode】Remove Duplicates from Sorted Array in JAVA
- [LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)
- Remove Duplicates from Sorted Array---leetcode 我的java题解
- python解析xml报错xml dom.minidom UnicodeEncodeError: 'ascii' codec can't encode character
- 一周搞定期末考系列之《数字电子技术》
- Android 去电状态判断 录音
- [C++]第七届蓝桥杯决赛之广场舞
- java实现遍历树形菜单方法——struts.xml实现
- [leetcode]Remove Duplicates from Sorted Array(Java实现)
- 14多校 B-A simple dynamic programming problem
- 东莞市2011年信息学特长生测试题 花店布置(dp)
- 记录一些面试问题吧。
- POJ-3621: Sightseeing Cows【最优比率生成环】
- java I/O系统(8)-文件压缩
- main(int argc,char *argv[])
- 菜鸟初学maven,pom标签解释
- ninja ripper新版教程