![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Google面试
Seth_Lin
这个作者很懒,什么都没留下…
展开
-
Rearrange a string so that all same characters become d distance away minDistance priority queue
Given a string and a positive integer d. Some characters may be repeated in the given string. Rearrange characters of the given string such that the same characters become d distance away from each oth转载 2015-09-15 12:10:42 · 650 阅读 · 0 评论 -
compute exact number of triples of distinct elements in large array
Given an array of N=10^6 int32 and an int32 X, compute the exact number of triples (a, b, c) of distinct elements of the array so that a + b + c <= X public int threeSumSmaller(int[] nums, int targe转载 2015-09-16 10:10:30 · 441 阅读 · 0 评论 -
Data Structure for HTML DOM with compare text inside
1. 设计HTML DOM数据结构信息是<html><body><p>Hello</p></body></html>或者<html><body><p><b>H</b>ello</p></body></html>【提示是类似tree】2. 写一个方法比较两个HTML数据中存粹文本是否一样. 鍥磋鎴戜滑@1point 3 acres上面两个例子都是Hello,所以此方法返回truepub转载 2015-09-16 10:20:35 · 309 阅读 · 0 评论 -
Find a sorted subsequence of size 3 three in linear time
Given an array of n integers, find the 3 elements such that a[i] < a[j] < a[k] and i < j < k in 0(n) time. If there are multiple such triplets, then print any one of them.Examples:Input: arr[] = {12,转载 2015-09-16 10:53:29 · 341 阅读 · 0 评论 -
Reservoir Sampling (a random element from indefinite length stream with same probability)
Reservoir Sampling Reservoir sampling is a family of randomized algorithms for randomly choosing k samples from a list of n items, where n is either a very large or unknown number. Typically n is larg转载 2015-09-17 00:07:01 · 537 阅读 · 0 评论 -
wiggle sort Given a list of integers, sort them so the output is s1 <= s2 >=s3 <=s4 ... sN.
每个元素和后一个元素比较看是不是希望的次序,如果不是的话互换就好public void wiggle_sort(int[] arr) { int n = arr.length; if(n <= 1) return; boolean inc = true; int prev = arr[0]; for(int i=1; i<n; i++) {转载 2015-09-17 00:18:54 · 463 阅读 · 0 评论 -
serialize a list of string and deserialize it
import java.util.*;public class combineStrings{ public static void main(String[] args) { String[] arr = {"abc%cde", "a#aa", "haha"}; for(String s : arr) {转载 2015-09-17 00:46:41 · 317 阅读 · 0 评论 -
find maximized count of 0 on left and 1 on right in binary array
给定一个 array,只包含 0,1, 找到一个分割位置,使左侧 0 出现的个数和右侧 1 出现的个数之和最 大化brute force, time: O(n^2), space: O(1) follow up, can we solve in O(n): DP, two temp array-google 1point3acres follow up, can we use O(1) spac转载 2015-09-17 00:48:19 · 310 阅读 · 0 评论 -
missing ranges of sorted integer array
大自然的搬运工:http://www.danielbit.com/blog/puzzle/leetcode/leetcode-missing-ranges Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges. For example, g转载 2015-09-17 00:53:29 · 313 阅读 · 0 评论 -
perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
用动态规划Dynamic Programming来做,我们建立一个长度为n+1的一维dp数组,将第一个值初始化为0,其余值都初始化为INT_MAX, i从0循环到n,j从1循环到i+j*j <= n的位置,然后每次更新dp[i+j*j]的值,动态更新dp数组,其中dp[i]表示正整数i能少能由多个完全平方数组成,那么我们求n,就是返回dp[n]即可,也就是dp数组的最后一个数字,参见代码如下:int转载 2015-09-17 02:01:21 · 538 阅读 · 0 评论 -
calculate sorted y of function y = ax^2 + bx + c given sorted x array
给定一个一元二次方程式y = ax^2 + bx + c, 一个sorted array X, 将X中所有元素代入方程中,返回sorted的Ysolution: 看a< or > 0,,搞出两边的y们,然后mergepublic class Ax2 { public static void main(String[] args) { } // x is sorted array转载 2015-09-16 15:42:31 · 583 阅读 · 0 评论 -
Randow Id generator with array of probability
给一组id和表示每个id出现概率的数组,概率之和为1.要求随机生成id,使得随机出的id满足之前的概率数组。**如果id很多,调用这个随机生成方法的次数也很多,怎么优化使用prefix sum,使用binary searchclass RandomID { public static void main(String[] args) { int[] id = { 1, 2,转载 2015-09-16 15:29:42 · 471 阅读 · 0 评论 -
Longest Consecutive Sequence
思路:既然要O(n)算法,排序显然不行,所以自然想到用hash table。将序列中的所有数存到一个unordered_set中。对于序列里任意一个数A[i],我们可以通过set马上能知道A[i]+1和A[i]-1是否也在序列中。如果在,继续找A[i]+2和A[i]-2,以此类推,直到将整个连续序列找到。为了避免在扫描到A[i]-1时再次重复搜索该序列,在从每次搜索的同时将搜索到的数从set中删除。原创 2015-09-15 12:59:45 · 225 阅读 · 0 评论 -
word abbreviation
一个string只保留首尾字母, 中间字母数用数字表示, 比如 “abcde” = “a3e”, “ab” = “ab”, “abc” = “a1c” . 给一组string, 输出相同abbreviation的所有string, 但重复的string不算.abv_string = StringBuilder.append(charAt[0]), StringBuilder.append(str.原创 2015-09-15 23:41:41 · 405 阅读 · 0 评论 -
fast Update, sum operation of 2D matrix
Given a 2D space of maximum size NxN which supports two operations : [1] void UPDATE(x,y,v) - sets the value of cell [x,y] to v [2] int SUM(x1,y1,x2,y2) - returns sub-rectangle sum (x1,y1) to (x2转载 2015-09-16 09:45:27 · 370 阅读 · 0 评论 -
float number square root
public float sqrt(float f, int p), precision是表示小数点后位数(2就要两位一致) p is the number of digits after the decimal point has to be rightSolution: 判断数据是否符合精确度,就用两个数都乘以10^p,再取整比较是否相等。比如p=2, f=0.64, curRes =转载 2015-09-16 02:10:13 · 531 阅读 · 0 评论 -
Rearrange int array to put odd number to left and even to right
双指针,左右调换。no extra memory, O(n)class Solution: def sepOddEven(self, nums): if not nums or len(nums) == 0: return None left = 0 right = len(nums) - 1 while left < righ转载 2015-09-16 03:30:07 · 350 阅读 · 0 评论 -
distinct sequence Google
leetcode ,暴力解法dfs, 双指针,不行 public int numDistinct(String s, String t) { return numDistinctHelper(s,t,0,0); } public int numDistinctHelper(String s, String t, int si, int ti){原创 2015-09-16 08:35:14 · 334 阅读 · 0 评论 -
adding up two (sum) big/large number with string representation
public static String sumStrings(String a, String b) { char[] num1 = a.toCharArray(); char[] num2 = b.toCharArray(); int i = num1.length - 1; int j = num2.length - 1;转载 2015-09-16 11:30:41 · 268 阅读 · 0 评论 -
different/Unique/distinct Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ /转载 2015-09-16 15:24:05 · 627 阅读 · 0 评论 -
Find the longest substring with k unique distinct characters in a given string (Google)
The problem can be solved in O(n). Idea is to maintain a window and add elements to the window till it contains less or equal k, update our result if required while doing so. If unique elements exceeds转载 2015-09-16 09:03:14 · 349 阅读 · 0 评论 -
longest increasing continuous subsequence in a 2D matrix
dp + dfs //These two are only for traversing around x,y public int[] xMove = { 1, 0, 0, -1 }; public int[] yMove = { 0, 1, -1, 0 }; public int longestIncreasingContinuousSubsequenceII(int转载 2015-09-17 02:16:48 · 424 阅读 · 0 评论