08 2014 档案

Symmetric Tree
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / 阅读全文

posted @ 2014-08-28 19:49 bug睡的略爽 阅读(140) 评论(0) 推荐(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 阅读全文

posted @ 2014-08-28 19:38 bug睡的略爽 阅读(133) 评论(0) 推荐(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 i 阅读全文

posted @ 2014-08-28 18:31 bug睡的略爽 阅读(224) 评论(0) 推荐(0)

Subsets系列
摘要:Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set 阅读全文

posted @ 2014-08-28 17:05 bug睡的略爽 阅读(195) 评论(0) 推荐(0)

Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree
摘要:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转 阅读全文

posted @ 2014-08-27 22:14 bug睡的略爽 阅读(149) 评论(0) 推荐(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 dept 阅读全文

posted @ 2014-08-26 17:15 bug睡的略爽 阅读(152) 评论(0) 推荐(0)

Minimum Depth of Binary Tree & Maximum Depth of Binary Tree
摘要:Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest l 阅读全文

posted @ 2014-08-26 16:47 bug睡的略爽 阅读(122) 评论(0) 推荐(0)

Flatten Binary Tree to Linked List
摘要: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 6 The flattened tr 阅读全文

posted @ 2014-08-26 16:30 bug睡的略爽 阅读(138) 评论(0) 推荐(0)

Path Sum系列
摘要:Path Sum 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 giv 阅读全文

posted @ 2014-08-26 16:23 bug睡的略爽 阅读(179) 评论(0) 推荐(0)

Best Time to Buy and Sell Stock系列
摘要:Best Time to Buy and Sell Stock 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 阅读全文

posted @ 2014-08-25 15:38 bug睡的略爽 阅读(184) 评论(0) 推荐(0)

Longest Palindromic Substring
摘要:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longes 阅读全文

posted @ 2014-08-23 20:48 bug睡的略爽 阅读(140) 评论(0) 推荐(0)

Palindrome Partitioning系列
摘要:Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome parti 阅读全文

posted @ 2014-08-23 20:27 bug睡的略爽 阅读(166) 评论(0) 推荐(0)

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 t 阅读全文

posted @ 2014-08-22 20:26 bug睡的略爽 阅读(142) 评论(0) 推荐(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 3 阅读全文

posted @ 2014-08-22 16:53 bug睡的略爽 阅读(129) 评论(0) 推荐(0)

Longest Consecutive Sequence
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longes 阅读全文

posted @ 2014-08-22 16:21 bug睡的略爽 阅读(143) 评论(0) 推荐(0)

Sum Root to Leaf Numbers
摘要:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 whic 阅读全文

posted @ 2014-08-22 15:55 bug睡的略爽 阅读(114) 评论(0) 推荐(0)

Reverse Integer
摘要:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here a 阅读全文

posted @ 2014-08-19 23:29 bug睡的略爽 阅读(147) 评论(0) 推荐(0)

Valid Palindrome
摘要:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Pan 阅读全文

posted @ 2014-08-19 23:10 bug睡的略爽 阅读(155) 评论(0) 推荐(0)

Palindrome Number
摘要:Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? 阅读全文

posted @ 2014-08-19 22:54 bug睡的略爽 阅读(126) 评论(0) 推荐(0)

Gas Station
摘要:There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it cost 阅读全文

posted @ 2014-08-19 20:46 bug睡的略爽 阅读(259) 评论(0) 推荐(0)

Reverse Words in a String
摘要:Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". click to show clarification. 阅读全文

posted @ 2014-08-19 18:26 bug睡的略爽 阅读(151) 评论(0) 推荐(0)

Maximum Subarray
摘要:Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array 阅读全文

posted @ 2014-08-19 16:17 bug睡的略爽 阅读(416) 评论(0) 推荐(0)

Permutations系列
摘要:一. 全排列递归算法 1. 数据没有重复的情况下 算法:每个元素依次与后面的数进行交换 例子:假设元素为123,则递归交换事例如下: 2. 数据有重复的情况下、 算法:在没有数据重复的全排列下,交换需要加上前提条件,即元素应该和后面没有重复出现的数字进行交换,即当访问到第k个元素的时候,如果 [k, 阅读全文

posted @ 2014-08-16 16:05 bug睡的略爽 阅读(184) 评论(0) 推荐(0)

海量数据等概率选取问题
摘要:1、问题定义可以简化如下:在不知道文件总行数的情况下,如何从文件中随机的抽取一行,并且每行被抽中的概率相等? 首先想到的是我们做过类似的题目吗?当然,在知道文件行数的情况下,我们可以很容易的用C运行库的rand()函数随机的获得一个行数,从而随机的取出一行,但是,当前的情况是不知道行数,这样如何求呢... 阅读全文

posted @ 2014-08-15 20:52 bug睡的略爽 阅读(207) 评论(0) 推荐(0)

Sort List系列
摘要:在严老师的数据结构书上学习了各种排序算法,比如插入排序,选择排序,堆排序,快速排序,归并排序等,不过书上代码的应用场景均是底层结构是顺序存储的情况,即数组。 今天总结下对于单链表,常用的排序算法能否使用。由于希尔排序、堆排序、计数排序都要求随机访问,而单链表只能顺序访问,故这 3 种是不能用于单向链 阅读全文

posted @ 2014-08-14 22:20 bug睡的略爽 阅读(227) 评论(0) 推荐(0)

Linked List Cycle系列
摘要:首先讨论下有环单链表相关问题 1. 判断单链表是否有环 使用slow,fast指针从头开始扫描链表,slow指针每次走一步,fast指针每次走两步,如果链表有环,那么fast指针一定会追上slow指针,否则,fast指针会遇到null。 2. 求有环单链表的环长 设环长为R,当fast,slow指针 阅读全文

posted @ 2014-08-14 15:49 bug睡的略爽 阅读(173) 评论(0) 推荐(0)

Construct Binary Tree系列
摘要:Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may ass 阅读全文

posted @ 2014-08-12 23:38 bug睡的略爽 阅读(234) 评论(0) 推荐(0)

Binary Tree Level Order Traversal系列
摘要: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) 阅读全文

posted @ 2014-08-12 22:25 bug睡的略爽 阅读(141) 评论(0) 推荐(0)

Binary Tree Traversal系列
摘要:Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 阅读全文

posted @ 2014-08-12 21:00 bug睡的略爽 阅读(139) 评论(0) 推荐(0)

Single Number 系列
摘要:Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear 阅读全文

posted @ 2014-08-10 23:49 bug睡的略爽 阅读(158) 评论(0) 推荐(0)

导航