LeetCode
文章平均质量分 53
wteo
这个作者很懒,什么都没留下…
展开
-
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?有f(n) = f(n - 1) + f(n原创 2014-07-03 21:10:10 · 1186 阅读 · 0 评论 -
LeetCode之Pow(x, n)
Implement pow(x, n).Recursive solution is easier to understand. It uses the divide-and-conquer approach, which also runs in time. The formula is shown below:class Solution {public:原创 2014-04-11 14:43:59 · 953 阅读 · 0 评论 -
LeetCode之Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]class Solut原创 2014-04-11 15:31:20 · 847 阅读 · 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 order of O(log n).If the target is not found in t原创 2014-03-26 18:39:14 · 753 阅读 · 0 评论 -
LeetCode之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 each原创 2014-03-26 18:05:21 · 832 阅读 · 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 order.You may assume no duplicates in the array.原创 2014-03-26 17:52:17 · 723 阅读 · 0 评论 -
LeetCode之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-04-05 12:57:57 · 1029 阅读 · 0 评论 -
LeetCode之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-03-24 22:15:05 · 807 阅读 · 0 评论 -
LeetCode之Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary原创 2014-04-05 14:26:37 · 896 阅读 · 0 评论 -
LeetCode之Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total原创 2014-03-24 22:01:50 · 1188 阅读 · 0 评论 -
LeetCode之Minimum Depth of Binary Tree
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.此题与Maximum Depth of Binary Tree有原创 2014-03-24 22:51:15 · 946 阅读 · 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 / \ 2 2 / \ / \3 4 4 3But the f原创 2014-04-05 14:18:18 · 974 阅读 · 0 评论 -
LeetCode之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-04-05 13:12:15 · 904 阅读 · 0 评论 -
LeetCode之Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if原创 2014-04-11 16:10:13 · 766 阅读 · 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.转换规则:wiki/罗马数字拼写规则罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述原创 2014-04-11 13:33:49 · 1301 阅读 · 0 评论 -
LeetCode之Decode Ways
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 containing digits, determine the total numb原创 2014-07-03 23:35:26 · 1052 阅读 · 0 评论 -
Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2014-07-03 20:34:41 · 882 阅读 · 0 评论 -
LeetCode之Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.c原创 2014-03-20 20:31:02 · 667 阅读 · 0 评论 -
LeetCode之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./** * Def原创 2014-03-20 14:25:49 · 651 阅读 · 0 评论 -
LeetCode之Merge Sorted Array
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 (size that is greater or equal to m + n) to hold additional elements from原创 2014-03-20 20:47:23 · 597 阅读 · 0 评论 -
LeetCode之Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?之前的the beginning of the loop也有介绍/**原创 2014-03-22 01:35:05 · 829 阅读 · 0 评论 -
LeetCode之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 of every node never diffe原创 2014-03-20 18:40:47 · 847 阅读 · 0 评论 -
LeetCode之Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive soluti原创 2014-03-20 19:32:01 · 863 阅读 · 0 评论 -
LeetCode之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 solutio原创 2014-03-20 19:54:41 · 904 阅读 · 0 评论 -
LeetCode之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原创 2014-03-20 21:48:59 · 817 阅读 · 0 评论 -
LeetCode之Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting原创 2014-04-11 18:49:17 · 839 阅读 · 0 评论 -
LeetCode之Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?class Soluti原创 2014-04-11 15:56:40 · 805 阅读 · 0 评论 -
LeetCode之Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node./** * Definition for binary原创 2014-03-24 22:28:26 · 1099 阅读 · 0 评论 -
LeetCode之Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2014-04-03 16:02:47 · 1083 阅读 · 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.Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A si原创 2014-03-21 19:46:18 · 804 阅读 · 0 评论 -
LeetCode之Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.多遍LeetCode之Merge Two Sorted Lists思想/** * Definition for singly-linked list. * struct Lis原创 2014-03-22 23:25:33 · 808 阅读 · 0 评论 -
LeetCode之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./** * Definition for singly-linked list. * struct ListN原创 2014-03-22 22:50:09 · 745 阅读 · 0 评论 -
LeetCode之Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2014-03-22 22:32:18 · 642 阅读 · 0 评论 -
LeetCode之Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [原创 2014-03-21 12:54:44 · 686 阅读 · 0 评论 -
LeetCode之Container With Most Water
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 endpoints of line i is at (i, ai) and (i, 0). Fin原创 2014-03-21 14:14:26 · 789 阅读 · 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?和之前写的矩阵翻转算法是一样的。class Solution {public: vo原创 2014-03-21 13:10:07 · 782 阅读 · 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, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2014-03-21 12:36:10 · 893 阅读 · 0 评论 -
LeetCode之Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.注意:越界问题class Solution {public: int sqrt(int x) { if(x<0) return -1; if(x==0||x==1) return x; i原创 2014-03-31 12:10:16 · 983 阅读 · 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 correct order, "()" and "()[]{}" are all vali原创 2014-03-31 15:16:20 · 798 阅读 · 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 to determine if a given target is in the a原创 2014-03-31 13:57:15 · 832 阅读 · 0 评论