摘要: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:...
阅读全文
摘要: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...
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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'
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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],
阅读全文
摘要: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.
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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 ...
阅读全文
摘要: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...
阅读全文
摘要: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], [...
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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...
阅读全文
摘要: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
阅读全文
摘要: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...
阅读全文
摘要: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.
阅读全文
摘要: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 ...
阅读全文
摘要: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"
阅读全文
摘要: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->
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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...
阅读全文