面试题
文章平均质量分 57
_KingKazma_
这个作者很懒,什么都没留下…
展开
-
使用 trie tree 实现 auto-completion
In-Memory TrieTries are specialized data structures where a word can be stored as a sequence of characters. Reading the word involves traversing down the branch of the tree. At each node, the po转载 2014-10-20 06:15:16 · 795 阅读 · 0 评论 -
Move all zeroes to end of array
Move all zeroes to end of arrayGiven an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0,转载 2014-11-09 10:53:16 · 777 阅读 · 0 评论 -
面试准备汇总
准备经验总结:http://mitbbskr.com/article_t/JobHunting/32516109.htmlhttp://blog.zhuhuang.com/2011/12/apply-google-microsoft-intern/http://www.huaren.us/dispbbs.asp?boardid=373&id=1175952&page=3&sta转载 2013-12-27 00:03:37 · 1972 阅读 · 2 评论 -
Process 和 thread 的区别
进程与线程的一个简单解释作者: 阮一峰日期: 2013年4月24日进程(process)和线程(thread)是操作系统的基本概念,但是它们比较抽象,不容易掌握。最近,我读到一篇材料,发现有一个很好的类比,可以把它们解释地清晰易懂。1.计算机的核心是CPU,它承担了所有的计算任务。它就像一座工厂,时刻在转载 2014-02-24 22:00:05 · 3561 阅读 · 0 评论 -
Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width o转载 2014-02-20 07:49:53 · 503 阅读 · 0 评论 -
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha原创 2014-02-20 05:49:40 · 512 阅读 · 0 评论 -
Split String into Valid Words
给定一个 dict, 里面是valid words. 再给定一个 String, 将String split 为 dict 中 valid words 的划分方法public ArrayList splitValidWords(String str, TrieNode dict) { ArrayList result = new ArrayList();原创 2014-02-20 03:06:12 · 575 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
public int removeDuplicates(int[] A) { if (A == null || A.length == 0) { return 0; } int size = 0; for (int i = 1; i < A.length; i++) {原创 2014-02-20 00:38:11 · 459 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
public int removeDuplicates(int[] A) { if (A == null || A.length == 0) { return 0; } int size = 0; for (int i = 0; i < A.length; i++) {原创 2014-02-20 00:10:34 · 553 阅读 · 0 评论 -
Insertion Sort List
Sort a linked list using insertion sort.public ListNode insertionSortList(ListNode head) { if (head == null || head.next == null) { return head; } // usding dumm原创 2014-02-19 23:40:12 · 498 阅读 · 0 评论 -
Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.Th原创 2014-02-19 22:41:14 · 489 阅读 · 0 评论 -
LeetCode Gas Station
public class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int gasSum = 0; int costSum = 0; int start = 0; int gasLeft = 0; for原创 2014-09-29 12:03:35 · 465 阅读 · 0 评论 -
LeetCode 3sum
public class Solution { public List> threeSum(int[] num) { List> results = new ArrayList>(); if (num == null || num.length < 3) { return results; }原创 2014-09-29 11:06:05 · 388 阅读 · 0 评论 -
wild card matching
题目描述 (http://www.leetcode.com/onlinejudge 倒数第三题)Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (inc转载 2014-10-02 11:26:48 · 526 阅读 · 0 评论 -
Check if a binary tree is subtree of another binary tree
Given two binary trees, check if the first tree is subtree of the second one. A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T.The subtree corresponding转载 2014-09-30 21:15:29 · 657 阅读 · 0 评论 -
If you are given two traversal sequences, can you construct the binary tree?
It depends on what traversals are given. If one of the traversal methods is Inorder then the tree can be constructed, otherwise not.Therefore, following combination can uniquely identify a tree.转载 2014-09-30 21:16:02 · 738 阅读 · 0 评论 -
The Boyer-Moore Majority Vote Algorithm
Majority ElementMajority Element: A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element).Write a function转载 2014-09-30 03:48:08 · 1893 阅读 · 0 评论 -
面试中的涉及随机的算法
- Cracking the coding interview, 17.11implement rand7() , given rand5()原创 2014-09-30 01:02:16 · 864 阅读 · 0 评论 -
Leetcode wild card matching
public class Solution { private int[][] dp; public boolean isMatch(String s, String p) { if (s == null || p == null) { return false; } dp = ne原创 2014-11-12 03:29:58 · 693 阅读 · 0 评论 -
随机算法之蓄水池抽样问题
蓄水池抽样问题是从动态变化的N个元素中随机抽选出M个元素(N>=M)算法描述如下:Init : a reservoir with the size: k for i= k+1 to N M=random(1, i); if( M < k) SWAP the Mth value and转载 2014-09-30 01:07:04 · 674 阅读 · 0 评论 -
数据工程师必知算法:蓄水池抽样
引言:众所周知,想要面试一个统计学家和软件工程师的合体——数据工程师——是件很难的事情。我在面试中常使用的方法是:提出即需要算法设计,又需要一些概率论知识的问题,来考察面试者的功底。下面就是在硅谷非常流行的例子:“给出一个数据流,这个数据流的长度很大或者未知。并且对该数据流中数据只能访问一次。请写出一个随机选择算法,使得数据流中所有数据被选中的概率相等。”当面对这样一个问题的时候,转载 2014-09-30 01:04:18 · 878 阅读 · 0 评论 -
随机算法
1. 要求从N个元素中随机的抽取1个元素。2. 要求从N个元素中随机的抽取K个元素。int remain = Nint select = Kfor (int i = 0; i N; ++i){ if (rand()% remain select){ couti" "; --select; } --rem转载 2014-09-30 01:02:59 · 516 阅读 · 0 评论 -
3sum closet
public class Solution { public int threeSumClosest(int[] num, int target) { int result = Integer.MAX_VALUE; if (num == null || num.length < 3) { return result;原创 2014-09-29 11:49:41 · 484 阅读 · 0 评论 -
Slice Wood
描述木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给定了。当然,我们希望得到的小段越长越好,你的任务是计算能够得到的小段木头的最大长度。木头长度的单位是厘米。原木的长度都是正整数,我们要求切割得到的小段木头的长度也要求是正整数。例如输入 A = {232, 124, 456}, targ原创 2014-02-19 10:26:45 · 722 阅读 · 0 评论 -
Search For A Range
public int[] searchRange(int[] A, int target) { int[] result = {-1, -1}; if (A == null || A.length == 0) { return result; } int left = 0; int right = A.length - 1原创 2014-02-19 09:48:53 · 461 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
// inorder 1 2 3 | 4 | 5 6 7 // postorder 1 3 2 | 5 7 6 | 4public class Solution { private int findPosition(int[] arr, int start, int end, int target) { int i; for (i = sta原创 2014-02-23 12:57:17 · 516 阅读 · 0 评论 -
Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devis原创 2014-02-23 09:36:20 · 612 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node./** * Definition for binary原创 2014-02-23 01:22:00 · 581 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2014-02-23 01:14:02 · 580 阅读 · 0 评论 -
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 3Return 6.原创 2014-02-22 12:35:05 · 802 阅读 · 0 评论 -
print out Fibonacci reversely
题很简单,逆序地打印斐波那契数列。第一个想到的思路是,循环地得到数列的每个值,存在一个容器里(比如stack),然后逆序一一输出即可。但面试官要求空间复杂度O(1)的话,就不能用额外的空间。首先想到的就是递归地输出:public static void reverseFibo(int n) { if (n == 0) { System.out.println(0)原创 2014-02-22 11:19:58 · 830 阅读 · 2 评论 -
Minimum Window Substring
题目:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BA转载 2014-02-22 04:21:00 · 646 阅读 · 0 评论 -
Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ原创 2014-02-22 08:38:52 · 508 阅读 · 0 评论 -
Maximal Rectangle
这个需要O(n3),所以没有通过大集合的测试。leetcode的讨论组给出了一个比较难理解的方法,这里就不采用了。说说第三个方法。前一个笔记,我们讨论了柱状图的最大矩形面积,那可以O(n)的,学以致用呀!btw,leetcode的这两题也是挨一块儿的,用心良苦。。。。如果我们把每一行看成x坐标,那高度就是从那一行开始往上数的1的个数。带入我们的maxAreaInHi转载 2014-02-22 08:05:01 · 546 阅读 · 0 评论 -
Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.E原创 2014-02-21 00:31:39 · 547 阅读 · 0 评论 -
Next Permutation
public class Solution { public void nextPermutation(int[] num) { if (num == null || num.length <= 1) { return; } int partitionIndex = -1;转载 2014-02-24 05:37:47 · 481 阅读 · 0 评论 -
Clone Graph
/** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * ArrayList neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList(); } * };转载 2014-02-24 10:51:07 · 542 阅读 · 0 评论 -
从Trie树(字典树)谈到后缀树
作者:July、yansha。出处:http://blog.csdn.net/v_JULY_v 。 引言 常关注本blog的读者朋友想必看过此篇文章:从B树、B+树、B*树谈到R 树,这次,咱们来讲另外两种树:Tire树与后缀树。不过,在此之前,先来看两个问题。 第一个问题: 一个文本文件,大约有一万行,每行一个词,要求统计出其中最频繁出现的前10个词,请给出思想,转载 2014-02-19 07:02:57 · 710 阅读 · 0 评论 -
Regular Expression Matching
'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isM原创 2014-02-19 05:53:29 · 551 阅读 · 0 评论 -
Collection Iterator
// Collection is an interfacepublic interface Collection { boolean add(E element); Iterator iterator(); ... }public interface Iterator { E next(); boolean hasNext(); void原创 2014-02-18 06:19:34 · 525 阅读 · 0 评论