
LeetCode
快乐LeetCode
_我们的存在
这个作者很懒,什么都没留下…
展开
-
LeetCode 236 Lowest Common Ancestor of a Binary Tree
题目描述Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v原创 2015-12-12 09:42:16 · 570 阅读 · 0 评论 -
LeetCode 232 Implement Queue using Stacks
题目描述Implement the following operations of a queue using stacks.push(x) – Push element x to the back of queue.pop() – Removes the element from in front of queue.peek() – Get the front element.empty(原创 2015-12-11 10:29:45 · 549 阅读 · 0 评论 -
LeetCode 213 House Robber II
题目描述Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time,原创 2015-12-08 11:21:19 · 1351 阅读 · 0 评论 -
LeetCode 207 Course Schedule
题目描述There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a p原创 2015-12-08 09:35:28 · 1588 阅读 · 0 评论 -
LeetCode 202 Happy Number
题目描述Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2015-12-07 08:58:06 · 1959 阅读 · 0 评论 -
LeetCode 198 House Robber
题目描述You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent ho原创 2015-12-04 20:55:27 · 1380 阅读 · 1 评论 -
LeetCode 100 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.分析用递归,首先要判断输入的两原创 2015-11-26 08:44:21 · 660 阅读 · 0 评论 -
LeetCode 064 Minimum Path Sum
题目描述Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2015-11-22 17:22:25 · 3089 阅读 · 0 评论 -
LeetCode 063 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 is marked as 1 and 0 respectively in the grid.Fo原创 2015-11-22 17:02:46 · 3009 阅读 · 0 评论 -
LeetCode 062 Unique Paths
题目描述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 right at any point in time. The robot is trying to reach the b原创 2015-11-22 16:40:41 · 825 阅读 · 0 评论 -
LeetCode 061 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.代码 public static ListNode rotateRig原创 2015-11-22 16:17:39 · 580 阅读 · 0 评论 -
LeetCode 060 Permutation Sequence
题目描述The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3):“123”“132”“213”“231”“312原创 2015-11-22 13:18:39 · 460 阅读 · 0 评论 -
LeetCode 059 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 ], [原创 2015-11-22 11:36:31 · 592 阅读 · 0 评论 -
LeetCode 058 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 define原创 2015-11-22 11:22:55 · 476 阅读 · 0 评论 -
LeetCode 055 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 you原创 2015-11-21 20:56:37 · 643 阅读 · 0 评论 -
LeetCode 054 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 ] ]You原创 2015-11-21 20:30:43 · 650 阅读 · 0 评论 -
LeetCode 053 Maximum Subarray
题目描述Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has原创 2015-11-21 16:56:48 · 873 阅读 · 0 评论 -
LeetCode 050 Pow(x, n)
题目描述Implement pow(x, n).代码 public double myPow(double x, int n) { if (n < 0) { return 1/pow(x, -n); } else { return pow(x, n); } } private doub原创 2015-11-11 13:52:16 · 450 阅读 · 0 评论 -
LeetCode 048 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?分析代码public static void rotate(int[][] matrix) { if (matri原创 2015-11-11 10:29:28 · 772 阅读 · 0 评论 -
LeetCode 047 Permutations II
题目描述Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].分析使原创 2015-11-11 10:16:26 · 527 阅读 · 0 评论 -
LeetCode 046 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].分析代码 public List<原创 2015-11-11 10:00:28 · 941 阅读 · 0 评论 -
LeetCode 043 Multiply Strings
题目描述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.分析代码1 public String multiply(String num原创 2015-11-11 09:38:13 · 601 阅读 · 0 评论 -
LeetCode 040 Combination Sum II
题目描述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 number in C may only be used once in the combinatio原创 2015-11-10 10:30:50 · 684 阅读 · 0 评论 -
LeetCode 039 Combination Sum
题目描述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 repeated number may be chosen from C unlimited number原创 2015-11-10 10:24:43 · 919 阅读 · 0 评论 -
LeetCode 038 Count and Say
题目描述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 as “two 1s” or 21. 21 is read off as “one 2,原创 2015-11-10 09:51:24 · 408 阅读 · 0 评论 -
LeetCode 036 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 the character ‘.’.Note: A valid Sudoku board原创 2015-11-10 09:35:07 · 506 阅读 · 0 评论 -
LeetCode 035 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.H原创 2015-11-10 08:59:44 · 658 阅读 · 0 评论 -
LeetCode 034 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 th原创 2015-11-10 08:54:05 · 660 阅读 · 0 评论 -
LeetCode 031 Next Permutation
题目描述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原创 2015-11-10 08:21:17 · 644 阅读 · 0 评论 -
LeetCode 029 Divide Two Integers
题目描述Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.代码 public int divide(int dividend, int divisor) { if (divisor == 0) {原创 2015-11-06 16:32:30 · 474 阅读 · 0 评论 -
LeetCode 028 Implement strStr()
题目描述Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.代码原创 2015-11-06 16:08:11 · 434 阅读 · 0 评论 -
LeetCode 027 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.代码原创 2015-11-06 16:01:23 · 652 阅读 · 0 评论 -
LeetCode 026 Remove Duplicates from Sorted Array
题目描述Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2015-11-06 15:56:40 · 652 阅读 · 0 评论 -
LeetCode 024 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 algorithm should use only constant space. You ma原创 2015-11-05 15:19:56 · 340 阅读 · 0 评论 -
LeetCode 022 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:“((()))”, “(()())”, “(())()”, “()(())”, “()()()”代码原创 2015-11-05 15:02:27 · 459 阅读 · 0 评论 -
LeetCode 021 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.代码 public ListNode mergeTwoLists(ListNode l1, ListNod原创 2015-11-05 14:53:14 · 838 阅读 · 0 评论 -
LeetCode 020 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 valid b原创 2015-11-04 09:43:33 · 401 阅读 · 0 评论 -
LeetCode 019 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 linked lis原创 2015-11-04 09:32:30 · 428 阅读 · 0 评论 -
LeetCode 018 4Sum
题目描述Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: Elements in a原创 2015-11-04 09:14:29 · 673 阅读 · 0 评论 -
LeetCode 017 Letter Combinations of a Phone Number
题目描述Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string “2原创 2015-11-04 08:57:57 · 466 阅读 · 0 评论