16题 Permutations II

独特排列:解决重复数字的全排列问题
此篇博客介绍了如何使用Java编程语言解决给定数组中重复数字的全排列问题,通过递归和回溯的方法找到所有不重复的组合。Solution类中的permuteUnique方法首先对数组进行排序,然后利用helper方法进行深度优先搜索,确保每一种排列都是唯一的。

Permutations II

Description
Given a list of numbers with duplicate numbers in it. Find all unique permutations of it.

public class Solution {
    /*
     * @param :  A list of integers
     * @return: A list of unique permutations
     */
    public List<List<Integer>> permuteUnique(int[] nums) {
        // write your code here
        List<List<Integer>> results = new ArrayList<>() ;
        if(nums == null){
            return null ;
        }
        if(nums.length == 0){
            results.add(new ArrayList<Integer>()) ;
            return results ;
        }
        Arrays.sort(nums) ;
        List<Integer> subset = new ArrayList<Integer>() ; 
        helper(nums , new boolean[nums.length] , subset ,results );
        return results ;
    }
    public void helper(int[] nums , boolean[] used , List<Integer>subset ,List<List<Integer>> results ){
        if(subset.size() == nums.length){
            results.add(new ArrayList<Integer>(subset)) ;
        }
        for(int i = 0 ; i < nums.length ; i++){
            if(used[i]){
                continue ;
            }
            if(i > 0 && nums[i] == nums[i-1] && !used[i-1]){
                continue ;
            }
            subset.add(nums[i]) ;
            used[i] = true ;
            helper(nums , used , subset , results) ;
            used[i] = false ;
            subset.remove(subset.size()-1) ;
        }
    }
};
### LeetCode Hot 100 目列表 以下是基于常见整理的 LeetCode Hot 100 的目列表,这些目涵盖了多种数据结构和算法的核心知识点: #### 数组与字符串 1. **两数之和 (Two Sum)** [^1] 2. **三数之和 (3Sum)** [^1] 3. **无重复字符的最长子串 (Longest Substring Without Repeating Characters)** 4. **寻找两个正序数组的中位数 (Median of Two Sorted Arrays)** 5. **最长回文子串 (Longest Palindromic Substring)** [^1] 6. **Z 字形变换 (Zigzag Conversion)** 7. **整数反转 (Reverse Integer)** #### 动态规划 8. **爬楼梯 (Climbing Stairs)** 9. **打家劫舍 (House Robber)** 10. **最大子数组和 (Maximum Subarray)** 11. **不同路径 (Unique Paths)** [^1] 12. **编辑距离 (Edit Distance)** #### 堆栈与队列 13. **有效的括号 (Valid Parentheses)** 14. **最小栈 (Min Stack)** 15. **滑动窗口最大值 (Sliding Window Maximum)** [^1] #### 双指针技术 16. **盛最多水的容器 (Container With Most Water)** 17. **接雨水 (Trapping Rain Water)** #### 排序与查找 18. **颜色分类 (Sort Colors)** 19. **搜索旋转排序数组 (Search in Rotated Sorted Array)** 20. **合并区间 (Merge Intervals)** #### 图论 21. **克隆图 (Clone Graph)** 22. **岛屿数量 (Number of Islands)** [^1] #### 树与二叉树 23. **验证二叉搜索树 (Validate Binary Search Tree)** 24. **二叉树的最大深度 (Maximum Depth of Binary Tree)** 25. **平衡二叉树 (Balanced Binary Tree)** 26. **二叉树层序遍历 (Binary Tree Level Order Traversal)** [^1] 27. **从前序与中序遍历序列构造二叉树 (Construct Binary Tree from Preorder and Inorder Traversal)** #### 链表 28. **删除链表中的倒数第 N 个节点 (Remove Nth Node From End of List)** [^1] 29. **环形链表 II (Linked List Cycle II)** [^2] 30. **相交链表 (Intersection of Two Linked Lists)** #### 贪心算法 31. **跳跃游戏 (Jump Game)** 32. **分割等和子集 (Partition Equal Subset Sum)** #### 并查集 33. **冗余连接 (Redundant Connection)** #### 其他经典问 34. **LRU 缓存机制 (LRU Cache)** 35. **全排列 (Permutations)** 36. **组合总和 (Combination Sum)** 37. **单词拆分 (Word Break)** [^1] 以上为常见的 LeetCode Hot 100 目列表,具体目可能会因社区热度变化而有所调整。 ```python # 示例代码:实现简单的 LRU 缓存功能 class LRUCache: def __init__(self, capacity: int): self.cache = {} self.capacity = capacity self.order = [] def get(self, key: int) -> int: if key in self.cache: self.order.remove(key) self.order.append(key) return self.cache[key] return -1 def put(self, key: int, value: int) -> None: if key in self.cache: self.order.remove(key) elif len(self.cache) >= self.capacity: evict_key = self.order.pop(0) del self.cache[evict_key] self.cache[key] = value self.order.append(key) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值