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. 认证模式之Basic模式

    HTTP协议规范中有两种认证方式,一种是Basic认证,另外一种是Digest认证,这两种方式都属于无状态认证方式,所谓无状态即服务端都不会在会话中记录相关信息,客户端每次访问都需要将用户名和密码放置 ...

  2. Shell入门之概念

    1.一切皆是文件: 在bash Shell 中一切皆是文件,不管是我们认为的文本文件,还是那些文件夹的东西,在这里都是文件,Linux只管比特和字节流,而不关心他们最终组成了什么格式,这些工作交给在L ...

  3. Leetcode_104_Maximum Depth of Binary Tree

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964475 Maximum Depth of Binar ...

  4. FSM之SMC使用总结

    FSM之SMC使用总结 Part1: Smc.jar state machine compiler usage Reference:     http://smc.sourceforge.net/   ...

  5. Linux System Programming --Chapter Seven

    文件和目录管理 一.文件与其元数据 我们首先看一下一个简单的文本文件是怎么保存的: 打开vim,编辑一段文本: [root@localhost ~]# vim hello.txt 编辑内容如下: op ...

  6. JavaScript进阶(五)js中取小数整数部分函数

    js中取小数整数部分函数 丢弃小数部分,保留整数部分 js:parseInt(7/2) 向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 四舍五入 js: Math.round(7 ...

  7. 网站开发进阶(二十二)HTML UI知识汇总(更新中...)

    HTML知识汇总(更新中...) 1.<iframe> 标签 浏览器支持 所有浏览器都支持 <iframe> 标签. 定义和用法 iframe 元素会创建包含另外一个文档的内联 ...

  8. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  9. 【LaTeX排版】LaTeX论文排版<四>

    1.表格的插入     一般的表格插入的代码如下: \begin{table}[H] \centering \begin{tabular}{|c|c|c|} \hline 感知方法&优点&am ...

  10. LeetCode之旅(16)-Climbing Stairs

    题目描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...