Leetcode题解(28)

90. Subsets II

题目

分析:代码如下

 1 class Solution {
 2 public:
 3     vector<vector<int> > subsetsWithDup(vector<int> &S) {
 4         vector<vector<int> > result;
 5         map<vector<int>, bool> m;
 6         int size = S.size();
 7         for(int i = 0; i < pow(2.0, size); i ++)
 8         {
 9             int tag = i;
10             vector<int> cur;
11             for(int j = size-1; j >= 0; j --)
12             {
13                 if(!tag)
14                     break;
15                 if(tag%2 == 1)
16                 {
17                     cur.push_back(S[j]);
18                 }
19                 tag >>= 1;
20             }
21             sort(cur.begin(), cur.end());
22             if(m.find(cur) == m.end())
23             {
24                 m[cur] = true;
25                 result.push_back(cur);
26             }
27         }
28         return result;
29     }
30 };

-----------------------------------------------------------------------------分割线--------------------------------------------------------------------

91. Decode Ways

题目

分析:这道题只需要求解码的种类数,因此一看到这道题目,就条件反射似的想到找递推公式

假设f[i]表示s[0..i]的解码数。在字符串s中,如果s[i]!=‘0‘并且s[i-1]s[i]构成的数大于等于10,小于等于26,则f[i]=f[i-1]+f[i-2]

代码如下:

 1 class Solution {
 2 public:
 3     int numDecodings(string s) {
 4         int len = s.length();
 5
 6         if(len<=1)
 7         {
 8             if(s[0]==‘0‘)
 9             return 0;
10             else
11             return len;
12         }
13         //如果第一位为0,不可能解码
14         if(s[0]==‘0‘)
15             return 0;
16
17         int first=1,second=1;
18         int temp,t;
19
20         for(int i=1;i<len;i++)
21         {
22             t=0;
23             if(s[i] != ‘0‘)
24                 t +=second;
25             temp = (s[i-1]-‘0‘)*10+s[i]-‘0‘;
26             //一开始我写成temp>=1,不能通过,再仔细看题目,如果是“01”-->temp = 1这种情况是不能解码的,
27             //因此temp>=10才可以
28             if(temp>=10 &&temp <=26)
29                 t +=first;
30             first = second;
31             second = t;
32         }
33         return second;
34     }
35 };
时间: 2024-08-10 15:10:21

Leetcode题解(28)的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] co

leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterative