【LeetCode】数组系列(去重)
来源:互联网 发布:python自然语言分析 编辑:程序博客网 时间:2024/04/28 17:47
26. Remove Duplicates from Sorted Array
题目:移除排序数组重复元素,使前n个数不重复
思路:设置一个指针k,标记目前不重复的最后一位,从前往后遍历,如果严格递增,就将该值覆盖k后一位,否则不进行操作,非常简单。好像是滴滴面到的题。
public class Solution { public int removeDuplicates(int[] nums) { int k = 0; for(int i = 1; i < nums.length; i++){ if(nums[i] > nums[k]){ nums[++k] = nums[i]; } } return k+1; }}
题目:移除数组中所有与给定值相同的数。
思路:与26题方法完全一致。
public class Solution { public int removeElement(int[] nums, int val) { int k = 0; for(int i = 0; i < nums.length; i++){ if(nums[i] != val){ nums[k++] = nums[i]; } } return k; }}
题目:将数组中所有零元素移到最后。
思路:与27题完全一致,只是将最后的数全部置为零。
public class Solution { public void moveZeroes(int[] nums) { int k = 0; for(int i = 0; i < nums.length; i++){ if(nums[i] != 0){ nums[k++] = nums[i]; } } for(int i = k; i < nums.length; i++){ nums[i] = 0; } }}
80. Remove Duplicates from Sorted Array II
题目:允许每个元素重复一次
思路:方法一样,从后往前进行比较
public class Solution { public int removeDuplicates(int[] nums) { if(nums.length < 3) return nums.length; int k = nums.length-2; for(int i = nums.length-3; i >= 0; i--){ if(nums[i] != nums[k+1]){ int temp = nums[i]; nums[i] = nums[k-1]; nums[k-1] = temp; k--; } } for(int i = k; i < nums.length; i++){ nums[i-k] = nums[i]; } return nums.length-k; }}
其实不用从后往前,从前往后也是一样的,这样少一次遍历。
public int removeDuplicates(int[] nums) { int i = 0; for (int n : nums) if (i < 2 || n > nums[i - 2]) nums[i++] = n; return i;}
83. Remove Duplicates from Sorted List
题目:移除链表中重复的元素
思路:非递归
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head == null || head.next == null) return head; ListNode p = head; while(p.next != null){ if(p.next.val == p.val){ p.next = p.next.next; } else{ p = p.next; } } return head; }}
递归
public ListNode deleteDuplicates(ListNode head) { if(head == null || head.next == null)return head; head.next = deleteDuplicates(head.next); return head.val == head.next.val ? head.next : head;}
82. Remove Duplicates from Sorted List II
题目:移除链表中所有出现重复的元素。
思路:当第一个节点可能被滤掉的时候使用头结点更方便。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head == null || head.next == null) return head; ListNode p = new ListNode(0); p.next = head; ListNode q = p; while(p.next.next != null){ if(p.next.val == p.next.next.val){ int x = p.next.val; while(p.next.next != null && p.next.next.val == x) p.next = p.next.next; p.next = p.next.next; } else{ p = p.next; } if(p == null || p.next == null) break; } return q.next; }}
递归
public ListNode deleteDuplicates(ListNode head) { if (head == null) return null; if (head.next != null && head.val == head.next.val) { while (head.next != null && head.val == head.next.val) { head = head.next; } return deleteDuplicates(head.next); } else { head.next = deleteDuplicates(head.next); } return head;}
0 0
- 【LeetCode】数组系列(去重)
- javascript系列之数组去重(传统和es6)
- 数组操作系列之循环去重
- 【leetcode】已排序数组去重
- 【leetcode】已排序数组去重 II
- 数组去重(方法)
- 数组去数组重
- 数组去重,json数组去重
- [C++]LeetCode 26: Remove Duplicates from Sorted Array(有序数组去重)
- ※ Leetcode - Array -Remove Duplicates from Sorted Array(就地有序数组去重)
- LeetCode @ Remove Duplicates from Sorted Array 数组去重
- LeetCode @ Remove Duplicates from Sorted ArrayII 数组去重II
- leetcode 27. Remove Element-数组去重|双指针
- 数组去重(c语言实现)
- 数组去重(数字和字符串)
- javascript 编程(2)【数组去重】
- 数组去重
- 数组去重
- P2234 HNOI2002 营业额统计——splay
- IToolbarMenu.AddItem()函数中的参数
- ROS模拟
- jdbc工具类-生成domain实体
- 数据库连接池之C3P0
- 【LeetCode】数组系列(去重)
- PWM 脉冲宽度调制
- 欢迎使用CSDN-markdown编辑器
- OpenStack Ocata 安装(四)安装计算(Nova)服务
- VCL多重继承
- hadoop2.7.3 eclipse环境搭建
- 文本处理命令
- 数组一切操作
- Unity基础.003MonoBehavior常用事件函数