10 2013 档案

Triangle [LeetCode]
摘要: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], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).Note:Bonus point if you are a... 阅读全文

posted @ 2013-10-31 13:37 假日笛声 阅读(206) 评论(0) 推荐(0)

Reverse Linked List II [LeetCode]
摘要:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ≤m≤n≤ length of list.Summary: First writes down the reverse parts (m to n) 阅读全文

posted @ 2013-10-29 14:41 假日笛声 阅读(219) 评论(0) 推荐(0)

Simplify Path [LeetCode]
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"click to show corner cases.Corner Cases:Did you consider the case wherepath="/../"?In this case, you should return&quo 阅读全文

posted @ 2013-10-29 12:50 假日笛声 阅读(229) 评论(0) 推荐(0)

Anagrams [LeetCode]
摘要:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.Summary: To sort and compare strings, using map to record distinct strings. 1 class Solution { 2 public: 3 vector anagrams(vector &strs) { 4 vector result; 5 map str_m... 阅读全文

posted @ 2013-10-29 11:35 假日笛声 阅读(168) 评论(0) 推荐(0)

Permutations [LeetCode]
摘要:Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].Notes: recursive, in place. 1 class Solution { 2 public: 3 vector > permute(vector &num) { 7 int size = num.size(); 8 ... 阅读全文

posted @ 2013-10-29 11:00 假日笛声 阅读(171) 评论(0) 推荐(0)

Sum Root to Leaf Numbers [LeetCode]
摘要:Problem description:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/Basic idea:To store the num vector in every node of tree by starting from leaf, the go up util to root. 1 class Solution { 2 public: 3 vector> subNumbers(TreeNode *root) { 4 vector> sums; 5 if(root == NU... 阅读全文

posted @ 2013-10-27 07:35 假日笛声 阅读(284) 评论(0) 推荐(0)

Symmetric Tree [LeetCode]
摘要:Problem description:http://oj.leetcode.com/problems/symmetric-tree/Basic idea:Both recursive and iterative solutions.Iterative solution: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x)... 阅读全文

posted @ 2013-10-27 07:06 假日笛声 阅读(187) 评论(0) 推荐(0)

Combination Sum II [LeetCode]
摘要:Problem description:http://oj.leetcode.com/problems/combination-sum-ii/Basic idea: use recursive approach, remember to avoid duplicate item. 1 class Solution { 2 public: 3 bool isExist(vector > &combinations, vector &item) { 4 for(auto com: combinations){ 5 if(com.size() == i... 阅读全文

posted @ 2013-10-26 14:12 假日笛声 阅读(206) 评论(0) 推荐(0)

Maximal Rectangle [LeetCode]
摘要:Problem Description:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.Basic idea: To increas one dimension by one, then calculate the maximum of other dimension every time. 1 class Solution { 2 public: 3 int maximalRectangle( 阅读全文

posted @ 2013-10-25 15:35 假日笛声 阅读(285) 评论(0) 推荐(0)

Trapping Rain Water [LeetCode]
摘要:Problem Description:http://oj.leetcode.com/problems/trapping-rain-water/Basic idea: Get the index of max number of the array, then split the array at that point. Find the left max num, calcaute the water trapped between left max num and the max num. Go left recursively until meeting the start of the 阅读全文

posted @ 2013-10-21 13:59 假日笛声 阅读(477) 评论(0) 推荐(0)

Combination Sum [LeetCode]
摘要:Problem Description:http://oj.leetcode.com/problems/combination-sum/ Basic idea:It seems complicate at first. But once I got the recursive idea, the solution was neat. This code could be not efficient. I will rewrite with dynamic programming next time. 1 class Solution { 2 public: 3 vector > ... 阅读全文

posted @ 2013-10-20 14:04 假日笛声 阅读(228) 评论(0) 推荐(0)

Best Time to Buy and Sell Stock II [LeetCode]
摘要:Problem Description:http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/Basic idea: from code below, it seems super easy. But intuitively, we may use one more variable "tmp_max_profit" to record every times we get the max profit. 1 class Solution { 2 public: 3 int maxProfit( 阅读全文

posted @ 2013-10-19 14:57 假日笛声 阅读(171) 评论(0) 推荐(0)

Word Search [LeetCode]
摘要:Problem Description:http://oj.leetcode.com/problems/word-search/Basic idea: recursively go forward, use char '#' to mark the char visited, so no extra memory is need, don't forget to recover the previous char if the program cannot go forward. 1 class Solution { 2 public: 3 bool existSub( 阅读全文

posted @ 2013-10-18 14:48 假日笛声 阅读(169) 评论(0) 推荐(0)

Same Tree [LeetCode]
摘要:Problem Description:http://oj.leetcode.com/problems/same-tree/ 1 class Solution { 2 public: 3 bool isSameTree(TreeNode *p, TreeNode *q) { 4 // Note: The Solution object is instantiated only once and is reused by each test case. 5 if(p == NULL && q == NULL) 6 return tr... 阅读全文

posted @ 2013-10-18 06:14 假日笛声 阅读(163) 评论(0) 推荐(0)

Reverse Nodes in k-Group [LeetCode]
摘要:Problem Description:http://oj.leetcode.com/problems/reverse-nodes-in-k-group/Basic Idea: Do it like reverse a linked list with a counter started by k.... 阅读全文

posted @ 2013-10-17 14:11 假日笛声 阅读(179) 评论(0) 推荐(0)

Text Justification [LeetCode]
摘要:Problem Description:http://oj.leetcode.com/problems/text-justification/Note: Just be careful about boundary when implementing it. 1 class Solution { 2 public: 3 string genNormalLine(vector &words, int L, int start, int end){ 4 int word_count = end - start + 1; 5 string line; 6 ... 阅读全文

posted @ 2013-10-16 11:34 假日笛声 阅读(567) 评论(0) 推荐(0)

Path Sum [LeetCode]
摘要:Problem Description:http://oj.leetcode.com/problems/path-sum/Pretty easy. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */10 class Solution {11... 阅读全文

posted @ 2013-10-14 07:51 假日笛声 阅读(203) 评论(0) 推荐(0)

Multiply Strings [LeetCode]
摘要:Problem Descriptionhttp://oj.leetcode.com/problems/multiply-strings/Basic idea is to multiply two nums like we do caculation on paper, one by one digital multiplication, then add the temporary results together to get the final one. Be careful about the last carry digital. 1 class Solution { 2 public 阅读全文

posted @ 2013-10-12 15:19 假日笛声 阅读(461) 评论(0) 推荐(0)

Populating Next Right Pointers in Each Node II [Leetcode]
摘要:Problem Descriptionhttp://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/Basic idea is to get every nodes in the current level by iterating nodes of the previous level, then to iterate all nodes in current level to connect them with pointer "next". 1 /** 2 * Defini 阅读全文

posted @ 2013-10-11 15:03 假日笛声 阅读(239) 评论(0) 推荐(0)