Leetcode
文章平均质量分 57
草鞋大脚
无他,唯手熟尔
展开
-
[Leetcode] Divide Two Integers (Java)
Divide two integers without using multiplication, division and mod operator.输入两个整数,dividend, divisor,不要使用乘法,除法,取模运算,求出a/b的值。如果每次减divisor,会超时,让sum=b,每次sum翻倍,然后count也翻倍,效率会高点儿public class Divide原创 2013-12-28 18:00:06 · 737 阅读 · 0 评论 -
[Leetcode] Substring with Concatenation of All Words (Java)
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without an原创 2013-12-28 20:18:54 · 745 阅读 · 0 评论 -
[Leetcode] Next Permutation (Java)
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原创 2013-12-28 20:41:13 · 750 阅读 · 0 评论 -
[Leetcode] Implement strStr() (Java)
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.比较字符串,查找haystack中是否包含needle,如果包含则返回haystack中首先出现needle的指针,如果不包含则返回原创 2013-12-28 15:07:37 · 1160 阅读 · 0 评论 -
[Leetcode] Search for a Range (Java)
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原创 2013-12-29 14:21:09 · 1413 阅读 · 0 评论 -
[Leetcode] Search Insert Position (Java)
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.原创 2013-12-29 14:50:05 · 646 阅读 · 0 评论 -
[Leetcode] Longest Valid Parentheses (Java)
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 "()",原创 2013-12-29 12:02:57 · 532 阅读 · 0 评论 -
[Leetcode] Valid Sudoku (Java)
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原创 2013-12-29 18:32:44 · 743 阅读 · 0 评论 -
[Leetcode] Search in Rotated Sorted Array (Java)
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原创 2013-12-29 13:13:16 · 1144 阅读 · 0 评论 -
[Leetcode] Sudoku Solver (Java)
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 su原创 2013-12-30 10:39:03 · 962 阅读 · 0 评论 -
[Leetcode] Count and Say (Java)
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 "o原创 2013-12-30 12:42:02 · 785 阅读 · 0 评论 -
[Leetcode] First Missing Positive (Java)
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原创 2013-12-31 10:15:06 · 952 阅读 · 0 评论 -
[Leetcode] Trapping Rain Water (Java)
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]原创 2013-12-31 11:23:36 · 698 阅读 · 0 评论 -
[Leetcode] Combination Sum (Java)
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原创 2013-12-30 17:40:48 · 536 阅读 · 0 评论 -
[Leetcode] Container With Most Water (Java)
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原创 2013-12-31 08:45:35 · 589 阅读 · 0 评论 -
[Leetcode] Combination Sum II (Java)
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原创 2013-12-30 17:49:10 · 1469 阅读 · 0 评论 -
[Leetcode] Letter Combinations of a Phone Number (Java)
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 st原创 2014-01-06 11:26:56 · 684 阅读 · 0 评论 -
[Leetcode] Multiply Strings (Java)
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.大数乘法按照正常做乘法的思路写下来的public clas原创 2014-01-06 11:21:26 · 605 阅读 · 0 评论 -
[Leetcode] Wildcard Matching (Java)
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原创 2014-01-06 16:09:46 · 2024 阅读 · 0 评论 -
[Leetcode] Jump Game (Java)
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 i原创 2014-01-06 17:05:16 · 716 阅读 · 0 评论 -
[Leetcode] Rotate Image (Java)
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度,原地(不占用与n有关系的空间大小)手写两组n为4,5的二维数组转换原创 2014-01-06 21:43:07 · 484 阅读 · 0 评论 -
[Leetcode] Permutations II (Java)
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].原创 2014-01-06 20:44:25 · 620 阅读 · 0 评论 -
[Leetcode] Anagrams (Java)
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.这题把我搞病了,半天没懂题意,去看discuss,原来是求所有同样字母构成的单词组,例如[dog,god,cat],则要返回[dog,god],若同时出现几组原创 2014-01-06 22:24:24 · 554 阅读 · 0 评论 -
[Leetcode] Jump Game II (Java)
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 is to原创 2014-01-06 19:13:45 · 799 阅读 · 0 评论 -
[Leetcode] Permutations (Java)w
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].输出全原创 2014-01-06 20:03:22 · 672 阅读 · 0 评论 -
[Leetcode] Pow(x, n) (Java)
Implement pow(x, n).实现pow(x, n)函数public class Solution { public double pow(double x, int n) { if(Math.abs(x-1)<1e-6||n==0) return 1; if(n==Integer.MIN_VALUE){ double d = pow(x, -n/2)原创 2014-01-08 16:00:45 · 634 阅读 · 0 评论 -
[Leetcode] N-Queens II (Java)
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.public class Solution { private int res=0; public int totalN原创 2014-01-08 17:09:38 · 463 阅读 · 0 评论 -
[Leetcode] Maximum Subarray (Java)
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原创 2014-01-08 17:19:17 · 513 阅读 · 0 评论 -
[Leetcode] Merge Intervals (Java)
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].整合集合按照start排序,然后扫描原始输入,合并有重合的并存到新集合中/** *原创 2014-01-08 18:57:12 · 519 阅读 · 0 评论 -
[Leetcode] Insert Interval (Java)
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.Examp原创 2014-01-08 19:16:50 · 661 阅读 · 0 评论 -
[Leetcode] Length of Last Word (Java)
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原创 2014-01-08 19:31:23 · 1499 阅读 · 1 评论 -
[Leetcode] Longest Common Prefix (Java)
Write a function to find the longest common prefix string amongst an array of strings.找最长公共前缀保存最长公共子串并跟所有字符串比较并更新public class Solution { public String longestCommonPrefix(String[] strs)原创 2014-01-08 19:39:02 · 521 阅读 · 0 评论 -
[Leetcode] Spiral Matrix II (Java)
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-01-08 20:17:10 · 568 阅读 · 0 评论 -
[Leetcode] N-Queens (Java)
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.原创 2014-01-08 17:00:43 · 536 阅读 · 0 评论 -
[Leetcode] Spiral Matrix (Java)
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原创 2014-01-08 17:46:49 · 470 阅读 · 0 评论 -
[Leetcode] Rotate List (Java)
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.这题写的好丑,磕磕绊绊才过/** * Definitio原创 2014-01-08 22:09:52 · 509 阅读 · 0 评论 -
[Leetcode] Minimum Path Sum (Java)
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原创 2014-01-09 10:16:47 · 566 阅读 · 0 评论 -
[Leetcode] Plus One (Java)
Given a number represented as an array of digits, plus one to the number.数字加1public class Solution { public int[] plusOne(int[] digits) { int flag = 1; for(int i=digits.length-1;i>=0;i--){原创 2014-01-09 16:36:23 · 569 阅读 · 0 评论 -
[Leetcode] Permutation Sequence (Java)
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原创 2014-01-08 21:14:54 · 743 阅读 · 0 评论 -
[Leetcode] Unique Paths (Java)
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原创 2014-01-09 09:40:48 · 621 阅读 · 0 评论