Menu Sidebar
Menu

Archive: November 8, 2015

Binary Tree Maximum Path Sum

给一个二叉树, 问你path sum最大是多少, 返回最大值 遍历所有node, 如果当前的path sum小于0, 那么是对sum没帮助的, 只关心大于0的. 然后找到最大就行了 int max = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { if(root == null) return 0; helper(root); return max; } public int helper(TreeNode root) { if (root == null) return 0; int left = Math.max(helper(root.left), 0); int right = Math.max(helper(root.right), 0); max = Math.max(max, root.val + left […]

Group Shifted Strings

给一组string, 定义一下shift “abc” – >; “bcd” ->; … ->; “xyz” 让你找这组string中,能互相shift的几组. 可以看出, 当一个string的字符之间的距离决定了shift后的string. 所以对字符之间的距离进行记录, 当做key, 就可以找到. 这里先取一下string首字母当做offset, 然后算距离. 最后记录在一个key(string)中. public List<List<String>> groupStrings(String[] strings) { List<List<String>> res = new ArrayList<List<String>>(); HashMap<String, List<String>> maps = new HashMap<>(); for(int i = 0 ; i < strings.length; i++) { String key = “”; int offset = strings[i].charAt(0) – ‘a’; […]

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

November 2015
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30