07 2013 档案

[Leetcode 95] 103 Binary Tree Zigzag Level Order Traversal
摘要:Problem:Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:... 阅读全文

posted @ 2013-07-28 12:13 freeneng 阅读(192) 评论(0) 推荐(0)

[Leetcode 94] 72 Binary Tree Inorder Traversal
摘要:Problem:Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?Analysis:Basic tree traversal problems. Recursive solution is just to proper arran... 阅读全文

posted @ 2013-07-28 11:48 freeneng 阅读(151) 评论(0) 推荐(0)

[Leetcode 93] 72 Edit Distance
摘要:Problem:Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a characterAnalysis:This is the classic dynamic p 阅读全文

posted @ 2013-07-28 11:00 freeneng 阅读(190) 评论(0) 推荐(0)

[Leetcode 92] 52 N-Queens II
摘要:Problem:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.Analysis:This is the extension of the former N-Queens problem. If we use the code before directly, there will be TLE problem. After checking the code, we find the isVali 阅读全文

posted @ 2013-07-28 08:41 freeneng 阅读(269) 评论(0) 推荐(0)

[Leetcode 91] 51 N-Queens
摘要:Problem:Then-queens puzzle is the problem of placingnqueens on ann�nchessboard such that no two queens attack each other.Given an integern, return all distinct solutions to then-queens puzzle.Each solution contains a distinct board configuration of then-queens' placement, where'Q'and' 阅读全文

posted @ 2013-07-28 07:16 freeneng 阅读(247) 评论(0) 推荐(0)

[Leetcode 90] 43 Multiply Strings
摘要:Problem:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.Analysis:It's the big number simulation problem. Recall the process how we compute the multiplication manually. Multiply the tow numb 阅读全文

posted @ 2013-07-27 08:02 freeneng 阅读(235) 评论(0) 推荐(0)

[Leetcode 89] 34 Search for a Range
摘要:Problem:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].A 阅读全文

posted @ 2013-07-27 06:18 freeneng 阅读(191) 评论(0) 推荐(0)

[Leetcode 88] 33 Search in Rotated Sorted Array
摘要:Problem: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.Analysis:The problem is 阅读全文

posted @ 2013-07-26 12:56 freeneng 阅读(169) 评论(0) 推荐(0)

[Leetcode 87] 29 Divide Two Integers
摘要:Problem:Divide two integers without using multiplication, division and mod operator.Analysis:I think the problem wants us to use binary search to find the answer instead of simply using some arithmetic operations. So the binary search version comes first. When dealing with it, pay attention to the e 阅读全文

posted @ 2013-07-25 08:27 freeneng 阅读(219) 评论(0) 推荐(0)

[Leetcode 86] 57 Insert Interval
摘要:Problem:Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Example 1:Given intervals[1,3],[6,9], insert and merge[2,5]in as[1,5],[6,9].Example 2:Given[1,2],[3,5], 阅读全文

posted @ 2013-07-25 06:32 freeneng 阅读(190) 评论(0) 推荐(0)

[Leetcode 85] 69 Sqrt(x)
摘要:Problem:Implementint sqrt(int x).Compute and return the square root ofx.Analysis:If we simply use the naive algorithm to go through all the numbers from 0 until the square root, it will exceed the time limit. The hero is binary search.We want such a number between 0 and n inclusively that x*x n. No. 阅读全文

posted @ 2013-07-25 05:43 freeneng 阅读(241) 评论(0) 推荐(0)

[Leetcode 84] 56 Merge Intervals
摘要:Problem:Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].Analysis:First sort the vector according to the start time of each interval.Then scan through the vector, if the current interval's start time is betwee 阅读全文

posted @ 2013-07-24 13:13 freeneng 阅读(158) 评论(0) 推荐(0)

[Leetcode 83] 28 Implement strStr()
摘要:Problem:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.Analysis:There are two ways: one is the naive comparing algorithm and the other is KMP algorithm.For KMP algorithm, please refer the wiki pageFor the naive algorithm, w 阅读全文

posted @ 2013-07-24 05:17 freeneng 阅读(176) 评论(0) 推荐(0)

[Leetcode 82] 116 Populating Next Right Pointers In Each Node
摘要:Problem:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set ... 阅读全文

posted @ 2013-07-24 01:31 freeneng 阅读(164) 评论(0) 推荐(0)

[Leetcode 81] 114 Flatten Binary Tree to Linked List
摘要:Problem:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6Analysis:Solution to t... 阅读全文

posted @ 2013-07-23 14:19 freeneng 阅读(188) 评论(0) 推荐(0)

[Leetcode 80] 107 Binary Tree Level Order Traversal II
摘要:Problem:Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [... 阅读全文

posted @ 2013-07-23 02:36 freeneng 阅读(150) 评论(0) 推荐(0)

[Leetcode 79] 106 Construct Binary Tree from Inorder and Postorder Traversal
摘要:Problem:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Analysis:This problem is the same as the former one of reconstructing the binary tree with inorder and preorder traversal array.The only difference is that 阅读全文

posted @ 2013-07-23 02:21 freeneng 阅读(122) 评论(0) 推荐(0)

[Leetcode 78] 105 Construct Binary Tree from Preorder and Inorder Traversal
摘要:Problem:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Analysis:We know that, for a binary tree, the structure of inorder traversal is as follows:|...left subtree inorder...| root |...right subtree inorder...|and 阅读全文

posted @ 2013-07-23 02:06 freeneng 阅读(180) 评论(0) 推荐(0)

[Leetcode 77] 102 Binary Tree Level Order Traversal
摘要:Problem:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]Analysis:The problem is an extensi... 阅读全文

posted @ 2013-07-22 15:13 freeneng 阅读(182) 评论(0) 推荐(0)

[Leetcode 76] 98 Validate Binary Search Tree
摘要:Problem: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 nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the lef 阅读全文

posted @ 2013-07-22 13:54 freeneng 阅读(241) 评论(0) 推荐(0)

[Leetcode 75] 96 Unique Binary Search Trees
摘要:Problem:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2... 阅读全文

posted @ 2013-07-22 13:24 freeneng 阅读(211) 评论(0) 推荐(0)

[Leetcode 74] 92 Restore IP Addresses
摘要:Problem:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)Analysis:Originally, I thought DFS may be a good solution. 阅读全文

posted @ 2013-07-22 12:53 freeneng 阅读(273) 评论(0) 推荐(0)

[Leetcode 73] 92 Reverse Linked List II
摘要:Problem: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 next == NULL)15 return head;16 17 ListNode dmy(-1), *p, *pm;18 ... 阅读全文

posted @ 2013-07-22 12:18 freeneng 阅读(157) 评论(0) 推荐(0)

[Leetcode 72] 91 Decode Ways
摘要:Problem:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12" 阅读全文

posted @ 2013-07-22 05:43 freeneng 阅读(209) 评论(0) 推荐(0)

[Leetcode 71] 86 Partition List
摘要:Problem:Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2-> 阅读全文

posted @ 2013-07-21 13:01 freeneng 阅读(197) 评论(0) 推荐(0)

[Leetcode 70] 82 Remove Duplicates from Sorted List II
摘要:Problems:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3.Analysis:This problem is a bit different fro 阅读全文

posted @ 2013-07-21 07:27 freeneng 阅读(155) 评论(0) 推荐(0)

[Leetcode 69] 79 Word Search
摘要:Problem:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.For example,Givenboa 阅读全文

posted @ 2013-07-20 08:24 freeneng 阅读(236) 评论(0) 推荐(0)

[Leetcode 68] 59 Spiral Matrix II
摘要:Problem:Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]Analysis:Treat it as a simulation problem. Use a "dir" variable to represent the direction 阅读全文

posted @ 2013-07-17 13:49 freeneng 阅读(167) 评论(0) 推荐(0)

[Leetcode 67] 31 Next Permutation
摘要:Problem:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra m 阅读全文

posted @ 2013-07-16 11:04 freeneng 阅读(171) 评论(0) 推荐(0)

[Leetcode 66] 48 Rotate Image
摘要:Problem:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Analysis:Please reference the answer on stackoverflow.http://stackoverflow.com/questions/3488691/how-to-rotate-a-matrix-90-degrees-without-using-any-extra-spaceT 阅读全文

posted @ 2013-07-16 09:45 freeneng 阅读(213) 评论(0) 推荐(0)

[Leetcode 65] 120 Triangle
摘要:Problem: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 y... 阅读全文

posted @ 2013-07-16 01:25 freeneng 阅读(191) 评论(0) 推荐(0)

导航