![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 51
开先哥哥
这个作者很懒,什么都没留下…
展开
-
leetcode: Palindrome Partitioning
用动态规划可解。二维数组dp[i][j]原创 2014-05-21 19:04:49 · 386 阅读 · 0 评论 -
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 · 443 阅读 · 0 评论 -
leetcode: Insert Interval
最简洁的实现转载 2014-05-21 16:47:05 · 376 阅读 · 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 · 378 阅读 · 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 · 386 阅读 · 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 · 396 阅读 · 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 · 517 阅读 · 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 · 339 阅读 · 0 评论 -
leetcode:Best Time to Buy and Sell Stock II
和Best Time to Buy and Sell Stock里面的题目原创 2014-06-24 19:08:14 · 666 阅读 · 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 · 685 阅读 · 0 评论 -
leetcode: Sort Colors
偷个懒吧,用个库函数,不然就自己写个kuai'pai原创 2014-06-24 21:30:24 · 314 阅读 · 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 · 386 阅读 · 0 评论 -
leetcode: Best Time to Buy and Sell Stock III
先用dp[i]表示当前下标之前最低价格,再ba原创 2014-06-24 21:19:49 · 372 阅读 · 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 · 333 阅读 · 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 · 378 阅读 · 0 评论 -
leetcode: N-Queens
数组col[n] = 表示当前行n对应的原创 2014-06-05 11:45:35 · 330 阅读 · 0 评论 -
leetcode: Merge Intervals
TLE了,/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */c原创 2014-05-21 16:58:24 · 456 阅读 · 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 · 396 阅读 · 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 · 326 阅读 · 0 评论 -
leetcode: N-Queens II
在N-Queens II原创 2014-06-05 11:53:37 · 356 阅读 · 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 · 391 阅读 · 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 · 375 阅读 · 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 · 364 阅读 · 0 评论 -
leetcode: Next Permutation
stl的实现方法,先从后往前找一个相邻的原创 2014-06-07 19:25:51 · 340 阅读 · 0 评论 -
leetcode: Word Ladder II
leetcode: Word Ladder II原创 2014-06-25 22:00:43 · 752 阅读 · 0 评论 -
leetcode: Unique Paths
状态转换方程:dp[i][j] = dp[i-1原创 2014-06-26 20:31:49 · 355 阅读 · 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 · 401 阅读 · 0 评论 -
leetcode: Unique Paths II
TLE递归tai'ma原创 2014-06-26 21:26:11 · 391 阅读 · 0 评论 -
leetcode: Permutation Sequence
这道题太费力气了,还是证明要直接在纸上画个例子,然后模拟,光想可能很多dong'xi原创 2014-06-27 16:28:27 · 360 阅读 · 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 · 291 阅读 · 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 · 405 阅读 · 0 评论 -
leetcode: Valid Sudoku
注意题中要求只要已出现的s原创 2014-06-29 20:56:08 · 398 阅读 · 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 · 558 阅读 · 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 · 422 阅读 · 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 · 392 阅读 · 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 · 385 阅读 · 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 · 347 阅读 · 0 评论 -
leetcode: Candy
先从左到右遍历,如果i+1再从右到左遍历原创 2014-06-30 20:57:29 · 367 阅读 · 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 · 345 阅读 · 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 · 381 阅读 · 0 评论