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. Python数据分析实战

    Python数据分析实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1nlHM1IW8MYg3z79TUwIsWg 提取码:ux8t 复制这段内容后打开百度网盘手 ...

  2. lua_table 学习

    lua  table (表)   Table 的常用操作   local fruits = {“aaa”,”bbb”,”ccc”,”ddd”,”eee”,”fff”,”ggg”}   table.co ...

  3. Android与ios 在input上的差异

    input{ -webkit-appearance:none; }

  4. python练习题目

    1.查看本机安装python版本 2.用python打印"Hello World",给出源代码和执行结果 a.命令行窗口输出(前提:python程序加入PATH系统环境变量) b. ...

  5. python学习:字符串

    字符串 #字符串操作# 对应操作:# 1.重复输出字符串# print('hello'*2)# 2.[],[:]通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表#print('h ...

  6. mybatis的基本语句的应用

    大家好今晚整理有关mybatis的添加删除修改更新的操作 一.select <!-- 查询学生,根据id --> <select id="getStudent" ...

  7. react-native 组件整理

    好早之前整理的部分组件,不全 怕丢

  8. Vs 发布编译问题

    如果勾选了预编译 发布后的目录会有PrecompiledApp.config文件,bin目录中会有App_global.asax.dll和App_global.asax.compiled文件 不勾选预 ...

  9. numpy.random 常用函数详解之简单随机数篇(Simple random data)

    1.numpy.random.rand(d0,d1,d2,...,dn) 参数:d0,d1,d2,...,dn 须是正整数,用来描述生成随机数组的维度.如(3,2)代表生成3行2列的随机数组. 返回值 ...

  10. Node.js(day6)

    初始化准备工作 初始化目录 nmp init -y 安装基本的第三方插件 express npm install express --save art-template npm install art ...