- 博客(48)
- 收藏
- 关注
原创 Climbing Stairs
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 climb to the top?思路: dp[i] = dp[i-1] + dp[i-2]...
2014-01-29 15:25:52 489
原创 Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if yo...
2014-01-29 13:52:11 571
原创 Word Ladder
Given two words (beginWordandendWord), and a dictionary's word list, find the length of shortest transformation sequence frombeginWordtoendWord, such that:Only one letter can be changed at a tim...
2014-01-28 15:31:54 782
原创 Recover Binary Search Tree
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
2014-01-27 16:08:47 539
原创 Merge Sorted Array
Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:The number of elements initialized innums1andnums2aremandnrespectively. You may assume tha...
2014-01-27 14:37:12 611
原创 Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.思路: 实际上考察的是mergesort里面的merge一步,很简单直接的merge,注意其中一个没有了的情况。一...
2014-01-27 13:56:37 533
原创 Merge Intervals
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].思路:主要注意comparator怎么写,还有新建一个interval的时候,要同时算两个interval的最小...
2014-01-26 09:07:00 592
原创 Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving ofs1 ands2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false....
2014-01-24 15:38:44 555
原创 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 value to search. If found in the array return its ...
2014-01-23 15:28:15 568
原创 Binary Tree Zigzag Level Order Traversal
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 tre...
2014-01-23 14:40:34 534
原创 Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal 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}, ...
2014-01-23 14:09:16 450
原创 Permutations
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].思路:经典的backtracking. 不要...
2014-01-23 12:55:36 569
原创 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()(...
2014-01-22 13:37:44 504
原创 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.思路:inorder: left, current, right;postorder, left, ri...
2014-01-22 13:02:32 550
原创 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.思路:注意current = preorder[pstart],不是preorder[0];preorder:...
2014-01-22 12:32:30 634
原创 Partition List
Given a linked list and a value x, partition it such that all nodes less thanx come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of
2014-01-21 14:34:55 472
原创 Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.思路:因为罗马数字中只有4,9是左减,其余的数字都是右加。所以,构建两个数组,分别是数字和对应的字母。数字,要把所有的4,9加进来。包括4,9,40,90,400,
2014-01-21 10:07:02 447
原创 Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.思路:首先,学习一下罗马数字,参考罗马数字罗马数字是最古老的数字表示方式,比阿拉伯数组早2000多年,起源于罗马罗马数字有如下符号:
2014-01-21 09:33:52 461
原创 Group Anagrams
Given an array of strings, group anagrams together.Example:Input: ["eat", "tea", "tan", "ate", "nat", "bat"],Output:[ ["ate","eat","tea"], ["nat"
2014-01-20 14:45:04 526
原创 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 and ask yourself what are the possible input case
2014-01-20 13:30:16 547
原创 Sqrt(x)
Implement intsqrt(int x).Compute and return the square root ofx.ExampleExample 1: Input: 0 Output: 0Example 2: Input: 3 Output: 1 Explanation: return the largest integer y that y...
2014-01-20 11:07:45 611
原创 Search a 2D matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of e...
2014-01-20 08:05:11 528
原创 First Missing Positive
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant spa
2014-01-19 16:04:20 492
原创 pow(x, n).
Implement pow(x, n).思路:用变量temp来存储值,这样可以避免重复计算。注意判断n < 0 还有n 为奇偶的情况。public class Solution { /** * @param x: the base number * @param n: the power number * @return: the result...
2014-01-19 14:49:19 520
原创 Binary Tree Maximum Path Sum
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 / \ 2 3Return 6.思路: 主要看
2014-01-19 13:51:15 557
原创 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 order of O(log n).If the target is not found in
2014-01-19 12:42:00 493
原创 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.T...
2014-01-19 07:41:46 541
原创 Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defi
2014-01-17 15:37:27 454
原创 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 nodes with keys less than the node's key. The ...
2014-01-17 13:18:43 626
原创 Binary Tree Inorder Traversal
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].Note: Recursive solution is trivial...
2014-01-17 12:28:20 1008
原创 Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum =...
2014-01-16 13:46:44 500
原创 Remove Nth Node From End of List
Given a linked list, remove the nth node 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 l
2014-01-16 13:02:19 430
原创 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 / \ 2 2 / \ / \3 4 4 3But the followin...
2014-01-15 14:40:22 479
原创 Binary Tree Level Order Traversal
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,9,20,#,#,15,7}, 3 / \ 9 20 / \...
2014-01-13 13:41:34 608
原创 Balanced Binary Tree
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 diff...
2014-01-12 16:17:08 469
原创 Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.思路:左边右边一起比较...
2014-01-12 15:50:49 440
原创 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->NULL.思路:首先算...
2014-01-12 15:23:39 462
原创 Flatten Binary Tree to Linked List
Flatten a binary tree to a fake "linked list" in pre-order traversal.Here we use therightpointer in TreeNode as thenextpointer in ListNode.ExampleExample 1:Input:{1,2,5,3,4,#,6}Output:{1...
2014-01-12 14:35:48 547
原创 Surrounded Region
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 X...
2014-01-12 13:41:13 639
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人