1.利用荷兰国旗的思路,每次记住最后一个位置,遇到一个不重复的数,放在它后面,代码很简单。

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

 public class Solution {
public int removeDuplicates(int[] A) {
int len=A.length;
if(len==0) return 0;
int pos=0; for(int i=1;i<len;i++)
{
if(A[i]!=A[pos])
{
pos++;
A[pos]=A[i]; } }
return pos+1; } }

2.只要跟end的前一个比较就行了,思路跟上边,就一点不一样,原创啊

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

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].

 public class Solution {
public int removeDuplicates(int[] A) {
int len=A.length;
if(len==0) return 0;
if(len==1) return 1;
int end=1;
for(int i=2;i<len;i++)
{
if(A[i]!=A[end-1])
{
end++;
A[end]=A[i]; } } return end+1; }
}

去掉有序数组中重复数字 原地 leetcode java (最简单的方法)的更多相关文章

  1. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  2. 去掉有序数组中的重复元素 c/c++

    去掉有序数组中的重复元素: int RemoveDuplates(int A[], int nCnt) { ; ; , j = ; i < nCnt && j < nCnt ...

  3. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  4. 剑指offer:1.找出数组中重复的数(java版)

    数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...

  5. 高效率去掉js数组中重复项

    Array类型并没有提供去重复的方法,如果要把数组的重复元素干掉,那得自己想办法: function unique(arr) { var result = [], isRepeated; for (v ...

  6. 求一个数组中重复数字的个数,要求复杂度为O(n)

    给出代码 #include <stdio.h> #include <unistd.h> #include <iostream> #include <memor ...

  7. (Java)怎么去掉字符串数组中重复的值?

    String fdbs = "WXB,WXA,FDA,WXB"; String[] str = fdbs.split(","); Set set = new H ...

  8. (python)剑指Offer:数组中重复的数字

    问题描述 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1, ...

  9. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

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

随机推荐

  1. 【实习记】2014-08-29算法学习Boyer-Moore和最长公共子串(LCS)

        昨天的问题方案一:寻找hash函数,可行性极低.方案二:载入内存,维护成一个守护进程的服务.难度比较大.方案三:使用前5位来索引,由前3位增至前5位唯一性,理论上是分拆记录扩大100倍,但可以 ...

  2. .NET MVC 依赖注入 来龙去脉

    找了很久,找到一篇好文章,细读很有收获: 点击打开链接http://www.codeproject.com/Articles/560798/ASP-NET-MVC-Controller-Depende ...

  3. ubuntu14.04+opencv 3.0+python2.7安装及测试

    本文记录了ubuntu下使用源码手动安装opencv的过程.步骤来自opencv官网 此外记录了在python中安装及载入opencv的方法. 1.安装opencv所需的库(编译器.必须库.可选库) ...

  4. Python3 列表

    list是一种有序的集合,可以随时添加和删除其中的元素. >>> classmates = ['Michael', 'Bob', 'Tracy'] >>> clas ...

  5. MySQL中EXPLAIN解释命令详解

    MySQL中的explain命令显示了mysql如何使用索引来处理select语句以及连接表.explain显示的信息可以帮助选择更好的索引和写出更优化的查询语句. 1.EXPLAIN的使用方法:在s ...

  6. 数据库MySQL与xls文件的互导

    最近的一个项目需要将xls表导入到MySQL数据库中和将MySQL数据表导出到xls表中,在网上搜了很多资料,经过多次尝试终于实现了功能,废话不多说,在这粘贴出代码,希望可以帮到需要的朋友. 一.将. ...

  7. 在虚拟机的linux中利用VMware Tools实现与windows共享文件

        很多人都知道安装"VMware Tools"可以实现与windows共享,但是其实它的功能远不止此.安装了"VMware Tools"后,虚拟机的网络. ...

  8. Xcode 设置输出颜色

    是不是看厌了Xcode单调的Log,在此,我教大家实现带颜色的Log 一 首先你需要安装一个Xcode插件---XCode Colors 插件Github地址 https://github.com/r ...

  9. 集成Facebook和Twitter的Model动画-b

    这个动画.  感谢大神分享 JPPopPresentAnimation.gif 要实现这个功能分两步走:第一步,先实现这个动画.第二步,0行代码集成到项目.也就是,你不用改原有项目的任何代码,只要把写 ...

  10. 【产品体验】喵街&飞凡

    最近O2O很火啊,我也来找几个O2O产品体验下~~~ 阿里今年5月30号上线了一款线下逛街App——喵街,号称消费者的逛街神器.阿里去年已经与银泰合作一年,探索互联网和传统实体零售合作之路,这次则免费 ...