01 2013 档案

逻辑题汇总
摘要:1. 黑帽白帽有一个牢房,有3个犯人关在其中。因为玻璃很厚,所以3个人只能互相看见,不能听到对方说话的声音。”有一天,国王想了一个办法,给他们每个人头上都戴了一顶帽子,只叫他们知道帽子的颜色不是白的就是黑的,不叫他们知道自己所戴帽子的是什么颜色的。在这种情况下,国王宣布两条如下: 1.谁能看到其他... 阅读全文

posted @ 2013-01-31 05:15 小刀初试 阅读(210) 评论(0) 推荐(0)

[FaceBook] Hanoi Moves, Solution
摘要:There are K pegs. Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius... 阅读全文

posted @ 2013-01-29 10:50 小刀初试 阅读(150) 评论(0) 推荐(0)

[LeetCode] Decode Ways, Solution
摘要:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message con... 阅读全文

posted @ 2013-01-28 13:35 小刀初试 阅读(178) 评论(0) 推荐(0)

[LeetCode] Count and Say, Solution
摘要:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off ... 阅读全文

posted @ 2013-01-28 13:09 小刀初试 阅读(136) 评论(0) 推荐(0)

[LeetCode] Convert Sorted List to Binary Search Tree, Solution
摘要:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.» Solve this problem[Thoughts]It is simil... 阅读全文

posted @ 2013-01-28 12:28 小刀初试 阅读(162) 评论(0) 推荐(0)

[LeetCode] Container With Most Water, Solution
摘要:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpo... 阅读全文

posted @ 2013-01-28 11:35 小刀初试 阅读(136) 评论(0) 推荐(0)

[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal, Solution
摘要:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.» Solve this pr... 阅读全文

posted @ 2013-01-28 10:50 小刀初试 阅读(125) 评论(0) 推荐(0)

[LeetCode] Combinations, Solution
摘要:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4... 阅读全文

posted @ 2013-01-27 10:37 小刀初试 阅读(128) 评论(0) 推荐(0)

[LeetCode] Combination Sum II, Solution
摘要:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each numb... 阅读全文

posted @ 2013-01-27 08:13 小刀初试 阅读(140) 评论(0) 推荐(0)

[LeetCode] Combination Sum, Solution
摘要:Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeate... 阅读全文

posted @ 2013-01-27 08:06 小刀初试 阅读(135) 评论(0) 推荐(0)

[LeetCode] Climbing Stairs, Solution
摘要:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you cli... 阅读全文

posted @ 2013-01-27 07:14 小刀初试 阅读(112) 评论(0) 推荐(0)

[LeetCode] Add Two Numbers, Solution
摘要: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 ... 阅读全文

posted @ 2013-01-27 05:46 小刀初试 阅读(147) 评论(0) 推荐(0)

[LeetCode] 3Sum Closest, Solution
摘要:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers.... 阅读全文

posted @ 2013-01-26 06:05 小刀初试 阅读(151) 评论(0) 推荐(0)

[LeetCode] 3 Sum, Solution
摘要:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of ... 阅读全文

posted @ 2013-01-26 05:48 小刀初试 阅读(168) 评论(0) 推荐(0)

[LeetCode] Binary Tree Maximum Path Sum Solution
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 ... 阅读全文

posted @ 2013-01-21 13:02 小刀初试 阅读(120) 评论(0) 推荐(0)

[LeetCode] Binary Tree Level Order Traversal Solution
摘要:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,... 阅读全文

posted @ 2013-01-18 15:30 小刀初试 阅读(137) 评论(0) 推荐(0)

[LeetCode] Binary Tree Inorder Traversal Solution
摘要:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].... 阅读全文

posted @ 2013-01-17 15:15 小刀初试 阅读(163) 评论(0) 推荐(0)

[LeetCode] Balanced Binary Tree Solution
摘要: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... 阅读全文

posted @ 2013-01-17 14:47 小刀初试 阅读(176) 评论(0) 推荐(0)

[LeetCode] Best Time to Buy and Sell Stock III Solution
摘要:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complet... 阅读全文

posted @ 2013-01-17 13:52 小刀初试 阅读(134) 评论(0) 推荐(0)

[LeetCode] Best Time to Buy and Sell Stock II Solution
摘要:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complet... 阅读全文

posted @ 2013-01-17 13:22 小刀初试 阅读(147) 评论(0) 推荐(0)

[LeetCode] Best Time to Buy and Sell Stock Solution
摘要:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction ... 阅读全文

posted @ 2013-01-17 12:48 小刀初试 阅读(156) 评论(0) 推荐(0)

[LeetCode] Anagrams 解题报告
摘要:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.» Solve this problem[解题思路]对每一个字符串取水印,... 阅读全文

posted @ 2013-01-16 13:50 小刀初试 阅读(220) 评论(0) 推荐(0)

[LeetCode] Add Binary 解题报告
摘要:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".» Solve this problem[解题思路]典型的实现题。没什么可说的。[Cod... 阅读全文

posted @ 2013-01-16 12:19 小刀初试 阅读(151) 评论(0) 推荐(0)

[LeetCode] ZigZag Conversion 解题报告
摘要: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 ... 阅读全文

posted @ 2013-01-15 11:29 小刀初试 阅读(194) 评论(0) 推荐(0)

[LeetCode] Word Search 解题报告
摘要: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 "adjace... 阅读全文

posted @ 2013-01-15 10:21 小刀初试 阅读(142) 评论(0) 推荐(0)

[LeetCode] Wildcard Matching, Solution
摘要:Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including th... 阅读全文

posted @ 2013-01-13 14:43 小刀初试 阅读(141) 评论(0) 推荐(0)

[LeetCode] Validate Binary Search Tree 解题报告
摘要: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 n... 阅读全文

posted @ 2013-01-13 13:42 小刀初试 阅读(262) 评论(0) 推荐(0)

[LeetCode] Valid Sudoku 解题报告
摘要:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with... 阅读全文

posted @ 2013-01-12 14:20 小刀初试 阅读(153) 评论(0) 推荐(0)

[LeetCode] Valid Parentheses 解题报告
摘要:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the c... 阅读全文

posted @ 2013-01-12 13:31 小刀初试 阅读(155) 评论(0) 推荐(0)

[LeetCode] Unique Paths II 解题报告
摘要: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 i... 阅读全文

posted @ 2013-01-11 14:18 小刀初试 阅读(159) 评论(0) 推荐(0)

[LeetCode] Unique Paths 解题报告
摘要:Ranking: **A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or righ... 阅读全文

posted @ 2013-01-11 13:23 小刀初试 阅读(154) 评论(0) 推荐(0)

[LeetCode] Triangle 解题报告
摘要: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 fol... 阅读全文

posted @ 2013-01-10 15:46 小刀初试 阅读(171) 评论(0) 推荐(0)

[LeetCode] Trapping Rain Water 解题报告
摘要:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.... 阅读全文

posted @ 2013-01-10 14:36 小刀初试 阅读(158) 评论(0) 推荐(0)

[LeetCode] Text Justification 解题报告
摘要:Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You shoul... 阅读全文

posted @ 2013-01-09 15:18 小刀初试 阅读(187) 评论(0) 推荐(0)

[LeetCode] Symmetric Tree 解题报告
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ ... 阅读全文

posted @ 2013-01-08 12:40 小刀初试 阅读(252) 评论(0) 推荐(0)

[LeetCode] Swap Nodes in Pairs 解题报告
摘要:Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your alg... 阅读全文

posted @ 2013-01-08 12:22 小刀初试 阅读(142) 评论(0) 推荐(0)

[LeetCode] Substring with Concatenation of All Words 解题报告
摘要:You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatena... 阅读全文

posted @ 2013-01-08 11:59 小刀初试 阅读(215) 评论(0) 推荐(0)

[LeetCode] Subsets II 解题报告
摘要:Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order... 阅读全文

posted @ 2013-01-07 08:14 小刀初试 阅读(169) 评论(0) 推荐(0)

[LeetCode] Subsets 解题报告
摘要: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 c... 阅读全文

posted @ 2013-01-07 08:03 小刀初试 阅读(203) 评论(0) 推荐(0)

[LeetCode] String to Integer (atoi) 解题报告
摘要:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below a... 阅读全文

posted @ 2013-01-07 06:39 小刀初试 阅读(131) 评论(0) 推荐(0)

[LeetCode] Sqrt(x) 解题报告
摘要:Implement int sqrt(int x).Compute and return the square root of x.» Solve this problem[解题思路]二分法。但是这题有意思的是,二分过程中终止条件的确认。因为整数的乘法有可能导致溢出,而这种溢出的检测跟整数加法直接判... 阅读全文

posted @ 2013-01-07 05:29 小刀初试 阅读(454) 评论(0) 推荐(0)

[LeetCode] Spiral Matrix 解题报告
摘要:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, ... 阅读全文

posted @ 2013-01-06 08:32 小刀初试 阅读(190) 评论(0) 推荐(0)

[LeetCode] Sort Colors 解题报告
摘要:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, ... 阅读全文

posted @ 2013-01-06 07:37 小刀初试 阅读(163) 评论(0) 推荐(0)

[LeetCode] Simplify Path 解题报告
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did y... 阅读全文

posted @ 2013-01-06 06:57 小刀初试 阅读(164) 评论(0) 推荐(0)

[LeetCode] Set Matrix Zeroes 解题报告
摘要:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight forward solu... 阅读全文

posted @ 2013-01-06 06:22 小刀初试 阅读(179) 评论(0) 推荐(0)

[LeetCode] Search Insert Position 解题报告
摘要: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 or... 阅读全文

posted @ 2013-01-04 13:56 小刀初试 阅读(243) 评论(0) 推荐(0)

[LeetCode] Search in Rotated Sorted Array II 解题报告
摘要:Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function ... 阅读全文

posted @ 2013-01-04 13:38 小刀初试 阅读(147) 评论(0) 推荐(0)

[LeetCode] Search in Rotated Sorted Array 解题报告
摘要:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target valu... 阅读全文

posted @ 2013-01-04 13:23 小刀初试 阅读(149) 评论(0) 推荐(0)

[LeetCode] Search for a Range 解题报告
摘要: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 ord... 阅读全文

posted @ 2013-01-04 11:36 小刀初试 阅读(126) 评论(0) 推荐(0)

[LeetCode] Scramble String 解题报告
摘要:Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representatio... 阅读全文

posted @ 2013-01-03 06:29 小刀初试 阅读(186) 评论(0) 推荐(0)

[LeetCode] Rotate List 解题报告
摘要:Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->... 阅读全文

posted @ 2013-01-02 08:22 小刀初试 阅读(136) 评论(0) 推荐(0)

[LeetCode] Rotate Image 解题报告
摘要:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?» Solve this pr... 阅读全文

posted @ 2013-01-02 07:49 小刀初试 阅读(104) 评论(0) 推荐(0)

[LeetCode] Roman To Integer 解题报告
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.» Solve this problem[解题思路]从前往后扫描,用一个临时变量记录分段... 阅读全文

posted @ 2013-01-01 13:34 小刀初试 阅读(131) 评论(0) 推荐(0)

[LeetCode] Reverse Nodes in k-Group 解题报告
摘要:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then le... 阅读全文

posted @ 2013-01-01 13:14 小刀初试 阅读(123) 评论(0) 推荐(0)

[LeetCode] Reverse Linked List II 解题报告
摘要:Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5... 阅读全文

posted @ 2013-01-01 08:47 小刀初试 阅读(155) 评论(0) 推荐(0)

[LeetCode] Reverse Integer 解题报告
摘要:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are s... 阅读全文

posted @ 2013-01-01 07:52 小刀初试 阅读(163) 评论(0) 推荐(0)

[LeetCode] Restore IP Addresses 解题报告
摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255... 阅读全文

posted @ 2013-01-01 07:33 小刀初试 阅读(190) 评论(0) 推荐(0)