
LeetCode
文章平均质量分 53
cfcf0517
这个作者很懒,什么都没留下…
展开
-
Two Sum
题目:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target,原创 2015-07-09 19:32:37 · 338 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length原创 2015-07-10 16:34:32 · 300 阅读 · 0 评论 -
Add Two Numbers
Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers原创 2015-07-10 10:25:33 · 315 阅读 · 0 评论 -
Pow(x, n)
Implement pow(x, n).一个数的32次方,只要知道16次方再求平方即可,依次类推,只要五次乘法:先求平方,再求4次方,8次方,16次方,32次方。奇数则乘以x。public class Solution { public double myPow(double x, int n) { if(n<0) return 1/power(原创 2015-07-25 13:49:05 · 313 阅读 · 0 评论 -
N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.对于一个4皇后问题,声明一个长度为4的数组(因为行数为4)。usedColumns[] = [1,0,2,3]表达含义是:当前4个原创 2015-07-25 15:14:08 · 233 阅读 · 0 评论 -
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-07-25 20:21:32 · 276 阅读 · 0 评论 -
Merge Intervals
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]./** * Definition for an interval. * public class Int原创 2015-07-26 10:53:00 · 232 阅读 · 0 评论 -
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-07-25 20:28:07 · 241 阅读 · 0 评论 -
Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.题目的意思是说字符串数组中由相同字母组成但字母顺序不同的字符串的集合。如“abc”和“bac”。将字符串转换为字符数组排序,再转换为字符串。使用map的key原创 2015-07-25 10:36:17 · 190 阅读 · 0 评论 -
Insert Interval
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.Exa原创 2015-07-27 14:16:34 · 321 阅读 · 0 评论 -
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""32原创 2015-07-27 20:51:48 · 277 阅读 · 0 评论 -
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 class Solution { public st原创 2015-07-27 21:31:45 · 265 阅读 · 0 评论 -
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 def原创 2015-07-27 14:25:47 · 241 阅读 · 0 评论 -
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原创 2015-07-28 08:58:14 · 230 阅读 · 0 评论 -
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a原创 2015-08-08 15:13:31 · 297 阅读 · 0 评论 -
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example, given [100, 4, 200, 1, 3, 2], the longest consecutive elements sequence should be [原创 2015-08-08 19:51:24 · 224 阅读 · 0 评论 -
Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O XX X O X原创 2015-08-08 21:59:24 · 225 阅读 · 0 评论 -
Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total su原创 2015-08-08 20:33:40 · 239 阅读 · 0 评论 -
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原创 2015-07-28 09:19:32 · 241 阅读 · 0 评论 -
Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11" b = "1"Return "100".public class Solution { public String addBinary(String a, String b) {原创 2015-07-28 14:52:05 · 280 阅读 · 0 评论 -
Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a timeEach i原创 2015-08-08 15:55:47 · 266 阅读 · 0 评论 -
Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most k transactions.Note:You may not en原创 2015-08-07 15:01:42 · 262 阅读 · 0 评论 -
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.只有当是9时,加1才会进位,否则返回即可。如果一原创 2015-07-28 14:17:52 · 260 阅读 · 0 评论 -
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 ambiguous.原创 2015-07-28 13:37:12 · 289 阅读 · 0 评论 -
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.也就是求最长回文子字符串。回文原创 2015-07-15 21:10:05 · 259 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return 6.Anal原创 2015-08-08 14:58:18 · 386 阅读 · 0 评论 -
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-07-28 10:12:10 · 264 阅读 · 0 评论 -
Median of Two Sorted Arrays
题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).leetcode将这个问题转换为求第k个元素原创 2015-07-15 20:05:25 · 341 阅读 · 0 评论 -
ZigZag Conversion
题目:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P原创 2015-07-15 22:05:58 · 432 阅读 · 0 评论 -
Reverse Integer
题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321一般来说整数的处理问题要注意的有两点,一点是符号,另一点是整数越界问题。对于32位整数,1000000003 的翻转就是越界的。public static int re原创 2015-07-16 10:08:11 · 384 阅读 · 0 评论 -
Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements: Each child must have at least on原创 2015-08-09 19:55:59 · 295 阅读 · 0 评论 -
Single Number
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using e原创 2015-08-09 20:04:35 · 230 阅读 · 0 评论 -
Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.Definition for undirected graph.原创 2015-08-09 16:29:25 · 246 阅读 · 0 评论 -
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to原创 2015-08-09 19:16:51 · 277 阅读 · 0 评论 -
Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since原创 2015-08-24 18:10:43 · 329 阅读 · 0 评论 -
Ugly Number II
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10原创 2015-08-24 18:36:59 · 369 阅读 · 0 评论 -
Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may as原创 2015-08-09 10:50:52 · 315 阅读 · 0 评论 -
Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","b"原创 2015-08-09 15:23:28 · 259 阅读 · 0 评论 -
Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.和剑指offer的复杂链表的复制是一个思路:先复制节点连接在原创 2015-08-09 22:20:59 · 399 阅读 · 0 评论 -
String to Integer
题目:Implement atoi to convert a string to an integer.实现中首先使用trim()处理空格,要注意正负号,并且多个符号位如“+-22”是返回0的,但是正确的数字后有非数字,如“+22ab”,是返回“22”的。public static int myAtoi(String str) { if(str.isEmpty()||str.l原创 2015-07-16 10:55:27 · 585 阅读 · 0 评论