11 2013 档案

Convert Sorted List to Binary Search Tree [LeetCode]
摘要:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Solution: 1 TreeNode *sortedListToBST(ListNode *head) { 2 if(head == NULL) 3 return NULL; 4 if(head->next == NULL) 5 return new TreeNode(head->val)... 阅读全文

posted @ 2013-11-29 07:41 假日笛声 阅读(183) 评论(0) 推荐(0)

Balanced Binary Tree [LeetCode]
摘要:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1.Solution: 1 bool helperBalanced(int *depth, TreeNode * root){ 2 if(root->left... 阅读全文

posted @ 2013-11-29 07:22 假日笛声 阅读(190) 评论(0) 推荐(0)

Minimum Depth of Binary Tree [LeetCode]
摘要:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Summary: BFS or DFS with recursion. 1 int minDepth(TreeNode *root) { 2 if(root == NULL) 3 return 0; 4 if(root-... 阅读全文

posted @ 2013-11-29 06:34 假日笛声 阅读(178) 评论(0) 推荐(0)

Surrounded Regions [LeetCode]
摘要:Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region .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 阅读全文

posted @ 2013-11-29 06:17 假日笛声 阅读(1884) 评论(1) 推荐(0)

Sort Colors [LeetCode]
摘要:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You are not suppose to use the l 阅读全文

posted @ 2013-11-29 05:16 假日笛声 阅读(244) 评论(0) 推荐(0)

Remove Duplicates from Sorted Array II [LeetCode]
摘要:Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3].Solution: 1 int removeDuplicates(int A[], int n) { 2 if(n = 2){17 //remove A[i]18 ... 阅读全文

posted @ 2013-11-27 15:50 假日笛声 阅读(164) 评论(0) 推荐(0)

Flatten Binary Tree to Linked List [LeetCode]
摘要: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 \ 6click to show hints.Hints:If y... 阅读全文

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

Best Time to Buy and Sell Stock III [LeetCode]
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).Solution: D 阅读全文

posted @ 2013-11-27 14:28 假日笛声 阅读(197) 评论(0) 推荐(0)

Copy List with Random Pointer [LeetCode]
摘要:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.Solution: Uses map to recorde node mapping between old linked list and new linked list. 1 RandomListNode *copyRandomList(RandomListNod... 阅读全文

posted @ 2013-11-27 12:45 假日笛声 阅读(175) 评论(0) 推荐(0)

Validate Binary Search Tree [LeetCode]
摘要: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 left and ri 阅读全文

posted @ 2013-11-26 14:28 假日笛声 阅读(225) 评论(0) 推荐(0)

Rotate Image [LeetCode]
摘要:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?Solution: 1 void rotate(vector > &matrix) { 2 int n = matrix.size(); 3 for(int i = 0; i < n / 2; i ++) { 4 for(int j = i; j < n - 1 - i; ... 阅读全文

posted @ 2013-11-26 13:25 假日笛声 阅读(533) 评论(0) 推荐(0)

Add two numbers [LeetCode]
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8Summary: Be careful a 阅读全文

posted @ 2013-11-25 15:51 假日笛声 阅读(181) 评论(0) 推荐(0)

Implement strStr() [LeetCode]
摘要:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.Summary: be careful about the corner case, haystack = "", needle = "" 1 char *strStr(char *haystack, char *needle) { 2 if(haystack[0] == '\0' && 阅读全文

posted @ 2013-11-23 15:34 假日笛声 阅读(171) 评论(0) 推荐(0)

Recover Binary Search Tree [LeetCode]
摘要:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?Sulotion: Considers a BST as a storted array, so we can traverse the BST inorderly 阅读全文

posted @ 2013-11-21 15:33 假日笛声 阅读(211) 评论(0) 推荐(0)

Minimum Path Sum [LeetCode]
摘要:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.Summary: DP, calucate the minimun sum from start point to every cell unitil we traverse ever 阅读全文

posted @ 2013-11-21 15:08 假日笛声 阅读(429) 评论(0) 推荐(0)

Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]
摘要:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Solution: Just do it recursively. 1 TreeNode *build(vector &preorder, int pstart, int pend, vector &inorder, int istart, int iend) { 2 TreeNode * root = ... 阅读全文

posted @ 2013-11-21 12:09 假日笛声 阅读(194) 评论(0) 推荐(0)

LRU Cache [LeetCode]
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not 阅读全文

posted @ 2013-11-18 06:21 假日笛声 阅读(553) 评论(0) 推荐(0)

Search Insert Position [LeetCode]
摘要:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0Summary: basic bin 阅读全文

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

Gas Station [LeetCode]
摘要:There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[i]of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.Return the starting gas 阅读全文

posted @ 2013-11-15 13:32 假日笛声 阅读(545) 评论(0) 推荐(0)

Permutation Sequence [LeetCode]
摘要:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence.Not 阅读全文

posted @ 2013-11-14 16:27 假日笛声 阅读(696) 评论(0) 推荐(0)

Binary Tree Level Order Traversal II [LeetCode]
摘要: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], [3],]BFS ... 阅读全文

posted @ 2013-11-12 12:46 假日笛声 阅读(535) 评论(0) 推荐(0)

Remove Duplicates from Sorted List II [LeetCode]
摘要: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.Solution: 1 ListNode *deleteDuplicates(ListNode *head 阅读全文

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

Valid Palindrome [LeetCode]
摘要:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome.Note:Have you consider that the string might be empty? This is a good question to 阅读全文

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

Merge Sorted Array [LeetCode]
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.Summary: It's easy, but should be careful about corner cases, like m is 0 阅读全文

posted @ 2013-11-10 05:53 假日笛声 阅读(371) 评论(0) 推荐(0)

Binary Tree Postorder Traversal
摘要:Summary: Basic staff, need to check if goes up along the tree, that's the difference to pre-order traverse. 1 vector postorderTraversal(TreeNode *root) { 2 vector seq; 3 if(root == NULL) 4 return seq; 5 stack nodes; 6 nodes.push(root); 7 TreeNo... 阅读全文

posted @ 2013-11-09 15:50 假日笛声 阅读(168) 评论(0) 推荐(0)

Subsets [LeetCode]
摘要:Given a set of distinct integers,S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,IfS=[1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]Summary: Recursive approach, ... 阅读全文

posted @ 2013-11-09 15:18 假日笛声 阅读(189) 评论(0) 推荐(0)

Search for a Range [LeetCode]
摘要: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].Summary: 阅读全文

posted @ 2013-11-09 14:50 假日笛声 阅读(853) 评论(0) 推荐(0)

Reorder List [LeetCode]
摘要:Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}.Solution: make sure the last element points to NULL in the new list; 1 void reorderList(ListNode *head) 阅读全文

posted @ 2013-11-09 13:54 假日笛声 阅读(399) 评论(0) 推荐(0)

Unique Binary Search Trees [LeetCode]
摘要: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 1 ... 阅读全文

posted @ 2013-11-08 14:46 假日笛声 阅读(173) 评论(0) 推荐(0)

Longest Consecutive Sequence [LeetCode]
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity.Summary: The key idea is using hash map 阅读全文

posted @ 2013-11-08 12:29 假日笛声 阅读(174) 评论(0) 推荐(0)

Remove Nth Node From End of List [LeetCode]
摘要:Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note:Givennwill always be valid.Try to do this in one pass.Su 阅读全文

posted @ 2013-11-07 14:38 假日笛声 阅读(170) 评论(0) 推荐(0)

ZigZag Conversion [LeetCode]
摘要:The string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I RAnd then read line by line:"PAHNAPLSIIGYIR"Write the code that will take a string and 阅读全文

posted @ 2013-11-07 13:45 假日笛声 阅读(558) 评论(0) 推荐(0)

Sudoku Solver [LeetCode]
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be only one unique solution.A sudoku puzzle......and its solution numbers marked in red.Summary: finds the cell which has the least candidates firs 阅读全文

posted @ 2013-11-07 12:52 假日笛声 阅读(605) 评论(0) 推荐(0)

Populating Next Right Pointers in Each Node [LeetCode]
摘要: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 toNULL.N... 阅读全文

posted @ 2013-11-07 09:24 假日笛声 阅读(182) 评论(0) 推荐(0)

Binary Tree Level Order Traversal [LeetCode]
摘要: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]]Solution: 1 vector > levelOrder(TreeNo... 阅读全文

posted @ 2013-11-06 15:53 假日笛声 阅读(168) 评论(0) 推荐(0)

Clone Graph [LeetCode]
摘要:Clone an undirected graph. Each node in the graph contains alabeland a list of itsneighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use#as a separator for each node, and,as a separator for node label and each neighbor of the node.As an example, consider the serialized g 阅读全文

posted @ 2013-11-06 15:38 假日笛声 阅读(174) 评论(0) 推荐(0)

Merge k Sorted Lists [LeetCode]
摘要:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.Summary: Finds the smallest node every round, then links it to the one sorted list. 1 ListNode *mergeKLists(vector &lists) { 2 ListNode * head = NULL; 3 ListNode * pre_node = NULL; 4 ... 阅读全文

posted @ 2013-11-06 13:28 假日笛声 阅读(167) 评论(0) 推荐(0)

Combinations [LeetCode]
摘要:Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]Summary: recursive functions 1 class Solution { 2 public: 3 vector > combine(int n, int k) { 4 vector > combinati... 阅读全文

posted @ 2013-11-05 15:14 假日笛声 阅读(534) 评论(0) 推荐(0)

Count and Say [LeetCode]
摘要:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of inte 阅读全文

posted @ 2013-11-05 14:00 假日笛声 阅读(582) 评论(0) 推荐(0)

Remove Duplicates from Sorted Array [LeetCode]
摘要:Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文

posted @ 2013-11-05 13:27 假日笛声 阅读(125) 评论(0) 推荐(0)

Search in Rotated Sorted Array II [LeetCode]
摘要:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.Summary:Almost the same toSearch in Rotated Sorted Array without duplicates, but careful about 阅读全文

posted @ 2013-11-05 13:02 假日笛声 阅读(559) 评论(0) 推荐(0)

Search in Rotated Sorted Array [LeetCode]
摘要: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.Summary:Binary search can apply 阅读全文

posted @ 2013-11-05 12:24 假日笛声 阅读(356) 评论(0) 推荐(0)

Convert Sorted Array to Binary Search Tree [LeetCode]
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Summary: pretty easy, recursive staff. 1 class Solution { 2 public: 3 TreeNode *sortedArrayToBST(vector &num) { 4 int size = num.size(); 5 if(size == 0) 6 return NULL; 7 ... 阅读全文

posted @ 2013-11-03 06:51 假日笛声 阅读(202) 评论(0) 推荐(0)

Reverse Integer [LeetCode]
摘要:Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integer's last digit is 0, what should 阅读全文

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

Roman to Integer [LeetCode]
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Summary: When meeting C/X/I, remembers to search forward to check if there is a bigger number at the front. 1 int romanToInt(string s) { 2 if(s.size() == 0) 3 return 0; 4... 阅读全文

posted @ 2013-11-02 13:37 假日笛声 阅读(410) 评论(0) 推荐(0)

Unique Paths II [LeetCode]
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1, 阅读全文

posted @ 2013-11-02 12:38 假日笛声 阅读(216) 评论(0) 推荐(0)

Unique Paths [LeetCode]
摘要:A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni 阅读全文

posted @ 2013-11-02 12:16 假日笛声 阅读(210) 评论(0) 推荐(0)