26. Remove Duplicates from Sorted Array(从排序数组中删除重复) —— Java
来源:互联网 发布:淘宝卖家注册流程图 编辑:程序博客网 时间:2024/05/18 14:22
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.
PS: 这里虽然返回int值,但实际上打印出来的是原来数组的前n个值。
所以修改需要在原来的数组中修改,后面的数字可以忽略不管。
Run Code Result:
Your input
[1,1,2,2,2,3,4,4,4,4,4,5,6,7,8,9,9,9]
Your answer
[1,2,3,4,5,6,7,8,9]
Expected answer
[1,2,3,4,5,6,7,8,9]
class Solution { public int removeDuplicates(int[] nums) { if(nums.length <= 1) return nums.length; int index = 0; nums[index++] = nums[0]; for(int i=1; i<nums.length; i++){ if(nums[index-1] != nums[i]){ nums[index++] = nums[i]; } } return index; }}
阅读全文
0 0
- 26. Remove Duplicates from Sorted Array(从排序数组中删除重复) —— Java
- Remove Duplicates from Sorted Array 从排序数组中删除重复
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- leetcode 26. Remove Duplicates from Sorted Array从排序数组中移出重复元素(双指针)
- Remove Duplicates from Sorted Array——移除排序数组中重复元素
- [leetcode 26]Remove Duplicates from Sorted Array——移除排序数组中重复元素
- Leetcode刷题记——26. Remove Duplicates from Sorted Array(删除数组重复元素)
- 26. Remove Duplicates from Sorted Array(删除已排序数组中的重复数)-two pointers
- 100.Remove Duplicates from Sorted Array-删除排序数组中的重复数字(容易题)
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- Leetcode 80. Remove Duplicates from Sorted Array II(从已排序数组中移除重复元素)
- LeetCode 26. Remove Duplicates from Sorted Array(删除重复)
- LeetCode OJ 之 Remove Duplicates from Sorted Array(删除有序数组中重复的数)
- Remove Duplicates from Sorted Array 删除数组中的重复元素
- LinCode Remove Duplicates from Sorted Array 删除排序数组中的重复数字
- LintCode Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
- FFMPEG学习【组件文档——视频缩放和像素格式转换器】
- 分类与检索
- poj 1753【枚举+dfs(位向量法)】
- VS2015 Git 源码管理工具简单入门
- 北交大选课系统抢课的GIT地址
- 26. Remove Duplicates from Sorted Array(从排序数组中删除重复) —— Java
- 大数据的核心价值是什么
- Bitmap: Error, cannot access an invalid/free'd bitmap here!
- Android消息机制底层原理
- 《oracle虚拟机配置流程(四)测试网站并登录》共四步
- test1
- h5微网页向上滑动屏幕加载跟多数据例子(原生js)
- tr:hover 背景变色的坑
- 人生苦短何不学习python