删除排序数组或者链表当中的重复项 在刷leetcode当中发现了一个删除重复项的问题,总结一下
删除排序数组中的重复项 题目描述 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用O(1) 额外空间的条件下完成。
示例 1:
1 2 3 4 5 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 你不需要考虑数组中超出新长度后面的元素。
示例 2:
1 2 3 4 5 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。 你不需要考虑数组中超出新长度后面的元素。
说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以「引用」 方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
你可以想象内部操作如下:
1 2 3 4 5 6 7 8 // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝 int len = removeDuplicates(nums); // 在函数里修改输入数组对于调用者是可见的。 // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。 for (int i = 0; i < len; i++) { print(nums[i]); }
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码 这种题目一看就是使用双指针的方法进行编写,但是其实我在想的时候还是没有那么直观的想出答案,不得不说还是有些巧妙的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public int removeDuplicates (int [] nums) { if (nums==null ||nums.length==0 ) return 0 ; int last = 0 ; int temp = nums[0 ]; for (int i=1 ;i<nums.length;i++){ if (temp!=nums[i]){ nums[last+1 ] = nums[i]; last++; temp =nums[i]; } } return last+1 ; } }
删除排序数组中的重复项 题目描述 给定一个排序数组,你需要在原地 删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组 并在使用 O(1) 额外空间的条件下完成。
示例 1:
1 2 3 4 5 给定 nums = [1,1,1,2,2,3], 函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。 你不需要考虑数组中超出新长度后面的元素。
示例 2:
1 2 3 4 5 给定 nums = [0,0,1,1,1,1,2,3,3], 函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。 你不需要考虑数组中超出新长度后面的元素。
说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以“引用” 方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
你可以想象内部操作如下:
1 2 3 4 5 6 7 8 // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝 int len = removeDuplicates(nums); // 在函数里修改输入数组对于调用者是可见的。 // 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。 for (int i = 0; i < len; i++) { print(nums[i]); }
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码 代码思路和上面一题其实类似,但是有一个容忍度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Solution { public int removeDuplicates (int [] nums) { int j = 1 , count = 1 ; for (int i = 1 ; i < nums.length; i++) { if (nums[i] == nums[i - 1 ]) { count++; } else { count = 1 ; } if (count <= 2 ) { nums[j++] = nums[i]; } } return j; } }
参考链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-xiang-i-7/
方法还是挺妙的
删除排序链表当中的重复元素 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
示例 2:
1 2 输入: 1->1->2->3->3 输出: 1->2->3
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码 这个比较简单,我是使用了两个指针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Solution { public ListNode deleteDuplicates (ListNode head) { if (head==null )return null ; if (head.next==null )return head; ListNode curent = head; ListNode next = head.next; while (next!=null ) { if (curent.val==next.val) { while (next!=null && curent.val==next.val) { next = next.next; } curent.next = next; curent = next; if (next!=null ) next =next.next; }else { curent = next; next = next.next; } } return head; } }
官方答案比我的要简洁、优雅一些,也贴出来
1 2 3 4 5 6 7 8 9 10 11 public ListNode deleteDuplicates (ListNode head) { ListNode current = head; while (current != null && current.next != null ) { if (current.next.val == current.val) { current.next = current.next.next; } else { current = current.next; } } return head; }
删除排序链表当中的重复元素II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
1 2 输入: 1->2->3->3->4->4->5 输出: 1->2->5
示例 2:
1 2 输入: 1->1->1->2->3 输出: 2->3
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码 我的思路是使用三个指针进行删除,因为这个删除是必须删除掉所有重复的元素,和上面还是有一些不相同,所以规定了一个last指针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 class Solution { public ListNode deleteDuplicates (ListNode head) { if (head==null )return null ; if (head.next==null )return head; ListNode last = null ; ListNode curent = head; ListNode next = head.next; ListNode newHead = null ; while (next!=null ) { if (curent.val==next.val) { while (next!=null && curent.val==next.val) { curent = curent.next; next = next.next; } if (last!=null ) { last.next = next; } curent.next = null ; curent = next; if (next!=null ) next =next.next; if (next==null &&newHead==null ) { newHead = curent; } }else { last = curent; curent = next; next = next.next; if (newHead==null ) { newHead = last; } } } return newHead; } }