LeetCode_26---Remove Duplicates from Sorted Array
来源:互联网 发布:c 数组 编辑:程序博客网 时间:2024/05/29 23:24
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 nums = [1,1,2]
,
Your function should return length = 2
, with the first two elements of nums being 1
and 2
respectively. It doesn't matter what you leave beyond the new length.
fan
Code:
package mengdexinTest;import java.util.Arrays;//436msApublic class LeetCode26 {public static int removeDuplicates(int[] nums) {int len = nums.length;int result = 0;int start = 0;while (start < len) {nums[result] = nums[start];while (start + 1 < len) {if (nums[start] == nums[start + 1]) {start++;} else {break;}}start++;result++;}return result;}public static void main(String[] args) {int[] a = { 1, 2, 2, 2, 3, 3, 6, 9 };int[] b = { 1 };int[] c = { 2, 2, 2 };int[] d = {};System.out.println("第二种:" + removeDuplicates(a));System.out.println("第二种:" + Arrays.toString(a));}}
0 0
- LeetCode_26---Remove Duplicates from Sorted Array
- leetcode_26. Remove Duplicates from Sorted Array
- leetcode_26. Remove Duplicates from Sorted Array 在排好序的数组中删除重复的数字,不允许分配其他空间
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Kickstart的高级应用
- using使用法总结
- Android之NDK开发
- 程序员保值的4个秘密
- AD(Active Directory)域信息同步,组织单位、用户等信息查询
- LeetCode_26---Remove Duplicates from Sorted Array
- 版本控制(八)--git分支&管理
- 树的遍历
- Git学习笔记(一)
- Python 学习之二:Python超短教程
- ios 打包ipa步骤
- js--window关闭事件
- 查看文件系统大小du df区别
- 应用程序开发选择工具应注重运行效率还是易用性