
Algorithms
文章平均质量分 72
Yadoer
这个作者很懒,什么都没留下…
展开
-
Reservoir sampling(水塘抽样)
题目1:给出一个数据流,这个数据流的长度很大或者未知。并且对该数据流中数据只能访问一次。请写出一个随机选择算法,使得数据流中所有数据被选中的概率相等。对于复杂问题一定要学会归纳总结,即从小例子入手,然后分析,得出结论,然后在证明。不然遇到一个抽象问题,不举例感觉这个问题,直接解还是比较难的。对于此问题的难处就是数据流的长度未知,如果已知,so easy。现在进行归纳总结:1)原创 2015-09-11 15:28:36 · 7107 阅读 · 1 评论 -
LeetCode-Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled,原创 2015-08-30 13:23:37 · 1144 阅读 · 0 评论 -
LeetCode-Same Tree & Symmetric Tree
两道题放一块了,一个是判断两棵树是否相同:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have原创 2015-02-03 17:50:48 · 1118 阅读 · 0 评论 -
LeetCode-Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.此题是Majority Element的继续,关于此题可以移步至Majority Elem原创 2015-08-06 11:03:29 · 1259 阅读 · 0 评论 -
LeetCode-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", ret原创 2015-08-28 17:48:18 · 1058 阅读 · 0 评论 -
LeetCode-Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non原创 2015-08-28 15:21:03 · 947 阅读 · 0 评论 -
LeetCode-Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet原创 2015-08-13 20:57:10 · 721 阅读 · 0 评论 -
LeetCode-Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empt原创 2015-08-13 20:08:16 · 1065 阅读 · 0 评论 -
LeetCode-Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly si原创 2015-08-27 17:05:39 · 978 阅读 · 0 评论 -
LeetCode-Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [原创 2015-08-27 14:34:47 · 906 阅读 · 0 评论 -
LeetCode-Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0原创 2015-08-27 17:45:34 · 873 阅读 · 0 评论 -
LeetCode-Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2015-08-13 15:29:44 · 891 阅读 · 0 评论 -
LeetCode - Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:原创 2015-08-27 20:02:41 · 845 阅读 · 0 评论 -
LeetCode-Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu原创 2015-08-25 19:57:08 · 1066 阅读 · 0 评论 -
LeetCode-Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O原创 2015-07-31 10:18:18 · 907 阅读 · 0 评论 -
LeetCode-Best Time to Buy and Sell Stock I II III IV
此处的三个题跟Maximum Subarray,可以先看此题I)Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, b原创 2015-08-25 17:46:27 · 1105 阅读 · 0 评论 -
LeetCode-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原创 2015-08-25 15:59:19 · 1205 阅读 · 0 评论 -
LeetCode-Maximum/Minimum Depth of Binary Tree
iven a binary tree, find its maximum/minimum depth.The maximum/minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.两个很相似的题,但是解法完全不怎么原创 2015-08-14 15:39:02 · 850 阅读 · 0 评论 -
LeetCode-Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.没什么可说的,直接上代码:原创 2015-08-14 21:16:15 · 698 阅读 · 0 评论 -
LeetCode-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原创 2015-08-15 16:10:26 · 1112 阅读 · 0 评论 -
LeetCode-Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2015-08-21 11:43:47 · 920 阅读 · 0 评论 -
LeetCode-Generate Parentheses & Letter Combinations of a Phone Number
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()原创 2015-08-20 14:20:53 · 1094 阅读 · 0 评论 -
Egg dropping puzzle
问题:有一个100层高的楼,如果一个鸡蛋从n层掉下来或者n层以上,鸡蛋就会碎掉。已知,现在有两个鸡蛋,找到满足条件的n,使在最坏情况下,丢鸡蛋的次数最少。原题链接可以转到wiki:点击打开链接很明显,不管egg1怎么丢,egg2肯定得线性得丢。我们假设egg1每次丢10层,即按10f, 20f, 30f, 40f, 50f, 60f, 70f, 80f, 90f, 100f。则,如原创 2015-08-18 19:40:53 · 1293 阅读 · 0 评论 -
LeetCode-Subsets(子集)
三种不同的方式,生产一个集合的所有子集,回溯法、迭代法。。。原创 2015-02-09 20:22:49 · 1116 阅读 · 0 评论 -
位操作总结
有一类题是位操作,但是感觉在面试中,很少有这种题,毕竟不需要那么细的操作,感觉位操作在汇编和C语言关系比较大,比较偏底层。但是有时候用位操作往往得到意想不到的效果。http://graphics.stanford.edu/~seander/bithacks.html关于常见的为操作可以看这链接,将了好多方法,现将比较常见的整理一下。一、位的基本操作基本操作主要有六种,与、或、异或、原创 2015-08-18 15:41:22 · 1408 阅读 · 0 评论 -
LeetCode-Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority原创 2015-01-27 10:31:47 · 887 阅读 · 0 评论 -
LeetCode-Lowest Common Ancestor of a Binary Tree 深度解析
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two node原创 2015-08-16 18:48:04 · 1577 阅读 · 0 评论 -
LeetCode-Single NumberI II III
I)Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2015-08-17 20:30:34 · 992 阅读 · 0 评论 -
LeetCode-3SUM(数组中三数之和为0)及其 closest
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2015-02-07 15:44:08 · 2009 阅读 · 0 评论 -
剑指offer-二叉搜索树与双向链表
输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向两步。空间复杂度为O(1).最笨的方法就是中序遍历放在一个数组或链表中,再次遍历串联起来。但是要求空间复杂度为O(1),就没办法这么做了,这时候可以参考这个解法。http://blog.youkuaiyun.com/my_jobs/article/details/47666909判断一个树是不是一颗二叉搜索树,可以模仿最后的那个方法。本题跟原创 2015-08-17 15:10:36 · 1332 阅读 · 0 评论 -
LeetCode-Path Sum II
题目:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree原创 2015-01-30 21:11:27 · 642 阅读 · 0 评论 -
LeetCode - Populating Next Right Pointers in Each Node II 及其变形题
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2015-08-15 18:10:11 · 1042 阅读 · 0 评论 -
LeetCode-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 diffe原创 2015-08-14 16:53:10 · 803 阅读 · 0 评论 -
LeetCode-Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2015-08-14 17:28:44 · 947 阅读 · 0 评论 -
LeetCode-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原创 2015-08-15 16:11:26 · 1387 阅读 · 0 评论 -
LeetCode-Number of Digit One(编程之美-1的数目)
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the followin原创 2015-08-29 18:05:33 · 2338 阅读 · 1 评论 -
LeetCode-N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2015-08-22 17:29:00 · 1106 阅读 · 0 评论 -
LeetCode-Word Break II
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"原创 2015-03-13 20:56:46 · 1076 阅读 · 0 评论 -
LeetCode-Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.跟题目http://blog.youkuaiyun.com/my_jobs/article/details/431原创 2015-02-04 22:15:22 · 590 阅读 · 0 评论 -
LeetCode-Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2015-02-04 21:25:45 · 900 阅读 · 0 评论