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. TimesTen中文乱码问题(其实是cmd.exe中文乱码)

    解决了导入数据的问题后,从导师那儿拷贝了一些样例数据,结果发现cmd.exe中文乱码(开始我以为是TimesTen无法显示中文).在网上搜了很多,因为我以为是TimesTen无法显示中文,所以都是搜的 ...

  2. xampp访问phpmyadmin访问不了

    我的xampp版本是xampp-linux-x64-5.6.15-2-installer.run, 浏览器输入“我的ip/phpmyadmin”出现如下问题: Access forbidden! Ne ...

  3. Linux指令集

    最近在学习Linux虚拟机,下面是我在学习虚拟机的过程中使用过的一些指令,及其作用. pwd-> 列出当前目录路径 ls-> 列出当前目录列表 cd-> 改变目录 mkdir-> ...

  4. python学习:列表

    列表 a = ['abc','bcd','cde','def','efg']print(a)列表的操作:增删改查 1)查:切片print(a[1:3]) #从'bcd'取到'cde',列表取值顾头不顾 ...

  5. Eclipse自动补全调教

    方法来自http://www.cnblogs.com/sunjie21/archive/2012/06/28/2567463.html 调教后可以做到: 1. sout + Tab 输出System. ...

  6. python中的单向链表实现

    引子 数据结构指的是是数据的组织的方式.从单个数据到一维结构(线性表),二维结构(树),三维结构(图),都是组织数据的不同方式. 为什么需要链表? 顺序表的构建需要预先知道数据大小来申请连续的存储空间 ...

  7. 0~5年一个Java程序员的晋升之路

    在程序界流行着一种默认的说法叫“黄金5年”,也就是一个程序员从入职的时候算起,前五年的选择直接影响着整个职业生涯中的职业发展方向和薪资走向,如何走好这5年,彻底从一个刚入行的菜鸟蜕变成可以以不变应万变 ...

  8. 课堂作业Complex类的实现

    #include <iostream> #include <cmath> using namespace std; class Complex{ public: Complex ...

  9. 【mysql】must reset your password using ALTER USER statement before executing this statement

    问题描述: must reset your password using ALTER USER statement before executing this statement 登录centos服务 ...

  10. 电子科技大学实验中学PK赛(二)比赛题解

    比赛地址:http://qscoj.cn/contest/27/ A题 FIFA强化 分析:这个题目要求说的比较明显,用几个if判断一下就好了.不要一判断完就输出,最好用一个ans储存下答案.输出答案 ...