LeetCode刷题系列(六)Remove Duplicates
2016-06-14 18:23
253 查看
本篇主要是从Array和List中remove duplicates,其中Array和List有已经排好序的和未排序的。
其中if(j-i < 2)使得程序允许出现两次重复,如果两次以上,则j直接掉过,然后i = j,直接把两次以上重复的元素全部覆盖掉。
这里注意一下边界检查,同时此判断语句head.next != null && head.next.next != null是由于如果head.next.next == null了,那么head.next一定不是重复元素(也是由于if中的while循环导致的)。
Remove Duplicates from Sorted Array
题目为删除有序数组中重复的元素。有序数组中的重复元素一定是与之前元素相等,我们很容易能判别出重复元素,但是我们删除元素时,数组后面的元素需要向前移动,这里我们使用了两个局部变量,一个i记录从头要访问的元素,一个记录需要保留在数组中元素,这样所有的元素相当于只进行了一次移动,避免增加时间复杂度。代码:public class Solution { public int removeDuplicates(int[] A) { if (A == null || A.length == 0) { return 0; } int size = 0; for (int i = 0; i < A.length; i++) { if (A[i] != A[size]) { A[++size] = A[i]; } } return size + 1; } }
Remove Duplicates from Sorted Array II
此题目与Remove Duplicates from Sorted Array I的区别在于这道题目允许存在两次重复,即如果某个元素出现两次,则不进行删除。代码:public int removeDuplicates(int[] nums) { if(nums == null) return 0; int cur = 0; int i ,j; for(i = 0; i < nums.length;){ int now = nums[i]; for( j = i; j < nums.length; j++){ if(nums[j] != now) break; if(j-i < 2) nums[cur++] = now; } i = j; } return cur; }
其中if(j-i < 2)使得程序允许出现两次重复,如果两次以上,则j直接掉过,然后i = j,直接把两次以上重复的元素全部覆盖掉。
Remove Duplicates from Sorted List
这道题目为删除有序链表中的重复元素。虽然代码不同,但是思路与Remove Duplicates from Sorted Array很相似。比较容易检测到重复元素,然后将重复元素的前一个元素的next指向重复元素之后的元素删除掉重复元素即可。同时,注意一下边界检查,代码:public ListNode deleteDuplicates(ListNode head) { if (head == null) { return null; } ListNode node = head; while (node.next != null) { if (node.val == node.next.val) { node.next = node.next.next; } else { node = node.next; } } return head; }
Remove Duplicates from Sorted List II
此题目与Remove Duplicates from Sorted List I的区别在与将所有重复的元素全部删掉,不保留任何一个副本。如之前blog的介绍,当头元素也有可能被操作时(这里是被删除),我们需要添加dummyNode作为Head,代码:public ListNode deleteDuplicates(ListNode head) { if(head == null || head.next == null) return head; ListNode dummy = new ListNode(0); dummy.next = head; head = dummy; while (head.next != null && head.next.next != null) { if (head.next.val == head.next.next.val) { int val = head.next.val; while (head.next != null && head.next.val == val) { head.next = head.next.next; } } else { head = head.next; } } return dummy.next; }
这里注意一下边界检查,同时此判断语句head.next != null && head.next.next != null是由于如果head.next.next == null了,那么head.next一定不是重复元素(也是由于if中的while循环导致的)。
Remove Duplicates from Unsorted List
最后一道题目是在非有序链表中删除重复元素,类似这样的题目,我们就需要花费力气在判断一个元素是否是重复元素上了,因此我们使用了一个HashSet,将所有元素扔进set中,然后调用其contains函数即可知道该元素是否为重复元素,我们也可以使用HashMap,不过需要使用其多个函数,而Set本身存在即是为了判断有哪些元素的,因而类似的问题我们均可使用HashSet(Set中较为常用的一种,时间效率较高)。另外,确定好重复元素之后将其删除与Remove Duplicates from Sorted List I思路类似,这里就不给出代码了。相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解