LeetCode Weekly Contest 43【总结】
2017-07-30 16:41
357 查看
652.Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them.
Two trees are duplicate if they have the same structure with same node values.
Example 1:
1
/ \
2 3
/ / \
4 2 4
/
4
The following are two duplicate subtrees:
2
/
4
and
4
Therefore, you need to return above trees’ root in the form of a list.
这道题是LeetCode Weekly Contest 43的第一道题。。
当时思路是有,肯定是用dfs,反正最后是没做出来,借口就不找了。。
看了下前几的大神的代码:
如下:
用了2个map。
第一个map用来记录TreeNode节点中其子树与其的结构,值为string类型
以”(+左子树string+节点值+右子树string+)”来记录
第二个map的key为string,即上面的树结构类型,值为一个vector< TreeNode*>
651 4 Keys Keyboard
Imagine you have a special keyboard with the following keys:
Key 1: (A): Prints one ‘A’ on screen.
Key 2: (Ctrl-A): Select the whole screen.
Key 3: (Ctrl-C): Copy selection to buffer.
Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed.
Now, you can only press the keyboard for N times (with the above four keys), find out the maximum numbers of ‘A’ you can print on screen.
暴力解决法……
key2-4一套素质三连,然后一直ctrl+v。所以我们是要选择在哪里找底数。
题目是给出可以使用的操作步骤有几次,然后让我们弄出越多的A。
暴力解决的思路是在处理有 i 个步骤数时,对大于(i+3)个步骤进行计算,并与当前(i+3)步骤弄出的最大数进行对比谁大。代码如下:
650 2 Keys Keyboard
Initially on a notepad only one character ‘A’ is present. You can perform two operations on this notepad for each step:
Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
Paste: You can paste the characters which are copied last time.
Given a number n. You have to get exactly n ‘A’ on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n ‘A’.
Example 1:
Input: 3
Output: 3
Explanation:
Intitally, we have one character ‘A’.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get ‘AA’.
In step 3, we use Paste operation to get ‘AAA’.
这道题挺简单的。因为他明说了一定要跟给出的n值一样所需要的最少步骤数,也就是即使有更小的步骤数但弄出的个数比n多了也不行,也就是必须得n个咯,又由于只能全部复制,这导致比如给一个7这种质数,那只能一直AAAAAAA。(复制最初的1个A,然后一直粘贴。)
大概就是求最大公约数??
代码如下:
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them.
Two trees are duplicate if they have the same structure with same node values.
Example 1:
1
/ \
2 3
/ / \
4 2 4
/
4
The following are two duplicate subtrees:
2
/
4
and
4
Therefore, you need to return above trees’ root in the form of a list.
这道题是LeetCode Weekly Contest 43的第一道题。。
当时思路是有,肯定是用dfs,反正最后是没做出来,借口就不找了。。
看了下前几的大神的代码:
如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: map<TreeNode*, string> ma; map<string, vector<TreeNode*>> res; void dfs(TreeNode* root) { if (root == NULL) return; dfs(root->left); dfs(root->right); ma[root] = "(" + ma[root->left] + to_string(root->val)+ ma[root->right] + ")"; res[ma[root]].push_back(root); } vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) { ma[NULL] = ""; dfs(root); vector<TreeNode*> ret; for (auto pr : res) { if (pr.second.size() > 1) ret.push_back(pr.second[0]); } return ret; } };
用了2个map。
第一个map用来记录TreeNode节点中其子树与其的结构,值为string类型
以”(+左子树string+节点值+右子树string+)”来记录
第二个map的key为string,即上面的树结构类型,值为一个vector< TreeNode*>
651 4 Keys Keyboard
Imagine you have a special keyboard with the following keys:
Key 1: (A): Prints one ‘A’ on screen.
Key 2: (Ctrl-A): Select the whole screen.
Key 3: (Ctrl-C): Copy selection to buffer.
Key 4: (Ctrl-V): Print buffer on screen appending it after what has already been printed.
Now, you can only press the keyboard for N times (with the above four keys), find out the maximum numbers of ‘A’ you can print on screen.
暴力解决法……
key2-4一套素质三连,然后一直ctrl+v。所以我们是要选择在哪里找底数。
题目是给出可以使用的操作步骤有几次,然后让我们弄出越多的A。
暴力解决的思路是在处理有 i 个步骤数时,对大于(i+3)个步骤进行计算,并与当前(i+3)步骤弄出的最大数进行对比谁大。代码如下:
class Solution { public: int maxA(int N) { vector<int> d(51,0); d[1]=1; for(int i=1;i<=N;++i) { d[i+1]=max(d[i+1],d[i]+1); for(int j=i+3;j<=N;++j) { d[j]=max(d[j],d[i]*(j-i-1)); } } return d ; } };
650 2 Keys Keyboard
Initially on a notepad only one character ‘A’ is present. You can perform two operations on this notepad for each step:
Copy All: You can copy all the characters present on the notepad (partial copy is not allowed).
Paste: You can paste the characters which are copied last time.
Given a number n. You have to get exactly n ‘A’ on the notepad by performing the minimum number of steps permitted. Output the minimum number of steps to get n ‘A’.
Example 1:
Input: 3
Output: 3
Explanation:
Intitally, we have one character ‘A’.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get ‘AA’.
In step 3, we use Paste operation to get ‘AAA’.
这道题挺简单的。因为他明说了一定要跟给出的n值一样所需要的最少步骤数,也就是即使有更小的步骤数但弄出的个数比n多了也不行,也就是必须得n个咯,又由于只能全部复制,这导致比如给一个7这种质数,那只能一直AAAAAAA。(复制最初的1个A,然后一直粘贴。)
大概就是求最大公约数??
代码如下:
class Solution { int zuidagongyueshu(int n) { for(int i=n/2;i>=1;--i) { if(n%i==0) { return i; } } return 1; } public: int minSteps(int n) { int x=zuidagongyueshu(n); if(x==1) { if(n==1) { return 0; } else { return n; } } else { return n/x+minSteps(x); } } };
相关文章推荐
- LeetCode 650 2 Keys Keyboard - LeetCode Weekly Contest 43
- LeetCode Weekly Contest 43解题思路
- leetcode Weekly Contest 67 -- 763. Partition Labels
- 73th LeetCode Weekly Contest Domino and Tromino Tiling
- LeetCode Weekly Contest 13-Matchsticks to Square【中】
- LeetCode 660 Remove 9 (LeetCode Weekly Contest 45)
- LeetCode Weekly Contest 23 之 536.Construct Binary Tree from String
- leetcode Weekly Contest 67 -- 765. Couples Holding Hands
- (dp)LeetCode Weekly Contest 34 -Non-negative Integers without Consecutive Ones
- LeetCode Weekly Contest 52 Longest Univalue Path(链表dfs)
- LeetCode Weekly Contest 38解题思路
- LeetCode Weekly Contest 26解题思路
- LeetCode Weekly Contest 42解题思路
- LeetCode Weekly Contest 27
- LeetCode Weekly Contest 52 Repeated String Match(string)
- leetcode做题总结,题目Multiply Strings 43
- LeetCode Weekly Contest 33解题思路
- LeetCode Weekly Contest 51
- LeetCode Weekly Contest 40(1) - Average of Levels in Binary Tree
- LeetCode Weekly Contest 41解题思路