摘要: 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 假日笛声 阅读(207) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(218) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(160) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(159) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(274) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(178) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(190) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(277) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(463) 评论(0) 推荐(0) 编辑
摘要: 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 假日笛声 阅读(222) 评论(0) 推荐(0) 编辑