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 ...
随机推荐
- ffmbc——为广播电视以及专业用途量身定制的FFmpeg
无意中发现了个为广播电视以及专业用途量身定制的FFmpeg.也是开源的,记录之. ffmbc 全称是 FFMedia Broadcast,是个改版的FFmpeg,有如下功能: 创建可以导入Final ...
- MiseringThread.java 解析页面线程
MiseringThread.java 解析页面线程 http://injavawetrust.iteye.com package com.iteye.injavawetrust.miner; imp ...
- 江湖问题研究-- intent传递有没有大小限制,是多少?
出门一步,便是江湖,江湖上有许多流言. 比如这条: intent传递是有大小限制的,具体在40KB左右. 当然也有传言说是1M左右. 数百头母驴为何半夜惨叫? 小卖部安全套为何屡遭黑手? 女生宿舍内裤 ...
- Android轶事之View要去大保健?View大小自己决定?
-"爹,我要吃糖" -"好哒儿子" -"爹,我要吃包包" - "好哒儿子" - "爹,我要吃串串" ...
- OpenCV特征点检测算法对比
识别算法概述: SIFT/SURF基于灰度图, 一.首先建立图像金字塔,形成三维的图像空间,通过Hessian矩阵获取每一层的局部极大值,然后进行在极值点周围26个点进行NMS,从而得到粗略的特征点, ...
- AWR報告詳解
AWR是Oracle 10g 版本 推出的新特性, 全称叫Automatic Workload Repository-自动负载信息库, AWR 是通过对比两次快照(snapshot)收集到的统计信息 ...
- ffdshow 源代码分析 6: 对解码器的dll的封装(libavcodec)
===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...
- Material Design之CardView的使用
本文介绍CardView这个控件的使用,CardView继承至FrameLayout类,是support-v7包下的一个类,使用时必须引入cardview依赖包,可在下载的sdk文件夹中找到... 使 ...
- 标准会话管理器——StandardManager
用于保存状态的会话对象已经有了,现在就需要一个管理器来管理所有会话,例如会话id生成.根据会话id找出对应的会话.对于过期的会话进行销毁等等操作.用一句话描述标准会话管理器:提供一个专门管理某个web ...
- kettel的stream lookup报错
kettel的stream lookup报错: you can't use the 'integer-pair' algorithm when you have more than one key o ...