remove duplicate of the sorted array
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的更多相关文章
- 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 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- Remove Duplicates from Sorted Array II leetcode java
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates from Sorted Array II
题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- No.026:Remove Duplicates from Sorted Array
问题: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode 26 Remove Duplicates from Sorted Array
Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- Java 8新特性探究(五)Base64详解
BASE64 编码是一种常用的字符编码,在很多地方都会用到.但base64不是安全领域下的加密解密算法.能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数据的正确性,有些网关或系统只能使 ...
- HTML5 预加载
原文地址: HTML5 Link Prefetching 原文日期: 2010年07月07日 翻译日期: 2013年08月13日 浏览器厂商和开发者之间共同努力的一个方向就是让网站更快.现在已有很多广 ...
- 【翻译】Sencha Cmd中脚本压缩方法之比较
概述 为什么要修改默认设置 YUI压缩 Google Closure编译器 UglifyJS 案例研究Ext JS 6示例应用程序 注意事项 自定义JS压缩 小结 概述 这么多年来,Web开发人员都被 ...
- 数据的压缩存储与解压缩算法实现(C语言)
在一些嵌入式的项目设计中,空间是相当宝贵的,因为一个CPU的存储是有限的,所以此时我们在保存数据的时候,喜欢来进行压缩保存,著名的有哈夫曼树算法,专门用来做压缩的算法,当然,本节我们不讨论这些稍微高级 ...
- Css的学习之旅-css的选择器(2)
1.最常用的是派生选择器:eg:ul li{ color:red} 2.id选择器:eg:#id{color:red} 3.类选择器:设置标签的class = "",类似id.用点 ...
- 常用Map实现类对比
翻译人员: 铁锚 翻译时间: 2013年12月12日 原文链接: HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap Map 是最常用的数据结构之一 ...
- python函数参数是值传递还是引用传递(以及变量间复制后是否保持一致):取决于对象内容可变不可变
函数参数传递本质上和变量整体复制一样,只是两个变量分别为形参a和实参b.那么,a=b后,a变了,b值是否跟着变呢?这取决于对象内容可变不可变 首先解释一下,什么是python对象的内容可变不可变? p ...
- hadoop上的C++程序开发
hadoop可以用C++开发,命令运行方式为pipes,例子:hadoop pipes -conf job_config.xml -input input/myfile.txt -output out ...
- Java进阶(五十二)利用LOG4J生成服务日志
Java进阶(五十二)利用LOG4J生成服务日志 前言 由于论文写作需求,需要进行流程挖掘.前提是需要有真实的事件日志数据.真实的事件日志数据可以用来发现.监控和提升业务流程. 为了获得真实的事件日志 ...
- EBS R12安装升级(FRESH)(二)
3 Linux系统设置 这一节步骤基本都在终端root用户下进行. 自行熟悉vi或其他文本工具的用法. 3.1 host-only外网连接 如果用的NAT模式这一节略过. 右击当前主机连接外网的网卡, ...