![](https://img-blog.csdnimg.cn/20201014180756916.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
非一次ac
freshXD
爱是势均力敌
展开
-
leetcode Sudoku Solver
class Solution {public: int flip(int i){ int ret = 1 << i; ret = ~ret; return ret; } bool valid(int v, int x, int y, vector<int>&row, vector<int>&column, vector<int原创 2015-04-16 13:03:01 · 346 阅读 · 0 评论 -
leetcode generate parenthese
class Solution {public: void dfs(int leftnum, int rightnum,const int totpair, vector<string>&ret, string str){ if(leftnum == totpair){ for(int iter = rightnum; iter < totpair;原创 2015-03-22 13:45:34 · 529 阅读 · 0 评论 -
leetcode Reverse Nodes in k-Group
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *rev原创 2015-03-30 18:21:49 · 332 阅读 · 0 评论 -
leetcode sum root to leaf
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solutio原创 2015-03-22 13:33:10 · 335 阅读 · 0 评论 -
leetcode single number2
//将一个整数右移k位再左移k位,不一定得到原来的结果//如果过程中间有对原数组中元素的修改,不可以乱用原值可以不用base * 2^k的形式,反而用ret |= (1<<i)的形式class Solution {public: int singleNumber(int A[], int n) { if(n == 0){ return 0;原创 2015-03-22 13:23:21 · 312 阅读 · 0 评论 -
leetcode surround regions
错误代码1:class Solution {public: const int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; bool isvalid(int tmph, int tmpw,const int h,const int w){ return (tmph > 0 && tmph < h - 1 && w > 0 &&原创 2015-03-22 17:11:05 · 476 阅读 · 0 评论 -
leetcode gas station
class Solution {public: inline int transId(const int id, const int size){ return id % size; } int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int size = gas.s原创 2015-03-29 19:39:07 · 337 阅读 · 0 评论 -
leetcode longest substring without repeating characters
思路1: greedy+hash,从左到右扫描 反例: abcade 思路2: 每次遇到重复字符时不是全部重新开始,而是弹出元素直到不重复class Solution {public: int lengthOfLongestSubstring(string s) { int len = s.length(); if(len == 0){原创 2015-03-22 03:07:48 · 398 阅读 · 0 评论 -
leetcode Minimum Window Substring
O(n * m) solution is ordinaryO(N)的想法就没有思路了。。 看了别人的思路,wa了4,5次才过class Solution {public: bool check(const vector<int>& cnts, const vector<int>&cntt){ for(int i = 0; i < 55; ++i){原创 2015-03-29 12:56:38 · 387 阅读 · 0 评论 -
leetcode multiply strings
class Solution {public: string uniMul(int v, string num2,int bias){ string ret = ""; for(int i = 0; i < bias; ++i){ ret += "0"; } int len2 = num2.length原创 2015-03-29 10:03:58 · 307 阅读 · 0 评论 -
leetcode regular expression match
问题1:状态的重复记录问题class Solution {public: vector<vector<bool> >dp; vector<vector<bool> >visit; bool dfs(const char*s, const char*p, int slen, int plen){ if(visit[slen][plen]){原创 2015-03-22 00:40:45 · 385 阅读 · 0 评论 -
leetcode Remove Elements
class Solution {public: int removeElement(int A[], int n, int elem) { int cur = 0; for(int i = 0; i < n; ++i){ if(A[i] != elem){ A[cur++] = A[i];原创 2015-03-21 11:31:54 · 384 阅读 · 0 评论 -
leetcode remove duplicates from sorted array
class Solution {public: int removeDuplicates(int A[], int n) { if(n <= 1){ return n; } int index = 0; for(int i = 1; i < n; ++i){ if(A[i] !=原创 2015-03-23 01:24:25 · 302 阅读 · 0 评论 -
leetcode Binary Tree Level Order Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solutio原创 2015-04-01 19:56:07 · 339 阅读 · 0 评论 -
leetcode Number of Islands
class Solution {public: const int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; bool valid(int x, int y, int h, int w, vector<vector<char> >&grid){ return x >= 0 && x < h && y >= 0 && y < w原创 2015-04-16 11:26:51 · 362 阅读 · 0 评论 -
leetcode Restore IP Address
class Solution {public: inline int getlen(int digit){ return digit == 0 ? 1 : (((int)log10(digit)) + 1); } void dfs(const string& s, const int totlen, int index, int cur, int partn原创 2015-04-16 00:37:37 · 402 阅读 · 0 评论 -
leetcode unique paths ii
class Solution {public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { int h = obstacleGrid.size(); if(h == 0){ return 0; } int w =原创 2015-04-08 16:38:08 · 321 阅读 · 0 评论 -
leetcode Remove Duplicates From Sorted List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *del原创 2015-04-08 21:20:16 · 349 阅读 · 0 评论 -
leetcode Insert Interval
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */class Sol原创 2015-04-08 21:00:48 · 342 阅读 · 0 评论 -
leetcode merge interval
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */class Sol原创 2015-04-08 18:12:07 · 423 阅读 · 0 评论 -
leetcode word ladder bfs
先贴代码:class Solution {public: //void expand(queue<string>&sq, ) int ladderLength(string start, string end, unordered_set<string> &dict) { int slen = start.length(); int rlen = 0原创 2015-04-08 13:36:51 · 363 阅读 · 0 评论 -
leetcode Set Matrix Zero
class Solution {public: void setZeroes(vector<vector<int> > &matrix) { int h = matrix.size(); if(h == 0){ return; } int w = matrix[0].size(); if原创 2015-04-08 23:35:14 · 422 阅读 · 0 评论 -
leetcode rotate image
class Solution {public: void rotate(vector<vector<int> > &matrix) { int n = matrix.size(); if(n < 2){ return; } int hl = 0, hr = n - 1, vu = 0, vd = n -原创 2015-04-08 19:52:45 · 332 阅读 · 0 评论 -
leetcode Sort Colors
class Solution {public: void sortColors(int A[], int n) { int cntZero = 0, cntTwo = 0; for(int i = 0; i < n; ++i){ cntZero += A[i] == 0 ? 1 : 0; cntTwo += A原创 2015-04-08 21:37:59 · 364 阅读 · 0 评论 -
leetcode reverse words in a string
class Solution {public: void reverseWords(string &s) { int begin = 0, end = s.length() - 1; if(begin > end){ s = ""; return; } int pointer =原创 2015-04-01 19:48:38 · 397 阅读 · 0 评论 -
leetcode search for a range
class Solution {public: vector<int> searchRange(int A[], int n, int target) { vector<int> ret; ret.push_back(-1); ret.push_back(-1); if(n < 1){ return r原创 2015-03-28 23:57:10 · 367 阅读 · 0 评论 -
leetcode Palindrome Number
错误1: sprintf打印一个整数时会造成前导零的问题class Solution {public: int getlen(int x){ int fx = abs(x); return fx == 0 ? 1 : (((int)log10(fx)) + 1); } bool isPalindrome(int x) {原创 2015-03-21 01:11:53 · 267 阅读 · 0 评论 -
leetcode Pascal's Palindrome 2
class Solution {public: vector<int> getRow(int rowIndex) { if(rowIndex == 0){ vector<int> ret; ret.push_back(1); return ret; } if(rowIn原创 2015-03-21 11:28:19 · 379 阅读 · 0 评论 -
leetcode dungeon game
开始用dfs,结果tle了 看了参考的解答 应该用DP 代码明天再写吧。。原创 2015-03-20 20:38:02 · 401 阅读 · 0 评论 -
leetcode Valid Palindrome
class Solution {public: bool isPalindrome(string s) { int len = s.length(); if(len == 0){ return true; } int left = 0, right = len - 1; for(int原创 2015-03-20 22:06:29 · 301 阅读 · 0 评论 -
leetcode Excel Sheet Column Title
class Solution {public: string convertToTitle(int n) { string res; const int maxlen = 1000; char tmpchar[maxlen]; memset(tmpchar, 0, sizeof(tmpchar)); long原创 2015-03-20 21:42:29 · 388 阅读 · 0 评论 -
非一次ac
class Solution {public: void rotate(int nums[], int n, int k) { k %= n; k = n - k; //这一句不要忘了.. int left = 0, right = k - 1; //while循环里也很容易忘记更新指针的值。。 while(l原创 2015-03-20 20:45:37 · 341 阅读 · 0 评论 -
leetcode Reverse Integer
class Solution {public: int reverse(int x) { const int maxlen = 100; char str[maxlen]; memset(str, 0, sizeof(str)); if(x == 0x80000000){ return 0;原创 2015-03-21 00:12:08 · 302 阅读 · 0 评论 -
leetcode Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *add原创 2015-03-20 23:48:55 · 335 阅读 · 0 评论 -
leetcode two sum
orz 连这道题也没AC真是给跪了class pairint{ public: int v, id; pairint(int v_,int id_):v(v_),id(id_){} bool operator < (const pairint& another) const{ if(v == another.v){ return原创 2015-03-20 20:55:20 · 330 阅读 · 0 评论 -
算法 之 有限状态自动机
这道题是从leetcode longest valid parentheses想到的 但是其实这道题和DMA的关系也不是很大… 先贴代码吧class Solution {public: int longestValidParentheses(string s) { int len = s.length(); int maxlen = 0;原创 2015-03-27 10:52:33 · 585 阅读 · 0 评论 -
leetcode Interleaving String
class Solution {public: bool isInterleave(string s1, string s2, string s3) { int len1 = s1.length(), len2 = s2.length(), len3 = s3.length(); if(len3 != len1 + len2){ re原创 2015-03-27 16:28:37 · 319 阅读 · 0 评论 -
leetcode Best Time to Buy and Sell
class Solution {public: int maxProfit(vector<int> &prices) { int ret = 0; vector<int> dppre, dppost; int size = prices.size(); if(size < 2){ return ret;原创 2015-03-26 09:29:38 · 351 阅读 · 0 评论 -
leetcode swap nodes in pairs
很简单的一道链表题,还是错了orz/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:原创 2015-03-26 00:46:56 · 349 阅读 · 0 评论 -
leetcode Compare Version Numbers
class Solution {public: int compareVersion(string version1, string version2) { int len1 = version1.length(), len2 = version2.length(); if(len1 == 0){ if(len2 == 0){原创 2015-03-27 17:10:17 · 339 阅读 · 0 评论