从有序数组中移除重复数字,并且返回不重复数字的个数
 
遍历操作: 可以使用新的for循环 for (int n : nums){}
 
每次进行对比,并且更新第一个遇到不相等的元素的下标为i
对数组进行重新赋值操作
 
当数组长度大于1时,ans初值为1,当数组长度为0时,返回0
 
参考代码 :
 
package leetcode_50;

/***
*
* @author pengfei_zheng
* 移除有序数组中的重复元素
*/
public class Solution26 {
public int removeDuplicates(int[] nums) {
if(nums.length==0) return 0;
int i = 1; for (int n : nums)
if (n > nums[i-1])//满足则说明不重复
nums[i++] = n;//更新i
return i;
}
}

LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)的更多相关文章

  1. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  2. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  3. 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 ...

  4. 26. Remove Duplicates from Sorted Array[E]删除排序数组中的重复项

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

  5. 26. Remove Duplicates from Sorted Array C++ 删除排序数组中的重复项

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ 双指针,注意初始时左右指针指向首元素! class Solutio ...

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

  7. **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

    1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

  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 ☆(从有序数组中删除重复项)

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

随机推荐

  1. This kernel requires an x86-64 CPU, but only detected an i686 CPU.

    为了运行一款软件,我也是拼了.彻底的玩了一次,因为A需要B,我去下载B,结果B又需要C,我去下载C,结果……怎一个艰难了得.最关键的是,目前还没有达到目的!!! 先记录下过程,有时间再来一遍,也许我已 ...

  2. 什么时候layoutSubview会被调用

    文档描述不够详细,有人测试结果如下: init does not cause layoutSubviews to be called (duh) addSubview causes layoutSub ...

  3. Android学习笔记——保存数据到SQL数据库中(Saving Data in SQL Databases)

    知识点: 1.使用SQL Helper创建数据库 2.数据的增删查改(PRDU:Put.Read.Delete.Update) 背景知识: 上篇文章学习了保存文件,今天学习的是保存数据到SQL数据库中 ...

  4. python unittest 3- 框架Nose

    当前python的测试框架主要有以下三个: 1)zope.testing 2)py.test 3)Nose Nose下载:https://github.com/nose-devs/nose 1.Nos ...

  5. jQuery easyUI的datagrid,如何在翻页以后仍能记录被选中的行

    1.先给出问题解决后的代码 <%@ page language="java" import="java.util.*" pageEncoding=&quo ...

  6. ios的两种界面跳转方式

    1.在界面的跳转有两种方法,一种方法是先删除原来的界面,然后在插入新的界面,使用这种方式无法实现界面跳转时的动画效果. if(self.rootViewController.view.supervie ...

  7. nuget类库xml说明以及类库说明文件添加到包中

    1.nuget包制作添加xml操作:项目右键属性,生成配置输出xml文档文件,debug,release都配置一下,项目右键 yesway.redis.csproj 文件增加: 添加类库说明文件con ...

  8. Eclipse Maven 配置setting.xml 的镜像远程仓库

    1.在.m2中新建settings.xml文件 1.window-->Preferences-->Maven-->User Settings 3.点击open file 编辑将远程仓 ...

  9. Go之继承的实现

    go的继承是使用匿名字段来实现的 package util //----------------Person---------------- type Person struct { Name str ...

  10. phpVirtualBox – 用浏览器操作虚拟机

    摘自:https://code.google.com phpVirtualBox 一个开源的,VirtualBox的用户界面,用PHP编写的AJAX实现.作为一个现代的Web界面,它允许你远程访问和控 ...