【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
来源:互联网 发布:centos 6.4 厂家 编辑:程序博客网 时间:2024/05/16 19:04
【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
原题
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.
题目大意
给定一个排序的数组,将数组中的重复元素去掉,相同的只保留一个,并且返回数组新的元素个数,不要创建一个新的数组来保存结果。在常量时间内解决这个问题
解题思路
从第二个元素开始处理,记为当前处理的元素,如果当前元素与他的前一个元素相同就删除这个元素,如果不同就将它移动到正确的位置,返回最后数组元素人个数。
代码实现
算法实现类
public class Solution { public int removeDuplicates(int[] A) { if (A.length == 0) { return 0; } int index = 0;//[0,index]只记录数组中出现的按从小到大的唯一一个数,已经排好序了 int next = 1; // 算法思想:找index之后的比A[index]大的数,如是找到就移动到A[index+1]处, // index移动到下一个位置,next移动到下一个位置,再找比A[index]大的数 while (next < A.length) { while (next < A.length && A[index] == A[next] ) { // 找不等于数组中最 next++; } if (next < A.length) { index++; A[index] = A[next]; next++; } } return index + 1; } private void swap(int[] a, int x, int y) { int tmp = a[x]; a[x] = a[y]; a[y] = tmp; }}
评测结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47034985】
2 0
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- 【LeetCode-面试算法经典-Java实现】【083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)】
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- Remove Duplicates from Sorted Array 删除数组中的重复元素
- LeetCode-26-Remove Duplicates from Sorted Array(消除已排序数组中的重复元素)
- LeetCode | Remove Duplicates from Sorted Array(删除有序数组的重复元素)
- 100.Remove Duplicates from Sorted Array-删除排序数组中的重复数字(容易题)
- Remove Duplicates from Sorted Array 删除中的重复元素
- 26. Remove Duplicates from Sorted Array(从排序数组中删除重复) —— Java
- Remove Duplicates from Sorted Array 有序数组删除重复元素 @LeetCode
- Leetcode刷题记——26. Remove Duplicates from Sorted Array(删除数组重复元素)
- (每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)
- leetcode:Remove Duplicates from Sorted Array去掉排序数组中重复的元素
- [leetcode 26]Remove Duplicates from Sorted Array——移除排序数组中重复元素
- leetcode 26. Remove Duplicates from Sorted Array从排序数组中移出重复元素(双指针)
- 删除有序数组中的重复元素 Remove Duplicates from Sorted Array
- leetCode 80.Remove Duplicates from Sorted Array II (删除排序数组中的重复II) 解题思路和方法
- C#__Dictionary类 __类似java Map 键值对
- 【LeetCode-面试算法经典-Java实现】【024-Swap Nodes in Pairs(成对交换单链表的结点)】
- Android中popupWindow的简单应用
- 设置圆角按钮的方法
- 【LeetCode-面试算法经典-Java实现】【025-Reverse Nodes in k-Group(单链表中k个结点一组进行反转)】
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- Objective-C之KVC(键值编码)详解
- solr5.2.1环境搭建
- 杭电ACM2032java做法
- 如何在中关村做好一门小生意——北漂18年(18)
- android布局
- struts2 之类型转化&日期
- 纪念
- 步入30岁