算法描述:

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. oracle 之 插入超长字段并包含&字符的处理方法

    oracle 在插入超长数据字符串时是默认转为varchar2类型,而这类型只有4000字节,即使通过oracle改变字符串类型为clob,也是在插入时默认转为varchar2类型. 处理方式:可以通 ...

  2. 【Entity framework】Code First Approach

    开篇之前感谢 china_fucan的文章给我的帮助,下面的评论也解决了很多问题同样给予感谢. code first 项目中的ORM框架如果采用的是EF,那么可能会采用code first的方式去使用 ...

  3. ETCD原理

    etcd:从应用场景到实现原理的全方位解读 从etcd的架构开始,深入到源码中解析etcd 1 架构 从etcd的架构图中我们可以看到,etcd主要分为四个部分. HTTP Server: 用于处理用 ...

  4. mysql表分区存储过程

    本文为博主原创,未经允许不得转载: 由于数据库一张表数据量有几千万条,而且在不断增长,看见公司前辈写了一个创建表分区的存储过程,感觉 甚是牛逼,在此供自己保留学习. /*PROCEDURE creat ...

  5. XMatch: 您的部门管理助手

    本博客为XMatch项目宣传博客. XMatch: 您的部门管理助手 目录 一.产品概述 二.产品功能 三.产品的创新特色 四.推广方案 五.宣传图 一.产品概述 当前社团的各方面管理工作主要都由手工 ...

  6. 17秋 软件工程 团队第三次作业 预则立&他山之石

    题目:团队作业-预则立&&他山之石 团队: 我说嘻(xì)哈(hà)你说侠 17秋 软件工程 团队第三次作业 预则立&他山之石 1.确立团队选题,建立和初步熟悉团队git的协作 ...

  7. 19. --mus-- 音乐,娱乐(词20)

  8. SAP-批量修改主数据(客户、供应商、物料)

    SAP-批量修改主数据(客户.供应商.物料) TCODE: MASS 对于批量修改主数据如客户,供应商等,可以试用一下Mass , 它所能修改的范围如下: 选定要修改的对象后,点击运行,会要求选择需要 ...

  9. JaveWeb 公司项目(1)----- 使Div覆盖另一个Div完成切换效果

    最近在做网页,用的是CSS+DIV的布局方法,搭建了一个简易的界面,大体上分为三个部分,如图所示: 左侧的为主功能导航栏,右侧是具体的功能实现,下方是固定的版权声明,单击左边不同的导航按钮,在div中 ...

  10. ubuntu14.04 Keras框架搭建

    >>>sudo su >>> pip3 install -U --pre pip setuptools wheel >>> pip3 instal ...