- 博客(236)
- 收藏
- 关注
原创 Find Minimum in Rotated Sorted Array
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.You may assume no duplicate exists in
2014-10-16 19:44:37
634
原创 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
2014-10-12 08:34:25
587
原创 Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word must exi
2014-10-11 18:13:02
619
原创 Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate word m
2014-10-11 15:56:36
549
原创 Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens = "
2014-10-11 14:43:41
660
原创 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""3
2014-10-11 11:07:35
475
原创 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
2014-10-11 09:18:11
457
原创 Permutations
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
2014-10-11 09:14:51
2267
原创 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 o
2014-10-11 09:00:34
485
原创 Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
2014-10-10 23:28:33
588
原创 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 / \ gr
2014-10-10 21:47:56
561
原创 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st
2014-10-10 19:44:35
536
原创 Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without an
2014-10-10 16:12:24
552
原创 Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain dupli
2014-10-10 13:59:25
481
原创 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.class Solution: # @param num1, a
2014-10-10 13:25:07
414
原创 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 ambiguo
2014-10-10 13:12:24
482
原创 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
2014-10-10 12:57:00
466
原创 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, 0). Fin
2014-10-10 12:13:56
427
原创 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 a word:
2014-10-10 09:55:52
508
原创 Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", r
2014-10-09 18:36:59
440
原创 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.
2014-10-09 09:48:18
359
原创 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the larges
2014-09-29 15:45:11
365
原创 String to Integer (atoi)
class Solution {public: int atoi(const char *str) { if(str==NULL) return 0; long long ret=0; long long ret_max=INT_MAX; long long ret_min=-((long long)INT_MIN); bool is_positive=true;
2014-09-18 17:28:44
330
原创 Maximal Rectangle
class Solution {public: int maximalRectangle(vector > &matrix) { //转换矩阵,变成Largest Rectangle in Histogram 问题,也是醉了 if(matrix.empty()) return 0; vector unit(matrix[0].size(),0); vector
2014-09-17 21:10:25
387
原创 Trapping Rain Water
//fun...class Solution {public: int trap(int A[], int n) { if(n<3) return 0; vector left_max(n,0),right_max(n,0); //left_max[i]: 从左到i,最大值的index //right_max[i]: 从右到i,最大值的index //from le
2014-09-17 15:56:26
424
原创 Surrounded Regions
class Solution {public: void solve(vector> &board) { if(board.size()<1) return; //矩阵is_visited用于存储是否访问过 vector can_get_out(board.size()*board[0].size(),false); vector is_visi
2014-09-16 23:24:30
365
原创 Recover Binary Search Tree
/*struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};*/class Solution {public: void recoverTree(TreeNode *
2014-07-12 16:52:44
508
原创 Morris遍历
解释:点击打开链接#include#include#include#include#include#include#include#include#include#include#includeusing namespace std;struct bst_node{ int key; bst_node *left; bst_node *right; bst
2014-07-12 13:51:19
628
原创 Best Time to Buy and Sell Stock III
class Solution {public: int maxProfit(vector &prices) { int n=prices.size(); if(n==0) return 0; //从左到右遍历计算maxProfit(prices[0:i]) int *minVal=new int[n];//minVal[i] -> [0:i]区间的最小值 int
2014-07-12 11:39:08
676
原创 Best Time to Buy and Sell Stock II
class Solution {public: int maxProfit(vector &prices) { int sum = 0; for (int i = 1; i < prices.size(); i++) { int diff = prices[i] - prices[i - 1]; if (di
2014-07-12 09:52:34
360
原创 Jump Game II
class Solution {public: int jump(int A[], int n) { //table[i] 为 i jump to n-1 的最少步数 int *table=new int[n]; memset(table,-1,n*sizeof(int)); table[n-1]=0; return _jump(A,n,0,table);
2014-07-12 09:17:39
366
原创 Jump Game
class Solution {public: bool canJump(int A[], int n) { if(n==1) return true; int pre=0,cur=A[0],next=A[0]; /* pre:上一个可以到达区域的右边界 cur:由[0:pre]Jump可以到达区域的右边界 next:由[0:cur]Jum
2014-07-12 00:26:17
361
原创 Palindrome Number
class Solution {public: bool isPalindrome(int x) { if(x<0) return false; int x_cp=x; int digits=1;//纪录x的位数 while(x_cp/10>0) { digits++; x_cp=x_cp/10; } int base=pow(10,d
2014-07-11 23:52:17
357
原创 Distinct Subsequences
class Solution {public: int numDistinct(string S, string T) { if(S.size()<T.size()) return 0; int *table=new int[S.size()*T.size()]; memset(table,0,S.size()*T.size()*sizeof(in
2014-07-10 00:33:52
348
原创 Word Break
class Solution {public: bool wordBreak(string s, unordered_set &dict) { if(s.empty()) return dict.count("")==0?false:true; vector is_sep(s.size()+1,false);//is_sep[i]表示s[0,i-1]是否可分 is
2014-07-07 22:35:46
343
原创 Minimum Window Substring
class Solution {public: string minWindow(string S, string T) { if(T.empty() || S.size()<T.size()) return ""; string min_window=S; bool has_answer=false; int s_hash[256],t_hash[256];
2014-07-06 12:08:55
614
原创 Combination Sum II
class Solution {public: vector > combinationSum2(vector &num, int target) { sort(num.begin(),num.end()); vector > ret(combinationSum(num,0,target)); sort(ret.begin(),ret.end()); ret.eras
2014-07-05 23:12:14
320
原创 Combination Sum
class Solution {public: vector > combinationSum(vector &candidates, int target) { sort(candidates.begin(),candidates.end()); return combinationSum(candidates,0,target); } vector > combi
2014-07-05 21:35:57
385
原创 Word Search
class Solution {public: bool exist(vector > &board, string word) { bool *used= new bool[board.size()*board[0].size()]; for(int i=0;i<board.size();i++) { for(int j=0;j<board[0].size
2014-07-05 15:49:42
403
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人