算法描述:

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的更多相关文章

  1. Remove Duplicates from Sorted Array [LeetCode]

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

  2. Remove Duplicates from Sorted List leetcode java

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

  3. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  4. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

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

  6. [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 ...

  7. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

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

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

  9. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

随机推荐

  1. 题解——Codeforces Round #507 (based on Olympiad of Metropolises) T1 (模拟)

    暴力模拟即可 就是情况略多 #include <cstdio> #include <algorithm> #include <cstring> using name ...

  2. EPPlus实战篇——Excel读取

    .net core 项目 可以从excel读取任何类型(T)的数据,只要T中的field的[Display(Name = "1233")]中的name==excel column ...

  3. _event_worldstate

    EventId 事件ID ID WorldStateUI.dbc第10列数字部分 StartValue 起始值 Entry 更新世界状态需要击杀生物或摧毁物体的entry,正数为生物,负数为物体 St ...

  4. hdu 6069 Counting Divisors 筛法

    Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Oth ...

  5. mongdb学习笔记

    1.MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的 2.支持动态查询 3.使用高效的二进制数据存储,包括大型对象(如视频等) 4.文件存储格 ...

  6. PHP Warning: Module 'memcache' already loaded in Unknown on line 0

    出现类似PHP Warning: Module * already loaded in Unknown on line 0,一般是可能因为升级php导致的组件重复加载,解决就是 1.vi /etc/p ...

  7. 深入JVM对象引用

    在jdk 1.2以前,创建的对象只有处在可触及(reachaable)状态下,才能被程序所以使用,垃圾回收器一旦发现无用对象,便会对其进行回收.但是,在某些情况下,我们希望有些对象不需要立刻回收或者说 ...

  8. Eclipse调试DEBUG时快速查看某个变量的值的快捷键、快速跳转到某行的快捷键

    Eclipse调试DEBUG时快速查看某个变量的值的快捷键 Ctrl + Shift + i

  9. MVC数据列表展示【三】

    一.控制器向视图传递参数的两种形式:使用到的技术有EF,linq表达式,StringBuilder,相关技术都可以在我的博客园中找到详细的技术介绍. 1. 第一种是通过字符通过foreach循环拼接将 ...

  10. 使用Hexo搭建一个简单的博客(一)

    搭建好简洁的博客框架后,回看时发现,简洁之中透露着一丝丝简陋,好的,网上关于丰富hexo的文章也很多 记录一下自己的一些瞎操作. 在你的hexo目录下,你可以看到themes文件夹里有个默认的land ...