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. HDU 1175 连连看(BFS)

    连连看 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. 用C#实现MD5算法

     /// <summary>     /// 一个实现MD5散列字符串的类     /// </summary>     public sealed class MD5Hash ...

  3. linux环境下安装php扩展

    本文只提供源码安装的方法(已安装pcntl为例) 其他方法请参考:http://doc3.workerman.net/appendices/install-extension.html 1.利用php ...

  4. c++ explicit 用法摘抄

    笔记 //Student.h[explicit修饰] Student (int n): Student doh(); doh = ; //没有 explicit=>doh = Student(5 ...

  5. MFC 简单输出EXCEL - (OLE)

    三图胜千言: 就是酱紫: //打印领料表 void CKnifeDlgDlg::PrintCurUsedTabel(int order) { // TODO: Add your command han ...

  6. netbeans git 配置(ssh方式)

    git出问题了,自己又重新配置了下git. 参考文章: https://netbeans.org/kb/docs/ide/git_zh_CN.html#github

  7. centos nginx

    1.关闭SELinux 查看SELinux状态: (1)/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...

  8. 定时工作方式2实现1s定时

    定时器的几种工作方式中,除0和前面的1类似都需要初始化计数值,然后开始计数,计数溢出后,需要再次控制计数的初值,但工作模式2不同于此,工作方式2将THx和TLx分开处理,将初值存放在THx中,计数时只 ...

  9. FusionCharts xml入门教程

    由于项目需求需要做一个报表,选择FusionCharts作为工具使用.由于以 前没有接触过报表,网上也没有比较详细的fusionCharts教程,所以决定好好研究FusionCharts,同时做一个比 ...

  10. ARM编译器4字节对齐

    (1)我们假设只有一个赋初值的char型全局变量,那么系统会在data区分配一个4字节的存储空间来存储它.实际上,只用了1个字节,但是为了4字节对齐,只好分配4个字节,所以就会有3个字节浪费. (2) ...