- 博客(144)
- 收藏
- 关注
原创 leetcode: Permutations I II
vector > res; sort(num.begin(), num.end()); res.push_back(num); if(num.size() < 2){ return res; } while(true){ int i = num.size()
2015-04-19 21:01:56
375
原创 leetcode: regular expression match
太丑bool isMatch(string s, string p) { vector > dp(s.size() + 1, vector(p.size() + 1, false)); dp[0][0] = true; for( int i = 1; i <= p.size(); ++i) dp[0][i] = '*' ==
2015-04-17 19:44:21
412
转载 leetcode: Distinct Subsequences
class Solution {public: int numDistinct(string S, string T) { if( S.size() == 0 || S.size() < T.size()) return 0; if( T.size() == 0) return 1; vect
2014-07-11 15:56:54
470
原创 leetcode: Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { int used[256]; memset( used, 0, sizeof(used)); int tmp = 0, maximum = 0; for( int i = 0; i < s.siz
2014-07-11 15:00:56
476
原创 leetcode: Minimum Depth of Binary Tree
必须是到叶节点/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */cla
2014-07-11 11:35:18
453
原创 leetcode: Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.class Solution {public: int romanToInt(string s) { unordered_map mapping;
2014-07-11 11:14:18
370
原创 leetcode: Integer to Roman
把关键的数字找出来,分别是和1,4,5,9相关的class Solution {public: string intToRoman(int num) { const int integer[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; const string roman[]
2014-07-11 10:45:10
390
原创 leetcode: Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the
2014-07-10 22:55:18
398
原创 leetcode: Search a 2D Matrix
杨氏矩阵,对比右上角的数值和目标值daclass Solution {public: bool searchMatrix(vector > &matrix, int target) { if( matrix.size() == 0) return false; int i = 0, j = matrix[0].size() -
2014-07-10 22:01:04
382
原创 leetcode: Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.class Solution {public: void setZeroes(vector > &matrix) { vector > tmp; for(
2014-07-10 21:46:17
477
原创 leetcode: Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha
2014-07-10 21:28:33
383
原创 leetcode: Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},
2014-07-10 21:19:09
411
原创 leetcode: Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20
2014-07-10 21:07:56
357
原创 leetcode: Reverse Linked List II
递归/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode
2014-07-10 14:12:19
425
转载 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 station i to
2014-07-09 23:03:04
436
原创 leetcode: Linked List Cycle II
注意两个测试用例,{}/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:
2014-07-09 22:40:40
484
1
原创 leetcode: Generate Parentheses
括号匹配种数的问题,其实就是卡特兰数class Solution {public: vector generateParenthesis(int n) { vector res; if( n == 0) return res; core( res, "", n, 0, 0); return res
2014-07-09 22:25:57
413
转载 leetcode: Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi
2014-07-09 22:09:06
435
原创 leetcode: Search Insert Position
看清题目,插入位置,不是二分查找class Solution {public: int searchInsert(int A[], int n, int target) { if( n == 0) return 0; return binarySearch( A, target, 0, n-1); } int b
2014-07-09 19:40:25
417
原创 leetcode: Pascal's Triangle II
要求只能用O(k)空间,所以用liangclass Solution {public: vector getRow(int rowIndex) { vector > dp( 2, vector( rowIndex+1, 0)); int cur = 1; int pre = 0; dp[pre][0] = 1;
2014-07-09 00:15:19
393
原创 leetcode: Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant
2014-07-08 23:19:18
543
原创 leetcode: Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.
2014-07-08 22:53:32
469
原创 leetcode: Container With Most Water
贪心,最大class Solution {public: int maxArea(vector &height) { if( height.size() < 2) return 0; int res = INT_MIN; int i = 0, j = height.size()-1; while(
2014-07-08 15:28:47
446
原创 leetcode: Trapping Rain Water
先计算每个格的class Solution {public: int trap(int A[], int n) { if( n < 3) return 0; int *max_left = new int[n](); int *max_right = new int[n](); max_left[
2014-07-08 11:29:55
337
转载 leetcode: Largest Rectangle in Histogram
http://blog.csdn.net/doc_sgl/article/details/11805519这篇分析写得heclass Solution {public: int largestRectangleArea(vector &height) { stack stk; height.push_back(0); int r
2014-07-08 10:40:35
338
原创 leetcode: Spiral Matrix
注意各个边界条件,i,j的++,--class Solution {public: vector spiralOrder(vector > &matrix) { vector res; if( matrix.size() == 0) return res; int m = matrix[0].size();
2014-07-07 21:20:58
279
原创 leetcode: Merge k Sorted Lists
多路链表归并,复杂度O(mn log)/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {pub
2014-07-07 20:34:03
392
原创 leetcode: Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from
2014-07-07 20:21:23
384
原创 leetcode: Sort List
Sort a linked list in O(n log n) time using constant space complexity.利用链表归并排序/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN
2014-07-07 19:57:16
338
原创 leetcode: Restore IP Addresses
DFS、回溯class Solution {public: vector restoreIpAddresses(string s) { vector res; if( s.size() 12) return res; core( res, s, 0, ""); return res;
2014-07-07 17:36:47
480
原创 leetcode: Balanced Binary Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti
2014-07-06 20:56:45
307
转载 leetcode: Regular Expression Matching
对于S[i]和P[j]:如果P[j+1]!='*',S[i] == P[j]=>匹配下一位(i+1, j+1),S[i]!=P[j]=>匹配失败;如果P[j+1]=='*',S[i]==P[j]=>匹配下一位(i+1, j+2)或者(i, j+2),S[i]!=P[j]=>匹配下一位(i,j+2)。匹配成功的条件为S[i]=='\0' && P[j]=='\0'。
2014-07-06 18:19:56
416
原创 leetcode: Word Search
深度搜索,挨个搜索每个格子的四个角,符合条件就退出,zhe'lclass Solution {public: bool exist(vector > &board, string word) { if( word == "") return true; for( int i = 0; i < board.size(); ++i)
2014-07-06 17:03:53
383
转载 leetcode: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 "()",
2014-07-06 15:54:48
396
原创 leetcode: Substring with Concatenation of All Words
提交了好多遍,注意每个词搜索class Solution {public: vector findSubstring(string S, vector &L) { vector res; const int word_length = L[0].size(); const int words_length = word_length *
2014-07-05 21:44:04
368
原创 leetcode: Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1-
2014-07-05 19:08:35
370
原创 leetcode: Remove Duplicates from Sorted Array II
class Solution {public: int removeDuplicates(int A[], int n) { if( n < 2) return n; int cnt = 0; int cur = INT_MIN; int allow = 1; for( int i =
2014-07-05 18:16:40
326
原创 leetcode: Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with
2014-07-05 17:51:38
327
原创 leetcode: String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca
2014-07-05 17:17:31
362
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人