
JAVA
星星斋
这个作者很懒,什么都没留下…
展开
-
(Java)LeetCode-29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.刚看这一题时确实没什么思路,还想着一直减减减,后来看到tags里有二分查找,就有思路了。amazing比如说 25/3 这一题,用一个Map存上(3-1)(6-原创 2016-06-14 14:51:54 · 358 阅读 · 0 评论 -
(Java)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 combina原创 2016-08-11 23:33:03 · 1257 阅读 · 0 评论 -
(Java)LeetCode-41. 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原创 2016-09-16 15:07:59 · 550 阅读 · 0 评论 -
(Java)LeetCode-42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2016-09-16 20:22:17 · 325 阅读 · 0 评论 -
(Java)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 integ原创 2016-09-16 22:56:51 · 434 阅读 · 0 评论 -
(Java)LeetCode-44.Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover t原创 2016-09-20 15:46:35 · 901 阅读 · 0 评论 -
(Java)LeetCode-68. Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that i原创 2016-11-02 22:37:03 · 407 阅读 · 0 评论 -
(Java)LeetCode-69. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.这道题么,当然最简单的方法就是一行代码 return(int)Math.sqrt(x);不过出于强迫症,还是用二分法又写了一遍代码。需要注意的地方是循环应该是while(left public int mySq原创 2016-11-03 16:02:19 · 635 阅读 · 0 评论 -
(Java)LeetCode-45. Jump Game II
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.Your goal i原创 2016-09-27 23:15:41 · 335 阅读 · 0 评论 -
(Java)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],原创 2016-09-28 13:11:17 · 293 阅读 · 0 评论 -
(Java)文件分割器
文件分割器原创 2016-08-09 17:39:35 · 436 阅读 · 0 评论 -
(Java)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 numb原创 2016-08-10 22:35:30 · 409 阅读 · 0 评论 -
(Java)LeetCode-32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",原创 2016-08-03 22:56:19 · 252 阅读 · 0 评论 -
(Java)LeetCode-30. Substring with Concatenation of All Words
这道题Hard模式,比较复杂,又没有用到很经典的算法,卡在这道题很长时间了。首先比较直观的解法package datastru;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class Solution {原创 2016-07-31 22:03:21 · 674 阅读 · 0 评论 -
(Java)LeetCode-33. 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 retur原创 2016-08-04 23:14:38 · 450 阅读 · 0 评论 -
(Java)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原创 2016-08-01 23:41:54 · 748 阅读 · 0 评论 -
(Java)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原创 2016-08-05 22:01:05 · 386 阅读 · 0 评论 -
(Java)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.原创 2016-08-07 21:46:08 · 707 阅读 · 0 评论 -
(Java)LeetCode-36. 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 '.'.A partially fille原创 2016-08-08 19:06:34 · 263 阅读 · 0 评论 -
(Java)LeetCode-306. Additive Number
Additive number is a string whose digits can form additive sequence.A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the原创 2016-08-08 20:59:59 · 597 阅读 · 0 评论 -
(Java)LeetCode-37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku原创 2016-08-09 21:36:31 · 766 阅读 · 0 评论 -
(Java)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原创 2016-09-28 13:57:43 · 430 阅读 · 0 评论 -
(Java)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度,要求时间复杂度O(n2),空间复杂度原创 2016-09-29 15:36:17 · 1016 阅读 · 0 评论 -
(Java)LeetCode-70. 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?这道题是一个比较经典比较简单的问题。原创 2016-11-06 16:01:21 · 703 阅读 · 0 评论 -
(Java)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 ], [原创 2016-10-05 22:21:08 · 280 阅读 · 0 评论 -
(Java)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):"123""132""213""231""3原创 2016-10-05 23:52:58 · 914 阅读 · 0 评论 -
(Java)LeetCode-72. Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:原创 2016-11-09 17:03:29 · 560 阅读 · 0 评论 -
(Java)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-10-06 17:52:28 · 949 阅读 · 0 评论 -
(Java)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原创 2016-10-06 21:54:21 · 642 阅读 · 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原创 2016-10-07 15:30:13 · 306 阅读 · 0 评论 -
(Java)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原创 2016-10-07 16:25:49 · 1016 阅读 · 0 评论 -
(Java)LeetCode-65. Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguo原创 2016-10-07 20:41:05 · 422 阅读 · 0 评论 -
(Java)LeetCode-66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.这道题题意刚开始没看明白。。原创 2016-11-02 15:03:05 · 847 阅读 · 0 评论 -
(Java)LeetCode-58. 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原创 2016-10-05 20:56:26 · 279 阅读 · 0 评论 -
(Java)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"Corner Cases:Did you consider the case where path原创 2016-11-09 10:29:53 · 399 阅读 · 0 评论 -
(Java)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: Al原创 2016-09-29 23:06:19 · 244 阅读 · 0 评论 -
(Java)LeetCode-50. Pow(x, n)
Implement pow(x, n).这道题最慢时间复杂度也是O(n),快一些的是O(logn),主要是将n考虑为二进制的形式,某一位是1的话,就乘上相应的次方数即可。代码如下:public class Solution { public double myPow(double x, int n) { if(原创 2016-10-01 17:44:19 · 595 阅读 · 0 评论 -
(Java)LeetCode-51. N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2016-10-01 21:51:19 · 281 阅读 · 0 评论 -
(Java)LeetCode-52. N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.这一题和上一题一样嘛。。代码几乎一模一样,就是返回原来的List的Size而已~代码如下:public cla原创 2016-10-02 13:24:46 · 288 阅读 · 0 评论 -
(Java)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] ha原创 2016-10-03 16:52:38 · 296 阅读 · 0 评论