随笔分类 -  LeetCode

上一页 1 2 3 4 5 6 7 ··· 9 下一页
leetcode Palindrome Partitioning
摘要:把一个字符串划分成几个回文子串,枚举所有可能的划分例如For example, givens="aab",Return[ ["aa","b"], ["a","a","b"] ]写一个子函数判断是否为回文。然后dfs,这个dfs比之前的稍微难理解一些。dfs函数每次输入的起点代表之前已经处理好了,从这... 阅读全文
posted @ 2014-12-08 00:31 higerzhang 阅读(210) 评论(0) 推荐(0)
leetcode[130] Surrounded Regions
摘要:给定一个类似棋盘,有X和O,把X圈住的O变为X例如:For example,X X X XX O O XX X O XX O X XAfter running your function, the board should be:X X X XX X X XX X X XX O X X其实这题的思路... 阅读全文
posted @ 2014-12-07 00:47 higerzhang 阅读(1794) 评论(0) 推荐(0)
leetcode[129] Sum Root to Leaf Numbers
摘要:给定一个数,从根节点到叶节点是一个值,返回所有的值的和。例如:For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.The root-to-leaf path1->3represents the ... 阅读全文
posted @ 2014-12-06 20:25 higerzhang 阅读(155) 评论(0) 推荐(0)
leetcode 128 Longest Consecutive Sequence
摘要:给定一个数组,找出里面连续的子序列的最长长度。例如:Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.思路一:排序,然后count一下就o... 阅读全文
posted @ 2014-12-06 19:44 higerzhang 阅读(407) 评论(0) 推荐(0)
leetcode Word Ladder II
摘要:和上题Word Ladder I题目差不多,不过这里是要记录所有最段路径的可能。不同点在于,我们不能再BFS每层的时候把相距一个字符的字符串在dict中删除,因为hot -> hit 的话其他的例如 jit -> hit 就是hit可以出现在两条路径里头。所以不能立马删除。但是我们发现我们可以删除的... 阅读全文
posted @ 2014-12-05 23:30 higerzhang 阅读(186) 评论(0) 推荐(0)
leetcode Word Ladder
摘要:给定两个字符串start和end,和一个字符串字典dict,判断从start到end 要经过几次变化,每次变化只能取字典里的词,且相邻的变化只有一个字符不同。例如:For example,Given:start="hit"end="cog"dict=["hot","dot","dog","lot",... 阅读全文
posted @ 2014-12-04 20:48 higerzhang 阅读(342) 评论(0) 推荐(0)
leetcode[125] Valid Palindrome
摘要:判断一个字符串是不是回文,忽略其中的非数字和非字母,例如符号和空格不考虑。For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome.Note:Have you consider ... 阅读全文
posted @ 2014-12-03 23:39 higerzhang 阅读(288) 评论(0) 推荐(0)
leetcode Binary Tree Maximum Path Sum
摘要:给定一颗数,然后任意的起点或者终点,找到最大的路径和。例如:Given the below binary tree, 1 / \ 2 3Return6.一看到是标hard的题目就觉得要吃力才能搞出来。果然,经过不懈奋斗,成功从Time Limited到AC。我看到这... 阅读全文
posted @ 2014-12-03 22:48 higerzhang 阅读(186) 评论(0) 推荐(0)
leetcode 123 Best Time to Buy and Sell Stock III
摘要:是之前两道题leetcode Best Time to Buy and Sell Stock和leetcode Best Time to Buy and Sell Stock II的加强版。这里要求只能买两次股票。做了一个多小时,试了好多次,终于AC了。思路:找到有可能为断点的地方,也就是出现递减的... 阅读全文
posted @ 2014-12-02 23:39 higerzhang 阅读(347) 评论(0) 推荐(0)
leetcode Best Time to Buy and Sell Stock II
摘要:和上一题类似Best Time to Buy and Sell Stock,这里还是用一个数组存每天股票价格,你有很多次买入卖出的机会,但是每次买入要在前一次卖出前。Say you have an array for which theithelement is the price of a giv... 阅读全文
posted @ 2014-12-02 21:45 higerzhang 阅读(221) 评论(0) 推荐(0)
leetcode Best Time to Buy and Sell Stock
摘要:有一个价格数组,对应的位置代表改天股票的价格,你有一次买入和卖出的机会,你如何使得收益最大化。也就是返回profit。Say you have an array for which theithelement is the price of a given stock on dayi.If you ... 阅读全文
posted @ 2014-12-02 21:38 higerzhang 阅读(465) 评论(0) 推荐(0)
leetcode[120] Triangle
摘要:给定一个三角数组,找从上到下的最小和,例如:For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11... 阅读全文
posted @ 2014-12-02 12:28 higerzhang 阅读(261) 评论(0) 推荐(0)
leetcode Pascal's Triangle II
摘要:和Pascal's Triangle类似,这里是不需要记录所有的,而是给定一个行号,返回那一行的数据就可以了。例如:For example, givenk= 3,Return[1,3,3,1].所以给0的时候返回[1].题目要求O(k)空间,也就是除了要返回的空间外,其他是常数空间。那就用一个tmp... 阅读全文
posted @ 2014-12-02 11:41 higerzhang 阅读(160) 评论(0) 推荐(0)
leetcode Populating Next Right Pointers in Each Node II
摘要:这题和上一题Populating Next Right Pointers in Each Node的不一样了,这里要求的是普通的树,难度就比较大了。之前是简单的找到左边的最后,和右边的最左链接就可以了。现在存在的问题是可能右边的左右一层不是在最左或者长度不一等等。 1 / ... 阅读全文
posted @ 2014-12-02 00:51 higerzhang 阅读(772) 评论(0) 推荐(0)
leetcode Populating Next Right Pointers in Each Node
摘要:看图就知道想要做什么事了。Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Given the fo... 阅读全文
posted @ 2014-12-01 00:02 higerzhang 阅读(270) 评论(0) 推荐(0)
leetcode Pascal's Triangle
摘要:给定行号,输出如下所示Pascal Triangle(杨辉三角)For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]思路,想到右上一个构建下一个,构成过程就是上一个的相邻... 阅读全文
posted @ 2014-11-30 22:22 higerzhang 阅读(303) 评论(0) 推荐(0)
leetcode[115] Distinct Subsequences
摘要:给定字符串S和T,S通过删除某些位置的字符得到T的话,就记作一种subSequence。返回总共有几种。Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a ... 阅读全文
posted @ 2014-11-30 21:54 higerzhang 阅读(2167) 评论(0) 推荐(0)
leetcode Flatten Binary Tree to Linked List
摘要:题目:给定一个棵树,将其转换成flattened tree。只有右节点的,类似于链表,且在原址操作。例如:Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look l... 阅读全文
posted @ 2014-11-30 18:04 higerzhang 阅读(196) 评论(0) 推荐(0)
leetco Path Sum II
摘要:和上一题类似,这里是要记录每条路径并返回结果。Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 ... 阅读全文
posted @ 2014-11-30 10:37 higerzhang 阅读(368) 评论(0) 推荐(0)
leetcode Path Sum
摘要:题目:给定一颗数,以及一个数sum。判断是否存在从树到叶节点的和等于sum。例如:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ ... 阅读全文
posted @ 2014-11-30 09:53 higerzhang 阅读(281) 评论(0) 推荐(0)

上一页 1 2 3 4 5 6 7 ··· 9 下一页