LC刷题3/27 - Top FB Questions - E 543/E 415/E 349
543. Diameter of Binary Tree
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int result = 0;
public int diameterOfBinaryTree(TreeNode root) {
recursion(root);
return result;
}
public int recursion(TreeNode root) {
if(root == null) return 0;
int leftLength = recursion(root.left);
int rightLength = recursion(root.right);
result = Math.max(result, leftLength+rightLength);
return Math.max(leftLength, rightLength) + 1;
}
}
Comment
感觉自己写的代码比solution的要合理一些。solution里面又是+1再-1的edge case很麻烦。在lc的comment里面看到了Python版的自己写的代码。这样写应该也算是比较优化了。
最后有一点是抄了Python版的就是开始写了diameterOfBinaryTree return recursion(root);
后来然后就晕菜了。后来一比对发现唯一不同的就是要改成return res, 因为最后一层root返回最大的单边length意义不大了。像昨天那题,BST类型的,都是需要生成一个cumulative var,写在global里,recursion的目的只是为了反复iterate这个值。
Solution
class Solution {
int ans;
public int diameterOfBinaryTree(TreeNode root) {
ans = 1;
depth(root);
return ans - 1;
}
public int depth(TreeNode node) {
if (node == null) return 0;
int L = depth(node.left);
int R = depth(node.right);
ans = Math.max(ans, L+R+1);
return Math.max(L, R) + 1;
}
}
415. Add Strings
class Solution {
public String addStrings(String num1, String num2) {
int length = Math.max(num1.length(), num2.length());
StringBuffer buffer = new StringBuffer();
int carry = 0;
if(num1.length() != length)
num1 = multiplyZeros(num2.length()- num1.length()) + num1;
if(num2.length() != length)
num2 = multiplyZeros(num1.length()- num2.length()) + num2;
for (int i = length-1; i>=0; i--) {
int sum = ((int)num1.charAt(i)-(int)'0') + ((int)num2.charAt(i)-(int)'0');
sum = sum + carry;
buffer.append(sum % 10);
carry = sum >= 10? 1: 0;
}
if (carry == 1) buffer.append(1);
return buffer.reverse().toString();
}
public String multiplyZeros(int n) {
String res = "";
for (int i = 0; i < n; i ++) {
res = res + '0';
}
return res;
}
}
comment
No comments.
它的运行速度和时间复杂度没有达到最佳。可以优化。
Solution
public class Solution {
public String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int carry = 0;
for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
int x = i < 0 ? 0 : num1.charAt(i) - '0';
int y = j < 0 ? 0 : num2.charAt(j) - '0';
sb.append((x + y + carry) % 10);
carry = (x + y + carry) / 10;
}
return sb.reverse().toString();
}
}
349. Intersection of Two Arrays
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set1 = new HashSet<Integer>();
for (Integer n : nums1) set1.add(n);
HashSet<Integer> set2 = new HashSet<Integer>();
for (Integer n : nums2) set2.add(n);
if (set1.size() < set2.size()) return set_intersection(set1, set2);
else return set_intersection(set2, set1);
}
public int[] set_intersection(HashSet<Integer> set1, HashSet<Integer> set2) {
int [] output = new int[set1.size()];
int idx = 0;
for (Integer s : set1)
if (set2.contains(s)) output[idx++] = s;
return Arrays.copyOf(output, idx);
}
}
Comment
用set的方向对了。不过有几个low level details:
- HashSet hashset, 不能hashset.addAll(<int[] type array>)
- 要记得specify type 如HashSet
- 可以用 set1.retainAll(set2);