import java.util.Arrays;

/**
* Source : https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/
*
*
* 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 RemoveDuplicates { /**
*
* 因为是有序的,所以如果有重复的元素一定是相邻的,只要判断相邻的不相等的个数就知道不重复的元素个数
*
* @param num
* @return
*/
public int remove (int[] num) {
int pos = 0;
for (int i = 0; i < num.length - 1; i++) {
if (num[i] != num[i + 1]) {
num[pos] = num[i];
pos++;
}
}
System.out.println(Arrays.toString(num));
// 另外加上最后一个元素
return pos + 1;
} public static void main(String[] args) {
RemoveDuplicates removeDuplicates = new RemoveDuplicates();
int[] arr = new int[]{1,1,2};
int[] arr1 = new int[]{1,1,1,2};
int[] arr2 = new int[]{1,1,22,22,22,33};
System.out.println(removeDuplicates.remove(arr));
System.out.println(removeDuplicates.remove(arr1));
System.out.println(removeDuplicates.remove(arr2));
}
}

leetcode — remove-duplicates-from-sorted-array的更多相关文章

  1. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  2. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  3. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  4. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  5. [LeetCode]Remove Duplicates from Sorted Array题解

    Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...

  6. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  7. [LeetCode] Remove Duplicates from Sorted Array II [27]

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  8. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  9. [LeetCode] Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  10. LeetCode——Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. powershell_基础篇

    powershell 想必大家对windows操作系统下的cmd命令提示符可能并不陌生,大多数人都应该使用过它.而对于今天我们要学习的PowerShell跟cmd有什么关系呢?可以简单地说,Power ...

  2. 2018-2019-2 网络对抗技术 20162329 Exp4 恶意代码分析

    目录 Exp4 恶意代码分析 一.基础问题 问题1: 问题2: 二.系统监控 1. 系统命令监控 2. 使用Windows系统工具集sysmon监控系统状态 三.恶意软件分析 1. virustota ...

  3. Rest API 操作List Items

    获取所有的List Itemsfunction getItems(url) { $.ajax({ url: url, type: "GET", headers: { "a ...

  4. JAVA DESIGN PATTERN

    工厂模式(factory) 简单工厂模式的概念 就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建.简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承 ...

  5. 离校登记网页项目个人总结(Alpha阶段)

    个人小结 在Alpha阶段,我和我的小团队六人,经过六天的努力完成了我们最初需求分析里的基本功能,在这里为我们团队的成功表示祝贺.在这个过程中,对于自己的表现觉得既有做的好的方面,也有很多不足需要改进 ...

  6. CSS content换行实现字符点点点loading效果

    CSS代码如下: dot { display: inline-block; height: 1em; line-height: 1; text-align: left; vertical-align: ...

  7. Qt5+MSVC2015环境将VS2015编写的控制台程序转化为GUI程序

    如题所述,如何将VS2015编写的控制台程序转化为Qt5+MSVC2015环境编译的GUI程序? 最近想到这个操作,类似于Linux下使用的命令行操作转到Windows下使用GUI操作,看了控制台的命 ...

  8. JAVA多线程学习笔记(1)

    JAVA多线程学习笔记(1) 由于笔者使用markdown格式书写,后续copy到blog可能存在格式不美观的问题,本文的.mk文件已经上传到个人的github,会进行同步更新.github传送门 一 ...

  9. 使用ANY和ALL条件

    在比较运算符中,可以出现ALL和ANY,表示“全部”和“任一”,但是ALL和ANY不能单独使用,需要配合单行比较操作符>.>=.<.<=一起使用.其中: > ANY : ...

  10. C语言复习6_doWhile循环

    基本语法 do{ 循环操作 }while(循环条件); 特点:先执行,再判断 先执行一遍循环操作 符合条件,循环继续执行 否则循环退出 例题: #include <stdio.h> #in ...