2016年Google面筋记录
2015-08-14 09:47
274 查看
记录周围伙伴们面试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,
Return
解决思路
递归
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.
程序
2. Interleaving String
问题描述
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 =
s2 =
When s3 =
When s3 =
解决思路
动态规划
递推式为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]).
程序
【算法】
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]; } }
相关文章推荐
- 去大公司还是去小公司工作——要进大公司的核心部门(提升视野,锻炼技能),远离没真本事的小公司,要自我驱动 good
- Django-模型
- web框架django安装
- VIEWGOOD(远古)多媒体系统在电信行业的应用
- Go 1.6 GC improvements 转自google doc
- Django 学习笔记(二) 《models》
- 【转】The Google File System 中文版
- The last packet sent successfully to the server was 0 milliseconds ago, The driver has not received
- OC中protocol、category和继承的区别
- 简单测试一下go(golang) 和libtask 协程的切换效率
- django book学习笔记――模型高级进阶
- POJ 2262 Goldbach's Conjecture(哥德巴赫猜想)
- Go 并发
- HUNAN 11562 The Triangle Division of the Convex Polygon(大卡特兰数)
- 关于lingo的例子
- django book学习笔记――模板高级进阶
- The last packet sent successfully to the server was 0 milliseconds ago.
- django 显示静态文件的几种方式
- GO语言UDP小笔记
- HDU 1286 找新朋友 (欧拉函数)