zoukankan      html  css  js  c++  java
  • 2016年Google面筋记录

    记录周围伙伴们面试Google遇到的问题和大致解决思路。

    【算法】

    ARRAY | 数组

    1.给定一个有序数组,和两个值表示区间的开始和结束,找出数组中不在这个区间里的元素

        比如[1,2,4] 1,10
        输出[3,5-10]
    2.给两个集合,找出两个集合中的不一样的部分 
         [1,2,2,2], [1,2,3]
        输出[2,2], [3]

    3.设计电信运营商选号系统

    TREE | 树

    1. Binary tree maximum path sum.

    问题描述

    Given a binary tree, find the maximum path sum.

    The path may start and end at any node in the tree.

    For example:
    Given the below binary tree,

           1
          / 
         2   3
    

    Return 6.

    解决思路

    递归

    1. 得到左子树的最大值,Math.max(root.left, 0),0代表不考虑将左子树的最大值加入;

    2. 得到右子树的最大值,Math.max(root.right, 0),0代表不考虑将右子树的最大值加入;

    3. 以root为根节点,树的最大值为root.val + Math.max(leftMax, rightMax).

    边界条件为root为null时,返回0.

    程序

    public class Solution {
        private int max = Integer.MIN_VALUE;
        
        public int maxPathSum(TreeNode root) {
            if (root == null) {
                return 0;
            }
            max = root.val;
            getSumMaxRec(root);
            return max;
        }
        
        private int getSumMaxRec(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int leftMax = Math.max(getSumMaxRec(root.left), 0);
            int rightMax = Math.max(getSumMaxRec(root.right), 0);
            max = Math.max(max, root.val + leftMax + rightMax); // update
            return root.val + Math.max(leftMax, rightMax);
        }
    }
    

    2. Interleaving String

    问题描述

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.

    For example,
    Given:
    s1 = "aabcc",
    s2 = "dbbca",

    When s3 = "aadbbcbcac", return true.
    When s3 = "aadbbbaccc", return false.

    解决思路

    动态规划

    递推式为dp[i+1][j+1]代表s3[0, i + j + 1]能否由s1[0, i]和s2[0, j]交替组成。

    dp[i+1][j+1] = (dp[i][j+1] && s3[i+j+1] == s1[i+1]) || (dp[i+1][j] && s3[i+j+1] == s2[j+1]).

    程序

    public class Solution {
        public boolean isInterleave(String s1, String s2, String s3) {
            if (s1 == null || s2 == null || s3 == null) {
                return false;
            }
            int len1 = s1.length();
            int len2 = s2.length();
            int len = s3.length();
            if (len != len1 + len2) {
                return false;
            }
            
            boolean[][] dp = new boolean[len1 + 1][len2 + 1];
            // init
            dp[0][0] = true;
            for (int i = 0; i < len1; i++) {
                dp[i + 1][0] = (s3.charAt(i) == s1.charAt(i) && dp[i][0]);
            }
            for (int i = 0; i < len2; i++) {
                dp[0][i + 1] = (s3.charAt(i) == s2.charAt(i) && dp[0][i]);
            }
            
            for (int i = 0; i < len1; i++) {
                for (int j = 0; j < len2; j++) {
                    dp[i + 1][j + 1] = 
                    (dp[i][j + 1] && s3.charAt(i + j + 1) == s1.charAt(i))
                    ||
                    (dp[i + 1][j] && s3.charAt(i + j + 1) == s2.charAt(j));
                }
            }
            
            return dp[len1][len2];
        }
    }
    
  • 相关阅读:
    [ PyQt入门教程 ] Qt Designer工具的使用
    [ PyQt入门教程 ] PyQt5开发环境搭建和配置
    Notepad++提升工作效率小技巧
    思考:测试人员如何快速成长
    Linux /tmp目录下执行脚本失败提示Permission denied
    使用Quartz实现定时任务
    算法篇(前序)——Java的集合
    深入理解 JVM(上)
    Linux集锦
    秒杀系统实现高并发的优化
  • 原文地址:https://www.cnblogs.com/harrygogo/p/4729095.html
Copyright © 2011-2022 走看看