题目描述:

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.

解题思路:

对组遍历一次,并且设置一个计数器,当数组前后的元素不一样时计数器加一,同时将不一样的数字放到对应计数器的位置处。

代码如下:

public int removeDuplicates(int[] nums) {
int length = nums.length;
int count = 1;
if (length == 0)
return 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i - 1] == nums[i])
continue;
else {
nums[count] = nums[i];
count++;
}
}
return count;
}

Java [leetcode 26]Remove Duplicates from Sorted Array的更多相关文章

  1. 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++> 给出排序好的 ...

  2. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  3. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  4. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  5. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  6. LeetCode 26 Remove Duplicates from Sorted Array

    Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  7. Leetcode 26. Remove Duplicates from Sorted Array (easy)

    Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...

  8. [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  9. leetcode 26—Remove Duplicates from Sorted Array

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

随机推荐

  1. (转)《深入理解java虚拟机》学习笔记2——Java内存溢出实例

    通过简单的小例子程序,演示java虚拟机各部分内存溢出情况: (1).java堆溢出: Java堆用于存储实例对象,只要不断创建对象,并且保证GC Roots到对象之间有引用的可达,避免垃圾收集器回收 ...

  2. go语言实现的目录共享程序

    其实程序很小,只不过是想写点东西了.后天晚上要回学校考试了,转眼已经出来了69天了,2个月多一点.工资加上老妈赞助的钱,不知道能不能买台电脑,作为程序员一直用着i3-3217u实在难受.回去找同学拷点 ...

  3. 快捷设置IE代理小工具

    时间:2015-02-06 起因: 公司新装了PLM系统,用这个系统必须使用指定IP段的IP才能访问.所以为了还能愉快的继续使用代理进行特定网站的访问,我们必须要频繁的去设置IE代理,这也太麻烦了吧. ...

  4. 一道简单的IOS面试题-b

    题目: (参考:陈曦 包子的iOS开发)我在code review的时候,发现了某个viewController中有这样一段代码,觉得很不妥当,请尝试找出代码中的任何问题,或者可以优化的部分. -(i ...

  5. csu 1312 榜单(模拟题)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1312 1312: 榜单 Time Limit: 1 Sec  Memory Limit: 128 ...

  6. 1057: [ZJOI2007]棋盘制作 - BZOJ

    Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴 ...

  7. Linux下使用GDB调试程序

    问题描述:          Linux下使用GDB调试程序 问题解决:          (1)生成调试文件 注:         使用命令   gdb IOStream.c   -o IOStre ...

  8. CSS的定位属性实现text-shadow属性的文本下产生阴影效果

    只要先理解text-shadow的原理,就能用定位元素进行效果的模仿. text-shadow: h-shadiv v-shadov blur color h-shadv为文本水平移动的距离,正值相对 ...

  9. uva 11151

    求最长回文串  就是将字符串翻转后求最长公子列..... #include <cstdio> #include <cstdlib> #include <algorithm ...

  10. C#基础精华05(正则表达式,)

    正则表达式 . 任意一个字符 除了\n以外的 []  [0-9]       [0-9a-zA-Z] |  或   [0-9]|[a-z] ()   提升优先级别   分组 ([a]|[0-9])[0 ...