
LeetCode
杜鲁门
keep moving
展开
-
[LeetCode]--75. 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, white and blue.Here, we will use the integers 0, 1,原创 2017-01-11 09:44:35 · 451 阅读 · 1 评论 -
[LeetCode]--74. 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 row is原创 2017-01-10 23:50:27 · 321 阅读 · 0 评论 -
[LeetCode]--73. 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.click to show follow up.Follow up: Did you use extra space? A straight forward solution using O(mn) space原创 2017-01-10 17:17:01 · 278 阅读 · 0 评论 -
[LeetCode]--71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” click to show corner cases.Corner Cases: Did you consider the原创 2016-12-04 21:44:17 · 329 阅读 · 0 评论 -
[LeetCode]--69. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.我采用的是二分法。每次折中求平方,如果大了就把中值赋给大的,如果小了就把中值赋给小的。public int mySqrt(int x) { long start = 1, end = x; while (start + 1 < end)原创 2016-12-04 15:34:42 · 443 阅读 · 0 评论 -
[LeetCode]--64. 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 any原创 2016-12-04 15:16:21 · 284 阅读 · 0 评论 -
[LeetCode]--63. 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.For ex原创 2016-12-03 17:06:11 · 432 阅读 · 0 评论 -
[LeetCode]--62. 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 botto原创 2016-11-06 11:39:12 · 480 阅读 · 0 评论 -
[LeetCode]--61. 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.我是利用双指针吧,就可以在一次遍历的情况下找到要断开的位置即倒数第几个位置,然后插到头上原创 2016-11-06 10:29:43 · 305 阅读 · 0 评论 -
[LeetCode]--60. 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):1."123"2."132"3."213"4."231"5原创 2016-11-05 11:00:14 · 293 阅读 · 0 评论 -
[LeetCode]--59. 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 ], [ 7, 6, 5原创 2016-11-05 10:29:29 · 351 阅读 · 0 评论 -
[LeetCode]--55. 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 are原创 2016-11-04 22:02:54 · 395 阅读 · 0 评论 -
[LeetCode]--54. 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 should原创 2016-11-04 17:21:18 · 547 阅读 · 0 评论 -
[LeetCode]--53. 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 the原创 2016-11-04 11:17:21 · 403 阅读 · 0 评论 -
[LeetCode]--50. Pow(x, n)
Implement pow(x, n).其实这个题递归和非递归应该都不难实现,就是边界值的问题。public double myPow(double x, int n) { if (n == 0) return 1.0; double res = 1.0; if (n < 0) { if (x >= 1.原创 2016-11-03 22:06:09 · 325 阅读 · 0 评论 -
[LeetCode]--49. Group Anagrams
Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note: All inputs will原创 2016-11-03 16:50:24 · 331 阅读 · 0 评论 -
[LeetCode]--48. 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?本地使得二维矩阵,旋转90角度。通过实际数据分析,通过两个步骤的元素交换可实现目标:按照主对角线,将对称元素交换 按照列,将对原创 2016-11-03 14:39:57 · 381 阅读 · 0 评论 -
[LeetCode]--47. 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], [2,1,1]][Le原创 2016-11-03 08:41:47 · 354 阅读 · 0 评论 -
[LeetCode]--46. Permutations
Given a collection of distinct 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], [3,2,1]][Leet原创 2016-11-02 22:04:56 · 452 阅读 · 0 评论 -
[LeetCode]--43. 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. Converting the input string to integer is N原创 2016-11-02 18:29:21 · 386 阅读 · 0 评论 -
[LeetCode]--40. 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 combination.No原创 2016-11-01 22:16:43 · 368 阅读 · 0 评论 -
[LeetCode]--39. 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 of t原创 2016-11-01 13:12:03 · 481 阅读 · 0 评论 -
[LeetCode]--35. 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.Here原创 2016-10-31 16:54:03 · 410 阅读 · 0 评论 -
[LeetCode]--34. 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 the ar原创 2016-10-31 16:15:37 · 416 阅读 · 0 评论 -
[LeetCode]--31. 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 orde原创 2016-10-31 15:34:32 · 475 阅读 · 0 评论 -
[LeetCode]--29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.以前我记得做过乘法变加法吧,这个有点像除法变减法,用位运算,二进制嘛,左移一位相当于乘以二。一个有趣的是 Math.abs(-2147483648) 结果还是 -2147483648原创 2016-10-28 10:43:26 · 443 阅读 · 0 评论 -
[LeetCode]--22. 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:[ "((()))", "(()())", "(())()", "()(())", "(原创 2016-10-27 22:03:31 · 424 阅读 · 0 评论 -
[LeetCode]--18. 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: The solution set mu原创 2016-10-25 17:13:18 · 343 阅读 · 0 评论 -
[LeetCode]--17. 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 "23"O原创 2016-10-24 21:13:52 · 554 阅读 · 0 评论 -
[LeetCode]--15. 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: The solution set must not contain duplic原创 2016-10-23 17:34:03 · 562 阅读 · 0 评论 -
[LeetCode]--12. Integer to Roman
[LeetCode]–13. Roman to Integer可以在上面的链接看一下罗马数字的排列规律。然后利用规律,构建数组,把基本的构建数放在数组里面,然后依次判断加进去就行。public class Solution { public String intToRoman(int num) { String[][] roman = { {原创 2016-10-23 11:26:57 · 511 阅读 · 0 评论 -
[LeetCode]--11. 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 lin原创 2016-10-23 10:37:19 · 485 阅读 · 0 评论 -
[LeetCode]--5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.我用的是中心扩展法 因为回文字符串是以中心轴对称的,原创 2016-10-18 20:17:37 · 681 阅读 · 0 评论 -
[LeetCode]--415. Add Strings
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both原创 2016-10-18 15:02:33 · 766 阅读 · 0 评论 -
[LeetCode]--412. Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For n原创 2016-10-18 13:17:15 · 836 阅读 · 0 评论 -
[LeetCode]--409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example “Aa” is not consider原创 2016-10-18 10:35:52 · 993 阅读 · 0 评论 -
[LeetCode]--405. Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.Note:All letters in hexadecimal (a-f) must be in lowercase. The hexadecimal str原创 2016-10-18 09:17:52 · 777 阅读 · 0 评论 -
[LeetCode]--404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.我们递归原创 2016-10-17 22:37:24 · 753 阅读 · 0 评论 -
[LeetCode]--401. Binary Watch(递归有点懵)
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LED represents a zero or one, with the least significant bit on the原创 2016-10-17 17:47:04 · 864 阅读 · 0 评论 -
[LeetCode]--400. Nth Digit
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …Note: n is positive and will fit within the range of a 32-bit signed integer (n < 231).Example 1:Input: 3Out原创 2016-10-17 16:20:30 · 977 阅读 · 0 评论