【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
来源:互联网 发布:sql判断字段是否为空 编辑:程序博客网 时间:2024/05/20 11:46
leetcode上有四道关于移除有序序列中重复数字的题目,其中两道为数组结构,两道为链表结构,分别为:
(1)Remove Duplicates from sorted array I:移除一个有序数组中的重复数字,并且返回新数组的大小。
(2)Remove Duplicates from sorted array II:移除一个有序数组中的重复数字,并且返回新数组的大小,和上道题目不同的是每个数字可以最多重复两次出现。
(3)Remove Duplicates from sorted list I:移除一个有序链表中的重复数字,并且返回首节点。
(4)Remove Duplicates from sorted list II:移除一个有序链表中有重复数字出现的节点,并且返回首节点。
以下针对这四道题目分别进行分析。
题目一: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]
.
Java AC代码如下:
public class Solution { public int removeDuplicates(int[] A) { if(A==null || A.length==0){ return 0; } int result = 1; int temp = A[0]; for(int i=1;i<A.length;i++){ if(temp<A[i]){A[result++] = A[i];temp = A[i]; } } return result; }}
题目二:Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3]
,
Your function should return length = 5
, and A is now [1,1,2,2,3]
.
解析:移除一个有序数组中的重复数字,并且返回新数组的大小,和上道题目不同的是每个数字可以最多重复两次出现。因此,可以添加一个布尔变量ifRepeated来记录是否出现数字重复。
Java AC代码:
public class Solution {public int removeDuplicates(int[] A) {if (A == null || A.length == 0) {return 0;}boolean ifRepeated = false;int curPos = 0;for (int i = 1; i < A.length; i++) {if (A[i] != A[curPos]) {ifRepeated = false;curPos++;A[curPos] = A[i];} else if (!ifRepeated) {ifRepeated = true;curPos++;A[curPos] = A[i];}}return curPos + 1;}}
题目三:Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
解析:移除一个有序链表中的重复数字,并且返回首节点。该题的主要思路是用两个节点pre,cur分别记录已完成链表的末端节点和当前遍历到的节点。注意如果当前节点cur是个重复节点,要把pre.next置为null。
Java AC代码如下:
public class Solution {public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) {return head;}ListNode pre = head;ListNode cur = pre.next;while (cur != null) {if (cur.val != pre.val) {pre.next = cur;pre = cur;}else{pre.next = null;}cur = cur.next;}return head;}}题目四:Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
注意当pre这个节点不能添加到结果链表中时,要用point.next = null 将当前节点断开。
Java AC代码:
public class Solution { public ListNode deleteDuplicates(ListNode head) {if (head == null || head.next == null) {return head;}ListNode pre = head;ListNode cur = pre.next;ListNode point = new ListNode(-1);ListNode dummy = point;boolean ifDuplicated = false;while (cur != null) {if (cur.val != pre.val) {if (!ifDuplicated) {point.next = pre;point = point.next;} else {point.next = null;}ifDuplicated = false;} else {point.next = null;ifDuplicated = true;}pre = pre.next;cur = cur.next;}if (!ifDuplicated) {point.next = pre;}return dummy.next;}}
- 【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
- LeetCode Remove Duplicates from Sorted Array 移除有序数列的重复元素
- leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array
- Leetcode 80. Remove Duplicates from Sorted Array II 重复移除 解题报告
- Leetcode #26. Remove Duplicates from Sorted Array 移除重复数字 解题报告
- Remove Duplicates from Sorted Array II 从有序数组里移除重复出现元素,最多重复出现2次 @LeetCode
- Leetcode 82. Remove Duplicates from Sorted List II 移除重复 链表版-2 解题报告
- Remove Duplicates from Sorted Array--从有序数组中移除重复元素
- 26. Remove Duplicates from Sorted Array(移除有序数组中的重复元素)
- (有序数组中移除重复元素)Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted List II 链表移除重复值@LeetCode
- Leetcode 80. Remove Duplicates from Sorted Array II(从已排序数组中移除重复元素)
- Leetcode刷题记—— Remove Duplicates from Sorted Array II(已排序数组移除重复元素2)
- Remove Duplicates from Sorted Array II 移除重复的元素(重)
- LeetCode:Remove Duplicates from Sorted Array I&&II
- LeetCode题解:Remove Duplicates from Sorted Array I and II
- 【LeetCode】Remove Duplicates from Sorted Array I && II
- LeetCode Solutions : Remove Duplicates from Sorted Array I & II
- ReportStudio入门教程(九十) - 在交叉表表头显示占比
- 专访魏子钧:喧嚣后的HTML5和JavaScript将长存
- QML类型说明-Camera
- AVS 和 H.264 核心技术对比总结
- JavaScript页面节点的函数
- 【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
- Android显示系统分析
- 神兽保佑 代码无bug
- 数据结构学习笔记(四)---遍历二叉树
- leetcode:maxprofit
- 如何阅读Java源码 阅读java的真实体会
- android发送邮件
- QML类型说明-CameraCapture
- 黑马程序员_Java 高新技术(6)