算法
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 · 270 阅读 · 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 · 136 阅读 · 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 · 364 阅读 · 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 · 161 阅读 · 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 · 157 阅读 · 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 · 183 阅读 · 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 · 211 阅读 · 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 · 325 阅读 · 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 · 182 阅读 · 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 · 280 阅读 · 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 · 256 阅读 · 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 · 162 阅读 · 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 · 129 阅读 · 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 · 162 阅读 · 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 · 290 阅读 · 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 · 204 阅读 · 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 · 159 阅读 · 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 · 254 阅读 · 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 · 177 阅读 · 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 · 212 阅读 · 0 评论 -
三色球排序
荷兰国旗题目描述拿破仑席卷欧洲大陆之后,代表自由,平等,博爱的竖色三色旗也风靡一时。荷兰国旗就是一面三色旗(只不过是横向的),自上而下为红白蓝三色。该问题本身是关于三色球排序和分类的,由荷兰科学家Dijkstra提出。由于问题中的三色小球有序排列后正好分为三类,Dijkstra就想象成他母国的国旗,于是问题也就被命名为荷兰旗问题(Dutch National Flag Problem)。下面是问题的转载 2016-09-28 23:57:46 · 1606 阅读 · 0 评论 -
求比正整数N大的最小正整数M,且M与N的二进制表示中有相同数目的1
转自http://blog.csdn.net/ligt0610/article/details/7262757一般最容易想到的方法就是先计算正整数N用二进制表示时1的个数count1,然后不停地计算N++用二进制表示时1的个数count2,直到碰到count1 == count2成立,代码如下:[cpp] view plain copy转载 2016-09-23 19:26:31 · 1839 阅读 · 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 · 167 阅读 · 0 评论 -
编辑距离 (Edit distance)
http://www.dreamxu.com/books/dsa/dp/edit-distance.html问题描述给定 2 个字符串 a, b. 编辑距离是将 a 转换为 b 的最少操作次数,操作只允许如下 3 种:插入一个字符,例如:fj -> fxj删除一个字符,例如:fxj -> fj替换一个字符,例如:jxj -> fyj思路用分治的思想解决比较简单,将复杂的问题分解成相似的子问题转载 2016-09-04 21:47:57 · 1889 阅读 · 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 · 189 阅读 · 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 · 185 阅读 · 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 · 263 阅读 · 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 · 202 阅读 · 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 · 158 阅读 · 0 评论 -
Path sum: four ways
#include <iostream>#include <vector>#include <queue>#include <map>using namespace std;#define VI vector<int>#define VII vector<vector<int> >VII dNext = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};class KV原创 2016-09-13 10:11:19 · 182 阅读 · 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 · 128 阅读 · 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 · 353 阅读 · 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 · 179 阅读 · 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 · 218 阅读 · 0 评论 -
欧几里德算法求解最大公约数
1、欧几里德算法(辗转相除法)最大公约数 greatest common divisor,简写为gcd;或highestcommon factor,简写为hcf 最小公倍数 最小公倍数(Least Common Multiple,缩写L.C.M.)最小公倍数=两数的乘积/最大公约数欧几里德算法(辗转相除法) 1. 欧几里德算法和扩展欧几里德算法 1). 欧几里德算法 欧几里德算法又称辗转原创 2016-03-05 20:35:44 · 675 阅读 · 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 · 419 阅读 · 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 · 141 阅读 · 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 · 136 阅读 · 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 · 175 阅读 · 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 · 141 阅读 · 0 评论