leetcode
CarryPotMan
梦想是成为北邮渣硕。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fin原创 2016-10-05 21:26:31 · 343 阅读 · 0 评论 -
368. Largest Divisible Subset
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0.If there are multiple solutions, return原创 2016-07-26 17:57:15 · 224 阅读 · 0 评论 -
331. Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. _9原创 2016-07-23 10:43:08 · 244 阅读 · 0 评论 -
139. 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", "co原创 2016-06-16 17:28:35 · 258 阅读 · 0 评论 -
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.Convertingthe input string to integer is NOT原创 2016-06-30 22:08:42 · 380 阅读 · 0 评论 -
134. 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 its nex原创 2016-06-14 22:31:55 · 226 阅读 · 0 评论 -
372. Super Pow
Your task is to calculate a^b mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.Example1:a = 2b = [3]Result: 8Example2:a = 2b = [1,0]R原创 2016-07-23 09:36:49 · 349 阅读 · 0 评论 -
376. Wiggle Subsequence
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either posi原创 2016-07-22 12:31:14 · 306 阅读 · 0 评论 -
17. Letter Combinations of a Phone Number
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 string "23"O原创 2016-06-13 22:06:13 · 214 阅读 · 0 评论 -
330. Patching Array
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Retur原创 2016-06-13 11:26:37 · 187 阅读 · 0 评论 -
85. 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.DP解法0th row: 0 0 0 1 0 0 0height: 0 0 0 1 0 0 0left: 0 0 0 3 0 0 0right 7 7 7 4 7原创 2016-06-28 21:07:32 · 219 阅读 · 0 评论 -
69. Sqrt(x)和367. Valid Perfect Square
69.Sqrt(x)Implement int sqrt(int x).Compute and return the square root of x.367.Valid Perfect SquareGiven a positive integer num, write a function which returns True if num is a perfect square else Fal原创 2016-06-28 15:22:47 · 360 阅读 · 0 评论 -
转载:LeetCode:5Longest Palindromic Substring 最长回文子串
本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html题目链接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转载 2016-05-22 15:39:33 · 264 阅读 · 0 评论 -
最长递增子序列
这是一道经典的动态规划题最长递增子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i设dp[i]表示以i为结尾的最长递增子序列的长度,则状态转移方程为:dp[i] = max{dp[j]+1}, 1 1 int lengthOfLIS(vectorint>& nums) { 2 int size = nums.size();原创 2016-05-22 15:38:27 · 210 阅读 · 0 评论 -
321. Create Maximum Number 解题方法详解
321. Create Maximum Number题目描述 Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of原创 2016-05-22 15:37:03 · 476 阅读 · 0 评论 -
375. Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I’ll tell you whether the number I picked is highe原创 2016-07-26 18:45:13 · 230 阅读 · 0 评论 -
Combination Sum I II 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 within原创 2016-04-30 15:37:25 · 434 阅读 · 0 评论 -
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 = “/../”? In th原创 2016-07-30 16:49:17 · 196 阅读 · 0 评论 -
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 space.《剑原创 2016-10-05 20:54:45 · 296 阅读 · 0 评论 -
46. Permutations 和 47. Permutations II
46. PermutationsGiven 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], and [3,2,1].c原创 2016-05-19 09:53:48 · 222 阅读 · 0 评论 -
373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.Define a pair (u,v) which consists of one element from the first array and one element from the second array.原创 2016-08-23 21:37:41 · 256 阅读 · 0 评论 -
114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2原创 2016-03-17 19:17:31 · 235 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock I II III IV
121. Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one an原创 2016-09-13 11:35:28 · 314 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and原创 2016-03-09 14:27:20 · 258 阅读 · 0 评论 -
382. Linked List Random Node
Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen.Follow up: What if the linked list is extremely large and its le原创 2016-08-22 21:32:26 · 229 阅读 · 0 评论 -
120. 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,4], [6,5,7],原创 2016-09-12 21:54:39 · 195 阅读 · 0 评论 -
164. Maximum Gap
问题描述Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements.You原创 2016-02-29 21:02:01 · 409 阅读 · 0 评论 -
29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.https://discuss.leetcode.com/topic/15568/detailed-explained-8ms-c-solutionIn this problem,原创 2016-08-07 11:25:01 · 239 阅读 · 0 评论 -
365. Water and Jug Problem
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two ju原创 2016-07-31 11:42:17 · 291 阅读 · 0 评论 -
欧几里德算法求解最大公约数
1、欧几里德算法(辗转相除法)最大公约数 greatest common divisor,简写为gcd;或highestcommon factor,简写为hcf 最小公倍数 最小公倍数(Least Common Multiple,缩写L.C.M.)最小公倍数=两数的乘积/最大公约数欧几里德算法(辗转相除法) 1. 欧几里德算法和扩展欧几里德算法 1). 欧几里德算法 欧几里德算法又称辗转原创 2016-03-05 20:35:44 · 807 阅读 · 0 评论 -
324. Wiggle Sort II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....Example:(1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. (2) Given nums = [1,原创 2016-07-31 10:41:55 · 222 阅读 · 0 评论 -
371 Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.Example: Given a = 1 and b = 2, return 3.转自 https://discuss.leetcode.com/topic/49771/java-simple-easy-un原创 2016-07-20 17:15:51 · 317 阅读 · 0 评论 -
90. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate s原创 2016-05-20 20:22:13 · 194 阅读 · 0 评论 -
162. Find Peak Element
A peak element is an 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 multiple peaks, in that case原创 2016-05-20 17:11:42 · 183 阅读 · 0 评论 -
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 orde原创 2016-05-18 22:36:38 · 211 阅读 · 0 评论 -
148. Sort List
Sort a linked list in O(n log n) time using constant space complexity.排序一个链表可以类似于插入排序那样,这样需要每次都从头开始遍历找到插入位置,所以复杂度是O(n^2),所以这样是不行的,数组中的排序方法里快排和堆排并不适合。如果有两个排序的链表,将他们进行融合是很容易的,所以采用分治法,是可以很快求解的。所以答案如下:/**原创 2016-03-23 22:35:34 · 223 阅读 · 0 评论 -
106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.class Solution {public: TreeNode* buildTree(vector<int>& in原创 2016-03-09 16:37:02 · 187 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.class Solution {public: /* from Preorder and Inorder Travers原创 2016-03-09 16:35:56 · 257 阅读 · 0 评论 -
337. House Robber III(包含I和II)
198. House RobberYou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is tha原创 2016-03-20 21:21:42 · 475 阅读 · 0 评论 -
318. Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case lette原创 2016-03-20 17:15:26 · 255 阅读 · 0 评论
分享