leetcode
文章平均质量分 50
RISKYHE何小河
这个作者很懒,什么都没留下…
展开
-
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-04-10 10:50:25 · 302 阅读 · 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.思考:用两个指针A,B,A先走k步,然后A、B同时想法1、A先走k步,原创 2015-04-08 19:32:53 · 301 阅读 · 0 评论 -
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-04-20 12:15:55 · 417 阅读 · 0 评论 -
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原创 2015-04-14 11:03:46 · 428 阅读 · 0 评论 -
Merge k Sorted Lists
问题描述Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思考:k路归并,用什么?排序想法1、利用最小堆。堆顶是最小元素,取出后调整堆。如果元素取出后链表为null,则将堆末尾的list放到堆顶。堆长度减一,并调整堆。2、其实其他排序也可以。这里复习了原创 2015-04-24 11:22:44 · 349 阅读 · 0 评论 -
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-04-13 10:55:32 · 265 阅读 · 0 评论 -
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-04-24 09:50:26 · 317 阅读 · 0 评论 -
Substring with Concatenation of All Words
问题描述You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and原创 2015-04-21 10:44:12 · 321 阅读 · 0 评论 -
Isomorphic Strings
问题描述Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another c翻译 2015-04-30 10:34:58 · 333 阅读 · 0 评论 -
Number of Islands
思考:与Surrounded Regions类似方法利用num计数,遇到gird[i][j] = ‘1’,就让num++; 然后查看grid[i - 1][j]和grid[i - 1][j]。如果为1,则unit,在unit中,如果两个类的根不同,则num–;并且将两个类的根置为同一个。如grid[i - 1][j] == grid[i][j] == 1,则比较grid[i - 1][j]的根ro原创 2015-04-08 17:20:15 · 527 阅读 · 0 评论 -
Word Break
问题描述Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, given s = “leetcode”, dict = [“leet”,原创 2015-05-17 11:22:29 · 371 阅读 · 0 评论 -
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-04-30 10:26:27 · 347 阅读 · 0 评论 -
House Robber II
问题描述After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a原创 2015-05-25 11:21:57 · 443 阅读 · 0 评论 -
Combination Sum III
问题描述Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers wit原创 2015-05-25 10:55:09 · 756 阅读 · 0 评论 -
Surrounded Regions
思考:只要将被包围的O和为被包围的O分开就好方法1、用 并查集(Union-find set)来区分2、用BFS将与边缘O相邻的O(为被包围的O)全部变为#(或者其他符号),然后对远数组更改代码块并查集public class Solution { int[] unitArray; //集合编号(隶属于哪一类) boolean[] edge; //是否被包围,每一类的ed原创 2015-04-08 16:16:53 · 588 阅读 · 0 评论 -
Unique Paths & Unique Paths II
思考:Unique Path 可以用动态规划和排列组合解 Unique Path II 可以用动态规划想法1、对与问题一,排列组合考虑,要走到重点总共有n+m-2步,其中m-1步down和n-1步right,如果在m*n的方格内,选择m-1个down,那么剩下n-1步已经确定,并且唯一确定。所以问题一的解为C_(n+m-2)^(n-1)x_i^2原创 2015-04-08 18:54:24 · 373 阅读 · 0 评论 -
Spiral Matrix I && Spiral Matrix II
问题I描述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 ] ] Y原创 2015-04-10 13:10:19 · 353 阅读 · 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” “原创 2015-04-09 13:33:13 · 340 阅读 · 0 评论 -
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.Each s原创 2015-04-10 14:48:34 · 309 阅读 · 0 评论 -
Implement strStr
问题描述Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.思考:字符串匹配,KMP算法想法1、暴力匹配苏纳法效率肯定不高2、重温一下KMP算法,next数组3、学习一下BM算法和sunday算法(一原创 2015-04-22 11:26:16 · 376 阅读 · 0 评论 -
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 puzzle… …an原创 2015-04-18 10:26:20 · 287 阅读 · 0 评论 -
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],翻译 2015-04-18 10:35:25 · 361 阅读 · 0 评论 -
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-04-19 11:16:19 · 435 阅读 · 0 评论 -
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 “()”, wh原创 2015-04-19 13:32:55 · 330 阅读 · 0 评论 -
Bitwise AND of Numbers Range
问题描述Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.For example, given the range [5, 7], you should return 4.思考:只要找到m和n的二进制表示中,最高的不同翻译 2015-04-16 09:50:22 · 413 阅读 · 0 评论 -
Anagrams
问题描述Given an array of strings, return all groups of strings that are anagrams. 即回文构词法 For example: Input: [“tea”,”and”,”ate”,”eat”,”den”] Output: [“tea”,”ate”,”eat”]思考:怎么才能把一个单词的各个字母不按顺序比较想法1: 排原创 2015-04-12 10:48:40 · 271 阅读 · 0 评论 -
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].思考:递归求解想法1、设number为原创 2015-04-12 16:18:02 · 327 阅读 · 0 评论 -
Rotate Image
问题描述You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).For example: Input: [[1, 2], [3, 4]] Output: [[3, 1], [4, 2]];思考:怎么变换使用O(1)的空间想法:先对矩阵进行对角线变原创 2015-04-12 15:39:41 · 287 阅读 · 0 评论 -
Binary Tree Right Side View
Binary Tree Right Side View思考:只需要知道每一行的最后一个节点值即可。方法1、考虑使用两个队列进行BFS,A队列存放当前行的节点,B队列存放当前节点的子节点,即当A队列空时,最后一个访问的节点即位当前行的最后一个节点。2、递归遍历。每一层都会有一个值加入列表,即可以利用DFS来遍历,并传入当前深度level(root为1),如果当前深度等于re(结果列表)的长度,即将原创 2015-04-08 12:02:38 · 411 阅读 · 0 评论 -
Shortest Palindrome
问题描述Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.For examp翻译 2015-05-26 14:09:09 · 676 阅读 · 0 评论