有点秀
有ios 安卓 C++ Java C#?
第一题 二叉树最近祖先
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) { return root; } TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if (left != null && right != null) { return root; } if (left != null) { return left; } return right; }
第二题 统计字符数量
public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine(); int count = 1; StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { while (i < str.length() - 1 && str.charAt(i + 1) == str.charAt(i)) { count++; i++; } sb.append(str.charAt(i)).append(count); count = 1; } System.out.println(sb.toString()); } }
全部评论
(10) 回帖