#leetcode

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
Example 1:
Given [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”]
Return 16
The two words can be “abcw”, “xtfn”.
Example 2:
Given [“a”, “ab”, “abc”, “d”, “cd”, “bcd”, “abcd”]
Return 4
The two words can be “ab”, “cd”.
Example 3:
Given [“a”, “aa”, “aaa”, “aaaa”]
Return 0
No such pair of words.

阅读更多

最近有个新闻特别火,Mac上brew工具的作者去面试google,结果跪了,正是跪在“反转二叉树”的问题上,谷歌对该大牛的评论是:

Google:我们90%的工程师都用你写的软件,但你没法在白板上翻转二叉树,所以滚蛋吧。

英文原文如下:

Google:90% of our engineers use the software you wrote(HomeBrew), but you can’t invert a binary tree on a whiteboard, so fuck off.

哈哈!LeetCode很快推出了一道OJ题目invert binary tree, 且把难度定位easy,今天瞅了一眼,写了递归版本的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class InvertBinaryTree {


static class Solution {

public void _invertTree(TreeNode root) {
if (root == null)
return;
if (root.left == null && root.right == null)
return;
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
_invertTree(root.left);
_invertTree(root.right);
}

public TreeNode invertTree(TreeNode root) {
if (root == null)
return null;
_invertTree(root);
return root;
}
}


public static boolean case01() {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(2);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);

root.right = new TreeNode(7);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(9);

TreeNode rest = new Solution().invertTree(root);

System.out.println(rest.val);
System.out.println(rest.left.val + "\t" +rest.right.val);
System.out.println(rest.left.left.val + "\t" + rest.left.right.val + "\t" + rest.right.left.val + "\t" + rest.right.right.val);

return true;
}

public static void main(String[] args) {
case01();
}
}

明天有时间,再写一个非递归版本的。

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×