LeetCode
快乐LeetCode
_我们的存在
这个作者很懒,什么都没留下…
展开
-
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 · 532 阅读 · 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 · 2907 阅读 · 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 · 2859 阅读 · 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 · 711 阅读 · 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 · 514 阅读 · 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 · 426 阅读 · 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 · 503 阅读 · 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 · 424 阅读 · 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 · 542 阅读 · 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 · 536 阅读 · 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 · 755 阅读 · 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 · 425 阅读 · 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 · 686 阅读 · 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 · 478 阅读 · 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 · 787 阅读 · 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 · 541 阅读 · 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 · 575 阅读 · 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 · 795 阅读 · 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 · 360 阅读 · 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 · 471 阅读 · 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 · 534 阅读 · 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 · 528 阅读 · 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 · 559 阅读 · 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 · 380 阅读 · 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 · 387 阅读 · 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 · 527 阅读 · 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 · 554 阅读 · 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 · 308 阅读 · 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 · 397 阅读 · 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 · 734 阅读 · 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 · 364 阅读 · 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 · 393 阅读 · 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 · 586 阅读 · 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 · 420 阅读 · 0 评论 -
LeetCode 016 3Sum Closest
题目描述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. You may assume that each input would have exac原创 2015-11-04 08:36:50 · 618 阅读 · 0 评论 -
LeetCode 015 3Sum
题目描述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 zero.Note: Elements in a triplet (a,b,c) must原创 2015-11-03 21:15:59 · 755 阅读 · 0 评论 -
LeetCode 014 Longest Common Prefix
题目描述Write a function to find the longest common prefix string amongst an array of strings.代码 public static String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) {原创 2015-11-03 20:42:10 · 408 阅读 · 0 评论 -
LeetCode 012 Integer to Roman
题目描述Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.分析罗马数字的计数方法 基本字符 I V X L C D M 阿拉伯数字表示 1 5 10 50 100 500 100原创 2015-11-02 10:19:00 · 505 阅读 · 0 评论 -
LeetCode 011 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). Find two原创 2015-11-02 09:54:59 · 405 阅读 · 0 评论 -
LeetCode 009 Palindrome Number
题目描述Determine whether an integer is a palindrome. Do this without extra space.代码 public static boolean isPalindrome(int x) { if (x < 0) { return false; } if (x >= 0原创 2015-10-29 10:19:54 · 506 阅读 · 0 评论