description:

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.

thoughts:

first we should juage if the array is empty, if true, we can just return length=0,and do not do anything special about the array.if not, then the length must >=1.we first record the nums[0]  as temp and  legnth = 1, then find the first different number with it,make the temp equal to it, and length++;end make the nums[length -1] = temp.do this operation untill you have scan all the value of the nums,then return length.

my code in java

package easy;

public class RemoveDuplicatesOfSortedArray {

    public int removeDuplicates(int[] nums){
//if the nums is empty return length = 0
int length = 0;
if(nums.length>0){
int temp = nums[0];
length=1;
for(int i = 0; i<nums.length;i++){
if(temp != nums[i]){
//找到所有不一样的数
temp = nums[i];
length++;
//将所有不一样的数放在相应的位置
nums[length - 1] = temp;
}
}
} return length;
} public static void main(String[] args){
RemoveDuplicatesOfSortedArray a = new RemoveDuplicatesOfSortedArray();
int[] nums = new int[]{1,1,2};
int length = a.removeDuplicates(nums);
System.out.println(length);
} }

remove duplicate of the sorted array的更多相关文章

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

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

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

  3. Remove Duplicates from Sorted Array II leetcode java

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

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

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

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

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

  6. Remove Duplicates from Sorted Array II

    题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

  7. No.026:Remove Duplicates from Sorted Array

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

  8. LeetCode 26 Remove Duplicates from Sorted Array

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

  9. Remove Duplicates from Sorted Array II [LeetCode]

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

随机推荐

  1. c#一些特殊语法

    1.using 语法 using不仅可以作为导入包,重命名类名.还可以释放资源 using (Pen gridLinePen = new Pen(Color.red)) { e.Graphics.Dr ...

  2. 《java入门第一季》之好玩的正则表达式

    先不通过正则表达式来判断输入的qq号码是否正确. import java.util.Scanner; /* * 校验qq号码. * 1:要求必须是5-15位数字 * 2:0不能开头 * * 分析: * ...

  3. 集群通信组件Tribes之如何维护集群成员信息

    一个集群包含若干成员,要对这些成员进行管理就必须要有一张包含所有成员的列表,当要对某个节点做操作时通过这个列表可以准确找到该节点的地址进而对该节点发送操作消息.如何维护这张包含所有成员的列表是本节要讨 ...

  4. java对象大小

    Java对象的内存布局:对象头(Header),实例数据(Instance Data)和对齐填充(Padding) 对象头在32位系统上占用8B,64位系统上占16B. 无论是32位系统还是64位系统 ...

  5. 015-OC基础语法-OC笔记

    学习目标 1.[了解]Objective-C语言简介 2.[掌握]第一个OC程序 3.[掌握]OC中的字符串 4.[熟悉]OC中的一些玩意 5.[了解]面向过程与面向对象 6.[掌握]类的声明和实现 ...

  6. Cocos2D中的Framerate状态

    对于额外绘制调试物理引擎的支持,Cocos2D同样可以绘制概述计数器,尤其是帧速率(framerate)显示. 为了启用这些概述计数器标签,你只需添加如下一行代码,比如说在AppDelegate.m里 ...

  7. gc实例与gc报告的阅读

    gc报告的阅读 首先我们看一条gc报告 D:\杂项\java>java -verbose:gc -Xms20m -Xmx20m -Xmn10m -XX:+PrintGCDetails -XX:+ ...

  8. Android Studio环境下搭建ReactNative

    1.安装Android Studio首先肯定是 安装Android Studio(包含SDK)(国内推荐)ps:这里有一点要注意,需要为SDK配置环境变量,名称必须为ANDROID_HOME 2.安装 ...

  9. iOS中UITableView分割线左侧顶齐

    iOS 7开始UITableView的分割线不在从左侧边界开始了,而是默认空出了一段距离. 如果想要使用默认的分割线而且还要从左侧边界开始的话,有几种解决方式: 1.在tableView的代理方法中设 ...

  10. NumberProgressBar开源项目学习

    1.概述 多看多学涨姿势, github真是个宝库 这个项目主要是实现数字进度条效果 github地址在https://github.com/daimajia/NumberProgressBar 感谢 ...