Morris InOrder Traverse Binary Tree 无需使用递归和栈
摘要:今天在切leetcode的时候看到一个Morris算法,用来中序遍历二叉树,非递归,O(1)空间。觉得很强大。记录一下。基本思想是利用了Threaded Binary Tree。步骤如下:current节点设置为root。如果current不为空,到2,否则返回;如果current没有左子树,输出current的值,current等于current.right;如果current有左子树,首先找到current节点的precedent,也就是该节点左子树中最最右边那个节点。然后把最最右边这个节点的右link指向当前节点。如下图。 e.g. 当current是7的时候,我们找到4,并人为地...
阅读全文
posted @
2013-07-25 15:14
lichen782
阅读(853)
推荐(0)
LeetCode 笔记系列 20 Interleaving String [动态规划的抽象]
摘要:题目:Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return false.这个题目值得记录的原因除了自己的解法没有通过大集合以外,主要还在与对动态规划的理解。在这两个月研究算法的过程中,我发现自己更倾向于直观的理解,而抽象思维上相对较弱
阅读全文
posted @
2013-07-24 12:06
lichen782
阅读(5266)
推荐(1)
LeetCode 笔记系列 19 Scramble String [合理使用递归]
摘要:题目:Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation ofs1="great": great / \ gr eat / \ / \g r e at / \ a tTo scramble the string, we may choose any non-leaf...
阅读全文
posted @
2013-07-21 17:18
lichen782
阅读(1976)
推荐(0)
LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
摘要:题目:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.leetcode的题目真是越来越经典了。比如这个题目,就是一道男默女泪的题。一般人拿到这个题目,除非做过类似的,很难一眼找出一个方法来,更别说找一个比较优化的方法了。首先一个难点就是,你怎么判断某个区域就是一个矩形呢?其次,以何种方式来遍历这个2D的matrix呢?一般来说,对这种“棋盘式”的题目,像什么Queen啦,象棋啦,数独啦,如果没有
阅读全文
posted @
2013-07-20 23:17
lichen782
阅读(23526)
推荐(3)
LeetCode 笔记系列 17 Largest Rectangle in Histogram
摘要:题目:Largest Rectangle in HistogramGivennnon-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 of each bar is 1, given height =[2,1,5,6,2,3].The largest rectangle is shown
阅读全文
posted @
2013-07-17 21:51
lichen782
阅读(29858)
推荐(11)
LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
摘要:题目: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"BANC".Note:If there is no such window in S that covers all characters in T, return the em
阅读全文
posted @
2013-07-16 18:45
lichen782
阅读(5326)
推荐(4)
LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
摘要:题目: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"BANC".Note:If there is no such window in S that covers all characters in T, return the em
阅读全文
posted @
2013-07-16 17:14
lichen782
阅读(866)
推荐(0)
LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
摘要:题目: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"BANC". Note: If there is no such window in S that covers all characters in T, return
阅读全文
posted @
2013-07-16 16:48
lichen782
阅读(1254)
推荐(0)
LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]
摘要:题目:Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m+n) space, but still not the best solution.Could you devise a constant space
阅读全文
posted @
2013-07-14 22:04
lichen782
阅读(703)
推荐(0)
LeetCode 笔记系列 14 N-Queen II [思考的深度问题]
摘要:题目:Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions.就是让你输出N皇后问题的解法数目。直接求解,每次还记录整个棋盘位置那种方法就不说了,必须超时。有一个牛逼大了的超级无敌帅的位移动解法。我们暂且不表。看看我当时想的一个解法。首先,对于皇后的那个递归方法,我有三个变量分别表示1.一个int值,表示递归到当前的level,当前的哪几个col被占领了,例如11011就表示我们下一个Q只能放在第三个(.
阅读全文
posted @
2013-07-12 20:22
lichen782
阅读(2816)
推荐(0)
LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
摘要:题目: Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A =[2,3,1,1,4]The minim
阅读全文
posted @
2013-07-10 23:13
lichen782
阅读(16864)
推荐(0)
LeetCode 笔记系列12 Trapping Rain Water [复杂的代码是错误的代码]
摘要:题目:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given[0,1,0,2,1,0,1,3,2,1,2,1], return6.例如给出上图中的黑色部分(数组表示),让你求出蓝色部分。这也是个神题。。。当然对小白我来说。想了半天,是不是遍历数组呢,然后依次计算当前bar构成的container大小。问题在于,这个..
阅读全文
posted @
2013-07-10 19:04
lichen782
阅读(1300)
推荐(0)
LeetCode 笔记系列11 First Missing Positive [为什么我们需要insight]
摘要:题目:Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.为什么这道题值得纪念呢? 因为它教育我们看问题看本质。要看出问题本质,首先要深刻理解问题本身是在说啥。。。(越来越像学习某某core的讲话了)在一个无序的整数数组中,找出第一个没有的正数。什么意思呢?这么想,所有正数是
阅读全文
posted @
2013-07-10 10:08
lichen782
阅读(1099)
推荐(0)
LeetCode 笔记系列十 Suduko
摘要:题目:Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character'.'. You may assume that there will be only one unique solution.下面是一个数独的题目:其解:数独不是很了解,没做过。不过知道规则。就是在这个9x9的格纸中间添1到9的数字。使每一行不能重复,每一列也不能重复,然后上面那个粗线框起来的3x3的格纸中的数字也不能重复。不知道这样的游戏有啥意义。。
阅读全文
posted @
2013-07-07 18:27
lichen782
阅读(698)
推荐(0)
LeetCode 笔记系列九 Search in Rotated Sorted Array
摘要:题目:Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array.就是说,排序数组可能是右移了一定位数。让你在这...
阅读全文
posted @
2013-07-07 17:39
lichen782
阅读(2202)
推荐(0)
LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
摘要:题目:Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring. For"(()", the longest valid parentheses substring is"()", which has length = 2. Another example is")()())", where the lon
阅读全文
posted @
2013-07-06 20:38
lichen782
阅读(4433)
推荐(0)
LeetCode 笔记系列七 Substring with Concatenation of All Words
摘要:题目:You are given a string,S, and a list of words,L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.For example, given:S:"barfoothefoobarman"L:["foo", &qu
阅读全文
posted @
2013-07-06 11:29
lichen782
阅读(1867)
推荐(0)
LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]
摘要:题目:Given a linked list, reverse the nodes of a linked listkat a time and return its modified list. If the number of nodes is not a multiple ofkthen left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory i...
阅读全文
posted @
2013-07-05 17:04
lichen782
阅读(14161)
推荐(2)
LeetCode 笔记系列五 Generate Parentheses
摘要:题目:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"解法:leetcode上的解法很赞。 其实这也是利用的递归的分支。构建了一树状结构并遍历,叶子节点就是valid
阅读全文
posted @
2013-07-04 17:56
lichen782
阅读(1093)
推荐(0)
LeetCode 笔记系列四 Remove Nth Node From End of List
摘要:题目:Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.就是让你删除单链表倒数第n个节点,同时希望能只遍历一次。解法一: 如果不遍历完所有节点,怎么知道倒数第n个在哪里呢?
阅读全文
posted @
2013-07-04 15:50
lichen782
阅读(551)
推荐(0)
LeetCode 笔记系列三 3Sum
摘要:题目:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2)额外的要求是不能返回重复的triplets,返回的a,b,c的顺序要是非递减的。解法一:首先想一下,三个数相加,要为0的话...
阅读全文
posted @
2013-07-04 11:47
lichen782
阅读(1791)
推荐(0)
LeetCode 笔记系列二 Container With Most Water
摘要:题目:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.就是说,x轴上
阅读全文
posted @
2013-07-03 18:10
lichen782
阅读(5386)
推荐(1)
LeetCode 笔记系列一 Median of Two Sorted Arrays
摘要:题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 刚开始的时候理解有误,以为是求中位数。其实不是。这里所谓的"median of the two sorted arrays"指的是:如果A.length + B.length 是奇数,返回merge后中间的那个数;如果是偶数,返回中间两个数的平均
阅读全文
posted @
2013-07-02 15:31
lichen782
阅读(2506)
推荐(0)