LeetCode OJ:Subsets II(子集II)】的更多相关文章

Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Path Sum. However, since the problem is asking for all possible root-to-leaf paths, so we should use BFS but not DFS. The python code is as follows. # D…
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Word Ladder I, which uses a double-direction BFS. However, the difference is that we need to keep track of all paths during the double-direction BFS in o…
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by using Dynamic Programming. Optimal Sub-structure Assume a string S has the palindrome minimum cuts n, and S = W1 + W2 + ... + Wn where Wi is a palindro…
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Number. Suppose we have following (3m+1) numbers in the array A: x0, x1, x1, x1, ..., xm, xm, xm We are asked to find out the value of x0. However we ca…
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the word break problem, so the solution is based on the discussion in Word Break. We also use DP to solve the problem. In this solution, A[i] is not a bool…
http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algorithm to use only O(k) extra space?其实计算当前行,也只用到前一行了.再前面的没有用. class Solution { public: vector<int> getRow(int rowIndex) { // IMPORTANT: Please reset a…
给定一个可能包含重复整数的列表,返回所有可能的子集(幂集).注意事项:解决方案集不能包含重复的子集.例如,如果 nums = [1,2,2],答案为:[  [2],  [1],  [1,2,2],  [2,2],  [1,2],  []]详见:https://leetcode.com/problems/subsets-ii/description/ Java实现: class Solution { public List<List<Integer>> subsetsWithDup(…
题目 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2]输出:[ [2], [1], [1,2,2], [2,2], [1,2], []] 思路 这题的思路和40题与90题非常类似,需要对数组排序后,进行回溯算法,同时需要注意剪枝 实现 class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: result…
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *p1, *p2; //p1和p2从链表的第一个节点出发,p1每次移动一…
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps…