LeetCode
文章平均质量分 51
开先哥哥
这个作者很懒,什么都没留下…
展开
-
leetcode: Interleaving String
状态转换fandp[i][j] = ( dp[i-1][j] && ( s1[i-1] == s3[i+j-1])) || ( dp[i][j-1] && ( s2[j-1] == s3[i+j-1]))原创 2014-06-02 16:21:07 · 451 阅读 · 0 评论 -
leetcode: Permutations
方法1:方法2:class Solution {public: vector > permute(vector &num) { vector > res; if( num.size() <= 1){ res.push_back(num); return res; }原创 2014-05-21 22:28:33 · 402 阅读 · 0 评论 -
leetcode: Palindrome Partitioning II
class Solution {public: int minCut(string s) { if( s.size() <= 1) return 0; int size = s.size(); vector > dp( size, vector( size, false)); for( int i =原创 2014-05-21 20:23:19 · 525 阅读 · 0 评论 -
leetcode: Valid Palindrome
判断shi'fou'huclass Solution {public: bool isPalindrome(string s) { string tmp = ""; for( int i = 0; i < s.size(); ++i){ if(isalnum(s[i])){ if(isupper(原创 2014-05-21 20:35:38 · 391 阅读 · 0 评论 -
leetcode: Palindrome Number
题目说不允许申请额外空间,一个kong'jiaclass Solution {public: bool isPalindrome(int x) { if( x < 0) return false; int y = 0; int xx = x; while(xx){ y *=原创 2014-05-21 20:48:27 · 385 阅读 · 0 评论 -
leetcode: Best Time to Buy and Sell Stock
Say 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 and sell one share of the stock),原创 2014-06-24 17:18:12 · 697 阅读 · 0 评论 -
leetcode: Palindrome Partitioning
用动态规划可解。二维数组dp[i][j]原创 2014-05-21 19:04:49 · 392 阅读 · 0 评论 -
leetcode: Single Number
class Solution {public: int singleNumber(int A[], int n) { int res = 0; for( int i = 0; i < n; ++i) res ^= A[i]; return res; }};原创 2014-06-24 21:24:11 · 339 阅读 · 0 评论 -
leetcode: Sort Colors
偷个懒吧,用个库函数,不然就自己写个kuai'pai原创 2014-06-24 21:30:24 · 322 阅读 · 0 评论 -
leetcode: Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.如果有多组相同的也可以放到结果中原创 2014-06-24 16:16:48 · 347 阅读 · 0 评论 -
leetcode: Add Tow Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *ad转载 2014-06-24 22:08:56 · 391 阅读 · 0 评论 -
leetcode: Binary Tree Inorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-06-24 23:00:15 · 395 阅读 · 0 评论 -
leetcode: N-Queens
数组col[n] = 表示当前行n对应的原创 2014-06-05 11:45:35 · 341 阅读 · 0 评论 -
leetcode:Best Time to Buy and Sell Stock II
和Best Time to Buy and Sell Stock里面的题目原创 2014-06-24 19:08:14 · 676 阅读 · 0 评论 -
leetcode: Best Time to Buy and Sell Stock III
先用dp[i]表示当前下标之前最低价格,再ba原创 2014-06-24 21:19:49 · 380 阅读 · 0 评论 -
leetcode: Binary Tree Preorder Traversal
非递归版:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class原创 2014-06-25 11:13:18 · 332 阅读 · 0 评论 -
leetcode: N-Queens II
在N-Queens II原创 2014-06-05 11:53:37 · 367 阅读 · 0 评论 -
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 · 405 阅读 · 0 评论 -
leetcode: Unique Paths II
TLE递归tai'ma原创 2014-06-26 21:26:11 · 399 阅读 · 0 评论 -
leetcode: Subsets
class Solution {public: vector > subsets(vector &S) { return _subsets( S, 0); } vector > _subsets( vector &nums, int index){ vector > res; if( index == nums.size()原创 2014-06-25 16:26:48 · 408 阅读 · 0 评论 -
leetcode: Unique Paths
状态转换方程:dp[i][j] = dp[i-1原创 2014-06-26 20:31:49 · 361 阅读 · 0 评论 -
leetcode: Subsets II
和Subset解法一样class Solution {public: vector > subsetsWithDup(vector &S) { vector > res; set > s; size_t upper_bounds = 1 << S.size(); for( size_t i = 0; i < upper_原创 2014-06-25 17:03:35 · 381 阅读 · 0 评论 -
leetcode: Word Ladder II
leetcode: Word Ladder II原创 2014-06-25 22:00:43 · 764 阅读 · 0 评论 -
leetcode: Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].原创 2014-06-18 11:50:32 · 370 阅读 · 0 评论 -
leetcode: Word Ladder
class Solution {public: int ladderLength(string start, string end, unordered_set &dict) { int shortest = INT_MAX; dict.insert(end); queue > q; q.push( pair( start,原创 2014-06-25 19:16:35 · 397 阅读 · 0 评论 -
leetcode: Permutation Sequence
这道题太费力气了,还是证明要直接在纸上画个例子,然后模拟,光想可能很多dong'xi原创 2014-06-27 16:28:27 · 365 阅读 · 0 评论 -
leetcode: Next Permutation
stl的实现方法,先从后往前找一个相邻的原创 2014-06-07 19:25:51 · 347 阅读 · 0 评论 -
leetcode: Linked List Cycle
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycl原创 2014-06-30 14:55:42 · 392 阅读 · 0 评论 -
leetcode: Clone Graph
用map保存已有的/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */class Solution原创 2014-06-30 16:22:02 · 355 阅读 · 0 评论 -
leetcode: Candy
先从左到右遍历,如果i+1再从右到左遍历原创 2014-06-30 20:57:29 · 374 阅读 · 0 评论 -
leetcode: Valid Sudoku
注意题中要求只要已出现的s原创 2014-06-29 20:56:08 · 406 阅读 · 0 评论 -
leetcode: Permutations II
用next_permutation的方法class Solution {public: vector > permuteUnique(vector &num) { vector > res; if(num.size() <= 1){ res.push_back(num); return res;原创 2014-06-09 16:07:18 · 414 阅读 · 0 评论 -
leetcode: Sudoku Solver
直接模拟,意料之中的TLEclass Solution {public: void solveSudoku(vector > &board) { generateCandidates( board, candidates); while( true){ for( int i = 0; i < board.size(); ++i)转载 2014-06-30 19:08:37 · 352 阅读 · 0 评论 -
leetcode: ZigZag Conversion
设置标记class Solution {public: string convert(string s, int nRows) { if( s == "" || nRows <= 1) return s; vector con( nRows, string()); int i = 0; int r原创 2014-06-30 22:36:38 · 395 阅读 · 0 评论 -
Plus One
需要记录当前的仅为carry,而且别忘了要+1class Solution {public: vector plusOne(vector &digits) { int carry = 0; int pos = digits.size() - 1; digits[pos] += 1; for( ; pos >= 0; --原创 2014-06-30 21:10:18 · 388 阅读 · 0 评论 -
leetcode: LRU Cache
waclass LRUCache{public: LRUCache(int capacity) { cap = capacity; } int get(int key) { if( cache.count( key)){ auto iter = cache[key]; cache原创 2014-06-30 12:32:26 · 568 阅读 · 0 评论 -
leetcode: Longest Consecutive Sequence
class Solution {public: int longestConsecutive(vector &num) { if( num.size() <= 1) return num.size(); unordered_set nums; for( int i = 0; i < num.size(); ++i){原创 2014-06-09 17:37:21 · 297 阅读 · 0 评论 -
leetcode: Search in Rotated Sorted Array
先二分找到逆序元素的下标,然后用二分法查找两段class Solution {public: int search(int A[], int n, int target) { int index = rotatedOne( A, n, 0, n-1); if( index >= 0){ int res1 = binarySear原创 2014-06-30 15:35:47 · 342 阅读 · 0 评论 -
leetcode: Recover Binary Search 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-06-29 21:34:34 · 400 阅读 · 0 评论 -
leetcode: First Missing Positive
class Solution {public: int firstMissingPositive(int A[], int n) { if( A == NULL || n == 0) return 1;//这里返回 for( int i = 0; i < n; ){ if( A[i] != i + 1 &&转载 2014-06-30 22:17:55 · 434 阅读 · 0 评论