
leetcode解题报告
文章平均质量分 72
LightEggPlant
这个作者很懒,什么都没留下…
展开
-
算法——最小调整有序
题目描述 有一个整数数组,请编写一个函数,找出索引m和n,只要将m和n之间的元素排好序,整个数组就是有序的。注意:n-m应该越小越好,也就是说,找出符合条件的最短序列。 给定一个int数组A和数组的大小n,请返回一个二元组,代表所求序列的起点和终点。(原序列位置从0开始标号,若原序列有序,返回[0,0])。保证A中元素均为正整数。 测试样例: [1,4,6,5,9,10],6原创 2015-09-03 21:09:47 · 1193 阅读 · 0 评论 -
leetcode——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?思路:首先,要求原地调整,那么需要寻找变换规律。举个例子:对于3x3的矩阵,如顺原创 2014-12-13 21:23:48 · 645 阅读 · 0 评论 -
leetcode——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].思路:递原创 2014-12-13 20:47:15 · 588 阅读 · 0 评论 -
leetcode——Palindrome Partitioning II
题目:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab"原创 2014-12-28 16:52:41 · 650 阅读 · 0 评论 -
leetcode——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原创 2015-02-04 16:07:56 · 693 阅读 · 0 评论 -
leetcode——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原创 2015-02-04 13:26:57 · 746 阅读 · 0 评论 -
leetcode——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 statio原创 2015-02-04 13:36:03 · 807 阅读 · 0 评论 -
leetcode——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.De原创 2014-12-01 13:19:31 · 567 阅读 · 0 评论 -
leetcode——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].(最终区间)思路:该题是一道极角排序的类似题目。我们可以使用O(n)复原创 2014-12-08 15:28:58 · 547 阅读 · 0 评论 -
leetcode——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 partia原创 2015-02-01 21:48:42 · 679 阅读 · 0 评论 -
leetcode——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.思路:1000之内原创 2014-12-09 14:45:50 · 608 阅读 · 0 评论 -
leetcode——Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.原创 2014-10-18 11:22:47 · 999 阅读 · 2 评论 -
leetcode——Find Minimum in Rotated Sorted Array II
题目: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).Find the minimum element.The array may contain dup原创 2014-12-13 20:24:18 · 537 阅读 · 0 评论 -
leetcode——Count Complete Tree Nodes
题目Count Complete Tree Nodes Given a complete binary tree, count the number of nodes.In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last lev原创 2015-08-04 16:11:49 · 696 阅读 · 0 评论 -
leetcode——Count Primes
题目Description:Count the number of prime numbers less than a non-negative number, n.方法对于这道题,最简单最直观的方法往往是:对n以下的每一个数字进行判断从而求出个数。而在对每个数字(比如x)判断的时候,又是暴力验证的思想,即从2开始一直到sqrt(x)进行相除判断是否整除。更有甚者直接从2到x/2或原创 2015-08-04 15:24:11 · 594 阅读 · 0 评论 -
leetcode——Sliding Window Maximum
题目Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window原创 2015-08-12 10:59:50 · 681 阅读 · 0 评论 -
leetcode——Product of Array Except Self
题目Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O(n).For原创 2015-08-12 10:59:12 · 720 阅读 · 0 评论 -
leetcode——Lowest Common Ancestor of a Binary Tree
题目Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.思路这一次说的是一个普通的二叉树,给出两个节点,求他们的最低公共父节点。 回想一下,当这棵二叉树是二分查找树的时候的解决方案: 二分查找树解法:http://blog.youkuaiyun.com/langduhualangd原创 2015-08-11 21:04:29 · 675 阅读 · 0 评论 -
leetcode——Lowest Common Ancestor of a Binary Search Tree
题目Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.思路题目要求一个二叉排序树的两个节点的公共父节点,说通俗点,其实就是这两个节点所在的分支是从哪里开始分叉的。求出这个分叉点。对于二叉排序树,它的一个特点就是:一个节点的左子树节点都小于该节点,而原创 2015-08-11 20:24:26 · 680 阅读 · 0 评论 -
leetcode——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原创 2014-12-20 21:00:34 · 590 阅读 · 0 评论 -
leetcode——Dungeon Game
题目:The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was in原创 2015-01-20 11:14:41 · 1073 阅读 · 0 评论 -
leetcode——Factorial Trailing Zeroes
题目:Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.思路:在1到n这些数字中,能够贡献多少个因子5,就有多少个0.因为0是由10得到的,而10=2*5;显然2的数量远原创 2015-01-20 10:15:53 · 1310 阅读 · 0 评论 -
leetcode——Largest Number
题目:Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result ma原创 2015-01-19 16:48:14 · 704 阅读 · 0 评论 -
leetcode——Find Peak Element
题目:A peak element is an only element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multi原创 2014-12-06 19:16:12 · 1028 阅读 · 0 评论 -
leetcode——Distinct Subsequences
题目:Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (c原创 2015-01-31 21:15:56 · 645 阅读 · 0 评论 -
leetcode——Decode Ways
题目:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the t原创 2014-11-27 16:34:18 · 559 阅读 · 0 评论 -
leetcode——Scramble_String
题目:Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great原创 2014-11-24 15:22:55 · 599 阅读 · 0 评论 -
leetcode——Triangle
题目:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3原创 2014-11-26 16:41:16 · 567 阅读 · 0 评论 -
leetcode——Symmetric Tree
题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2014-11-26 17:29:02 · 586 阅读 · 0 评论 -
leetcode——LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if原创 2014-11-02 22:46:02 · 609 阅读 · 0 评论 -
leetcode——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 is [1, 2, 3原创 2014-11-03 13:30:01 · 654 阅读 · 0 评论 -
leetcode——Reverse Nodes in k-Group
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain原创 2014-11-19 13:36:15 · 566 阅读 · 0 评论 -
leetcode——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.E原创 2014-11-17 15:15:18 · 635 阅读 · 0 评论 -
leetcode——Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2014-10-11 15:59:20 · 709 阅读 · 0 评论 -
leetcode——Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2014-10-11 11:14:52 · 646 阅读 · 0 评论 -
leetcode——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 th原创 2014-11-20 13:32:30 · 540 阅读 · 0 评论 -
leetcode——Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of原创 2014-11-18 11:13:40 · 559 阅读 · 0 评论 -
leetcode——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:"((()))", "(()())", "(())()", "()(()原创 2014-12-06 20:21:00 · 652 阅读 · 0 评论 -
leetcode——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,原创 2014-12-06 18:21:31 · 627 阅读 · 0 评论 -
leetcode——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:原创 2014-12-04 14:38:04 · 512 阅读 · 0 评论