第三题用的递归写的。已AC。
public class NCLongestSingleColorPath {
public int max=0;
public int findPath(TreeNode root) {
// write code here
recur(root);
return max;
}
public int[] recur(TreeNode root){
int[] res = new int[]{0,0};
if(root==null)
return res;
int[] left = recur(root.left);
int[] right = recur(root.right);
res[root.val]=Math.max(left[root.val],right[root.val])+1;
int temp=left[root.val]+right[root.val]+1;
if(temp>max) max = temp;
return res;
}
}
import java.util.*;
public class Transform {
public String trans(String s, int n) {
// write code here
s = s.substring(0,n);
String[] temp = s.split(" +");
StringBuilder res = new StringBuilder();
char tempChar;
for (int i = temp.length-1; i >= 0 ;i--){
for (int j = 0; j < temp[i].length() ; j++){
tempChar = temp[i].charAt(j);
if (Character.isLowerCase(tempChar))
res.append(Character.toUpperCase(tempChar));
else if (Character.isUpperCase(tempChar))
res.append(Character.toLowerCase(tempChar));
}
if (i!=0)
res.append(" ");
}
return res.toString();
}
}
求问第一题代码哪里错了。。一直不能AC。。第三题也是_(:зゝ∠)_