leetcode
恰似惊鸿踏雪泥
这个作者很懒,什么都没留下…
展开
-
KMP算法
KMP算法:前缀函数:对于长度为m的字符串s,前缀函数为π(i)(0<=i<m)表示s的子串s[0:i]的最长的相等的真前缀和真后缀的长度。特别地,如果不存在符合条件的前后缀,那么π(i)=0.真前缀,真后缀:不等于自身的前后缀。aabaaab: 0,1,0,1,2,2,3a ∅ ∅ 0aa a a 1aab a,aa ab,b 0aaba a,aa,aab a,ba,aba 1aabaa a,aa原创 2021-08-04 11:19:30 · 267 阅读 · 0 评论 -
leetcode1006:笨阶乘
class Solution {public: int myclumsy(int n,char op,long long int now1,long long int now2){ if(n==0) return now1-now2; if(op=='*'){ return myclumsy(n-1,'/',now1,now2*n); } else if(op=='/'){ return原创 2021-04-01 08:43:13 · 151 阅读 · 0 评论 -
leetcode173_3-28每日题:二叉搜索树迭代器
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} *原创 2021-03-28 13:50:41 · 161 阅读 · 0 评论 -
leetcode83:删除排序链表中的重复元素
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next)原创 2021-03-26 09:29:20 · 95 阅读 · 0 评论 -
leetcode82:删除排序链表中的重复元素II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next)原创 2021-03-25 23:20:56 · 89 阅读 · 0 评论 -
leetcode456_3-24每日题:132模式
class Solution {public: bool find132(vector<int>& nums,int start,int end){ if(end-start+1<3) return false; int max = nums[start]; int maxn = start; for(int i=start+1;i<=end;i++){ maxn = nums[原创 2021-03-24 13:13:57 · 106 阅读 · 0 评论 -
leetcode341_3-23每日题:扁平化嵌套列表迭代器
/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Return true if this NestedInteger holds a single integer, rather原创 2021-03-23 22:31:21 · 113 阅读 · 0 评论 -
leetcode191:位1的个数
class Solution {public: int hammingWeight(uint32_t n) { int res = 0; while(n!=0){ if(n&1==1) res+=1; n>>=1; } return res; }};原创 2021-03-22 10:09:24 · 101 阅读 · 0 评论 -
leetcode150:逆波兰表达式求值
class Solution {public: int evalRPN(vector<string>& tokens) { stack<int> num; for(int i=0;i<tokens.size();i++){ if(tokens[i]!="+"&&tokens[i]!="-"&&tokens[i]!="*"&&tokens[i]!="/"){原创 2021-03-20 16:09:27 · 94 阅读 · 0 评论 -
leetcode1603_3-19每日题:设计停车系统
class ParkingSystem {public: int big_park; int med_park; int sma_park; ParkingSystem(int big, int medium, int small):big_park(big),med_park(medium),sma_park(small) { } bool addCar(int carType) { switch(carType){原创 2021-03-19 10:33:58 · 129 阅读 · 0 评论 -
leetcode92_3-18每日题:反转链表II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next)原创 2021-03-18 10:14:53 · 97 阅读 · 0 评论 -
leetcode115_3-17每日题:不同子序列
class Solution {public: int numDistinct(string s, string t) { int answer = 0; map<char,int> m; for(int i=0;i<t.length();i++) m[t[i]] += 1; string s1; for(int i=0;i<s.length();i++){ if(m[s原创 2021-03-17 10:46:38 · 109 阅读 · 0 评论 -
leetcode76:最小覆盖子串
class Solution {public: string minWindow(string s, string t) { map<char,int> m; string s1 = s; for(int i=0;i<t.length();i++){ m[t[i]] += 1; } for(int i=0;i<s.length();i++){ if原创 2021-03-15 15:36:08 · 115 阅读 · 0 评论 -
leetcode75:颜色分类
class Solution {public: void sortColors(vector<int>& nums) { sort(nums.begin(),nums.end()); }};原创 2021-03-15 11:07:51 · 122 阅读 · 0 评论 -
leetcode706_3-14每日题:设计哈希映射
class MyHashMap {public: /** Initialize your data structure here. */ int *k[1000001]={nullptr}; stack<int> s; MyHashMap() { } /** value will always be non-negative. */ void put(int key, int value) { s.push(va原创 2021-03-14 14:36:03 · 88 阅读 · 0 评论 -
leetcode331_3-12每日题:验证二叉树的前序序列化
class Solution {public: bool isValidSerialization(string preorder) { if(preorder == "#") return true; stack<int> s; map<int,int> m; for(int i=0;i<preorder.length();i++){ if(preorder[i]==' ') c原创 2021-03-12 10:31:29 · 104 阅读 · 0 评论 -
leetcode74:搜索二维数组
class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int left = 0, right = matrix.size()-1; while(left<right){ int mid = (left+right)/2; if(matrix[mid][0]>ta原创 2021-03-11 15:08:51 · 138 阅读 · 0 评论 -
leetcode227:基本计算器II
class Solution {public: int calculate(string s) { stack<int> num; stack<char> op; stack<int> n_num; queue<char> n_op; for(int i=0;i<s.length();i++){ if(s[i]==' ') continue;原创 2021-03-11 14:44:57 · 114 阅读 · 0 评论 -
leetcode224_3-10每日题:基本计算器
class Solution {public: int calculate(string s) { stack<char> op; stack<int> num; for(int i=0;i<s.length();i++){ if(s[i]=='(') op.push(s[i]); else if(s[i]==' ') continue; else原创 2021-03-10 12:49:40 · 80 阅读 · 0 评论 -
leetcode73:矩阵置零
class Solution {public: void setZeroes(vector<vector<int>>& matrix) { map<pair<int,int>,int> m; for(int i=0;i<matrix.size();i++){ for(int j=0;j<matrix[0].size();j++){ if(mat原创 2021-03-09 22:11:24 · 90 阅读 · 0 评论 -
leetcode72:编辑距离
class Solution {public: int minDistance(string word1, string word2) { int dp[500][500]; int m = word1.length(); int n = word2.length(); if(m==0||n==0) return max(m,n); if(word1[0]==word2[0]) dp[0][0] = 0;原创 2021-03-09 21:37:23 · 103 阅读 · 0 评论 -
leetcode71:简化路径
class Solution {public: string simplifyPath(string path) { string res="/"; for(int i=0;i<path.length();i++){ if(path[i]=='/'&&res[res.length()-1]=='/') continue; else if((i-1<0||(i-1>=0&&原创 2021-03-09 20:45:09 · 92 阅读 · 0 评论 -
leetcode69:x的平方根
class Solution {public: int mySqrt(int x) { if(x == 0) return 0; if(x == 1) return 1; int left = 0,right = x/2+1; while(left<right){ int mid = (left+right)/2; if(mid > x/mid) right = mid-1;原创 2021-03-09 19:55:10 · 65 阅读 · 0 评论 -
leetcode68:文本左右对齐
class Solution {public: vector<string> fullJustify(vector<string>& words, int maxWidth) { vector<vector<string> > answer; vector<string> now; int nowwidth=0; for(int i=0;i<words.size原创 2021-03-09 19:45:54 · 96 阅读 · 0 评论 -
leetcode67:二进制求和
class Solution {public: string addBinary(string a, string b) { int n_a = a.length(); int n_b = b.length(); int num_a = max(n_a,n_b); int num_b = min(n_a,n_b); string res; reverse(a.begin(),a.end());原创 2021-03-09 19:09:38 · 89 阅读 · 0 评论 -
leetcode1047_3-9每日题:删除字符串中所有相邻重复项
class Solution {public: string removeDuplicates(string S) { for(int i=1;i<S.length();i++){ if(i-1>=0&&S[i]==S[i-1]) { S.erase(i-1,2); i-=2; } } return S;原创 2021-03-09 18:10:36 · 164 阅读 · 0 评论 -
leetcode66:加一
class Solution {public: vector<int> plusOne(vector<int>& digits) { int res=0; for(int i = digits.size()-1;i>=0;i--){ if(i==digits.size()-1)digits[i] +=1; else digits[i]+=res; res = 0; if(digi原创 2021-03-08 17:16:59 · 97 阅读 · 0 评论 -
leetcode65:有效数字
class Solution {public: bool isNumber(string s) { bool flage=false; int pose=-1; bool flagd=false; for(int i=0;i<s.length();i++){ if((i==0&&(s[i]=='+'||s[i]=='-'))|| (i==pose+1&&am原创 2021-03-08 16:24:40 · 142 阅读 · 0 评论 -
leetcode132_3-8每日题:分割回文串II
class Solution {public: bool huiwen(string s){ for(int i=0;i<s.length();i++){ if(s[i]!=s[s.length()-1-i]) return false; } return true; } int minCut(string s) { int n = s.length(); int dp[原创 2021-03-08 13:12:33 · 92 阅读 · 0 评论 -
leetcode131_3-7每日题:分割回文串
class Solution {public: bool huiwen(string s){ for(int i=0;i<s.length();i++){ if(s[i]!=s[s.length()-1-i]) return false; } return true; } vector<vector<string> > answer; void func(string s原创 2021-03-07 13:06:34 · 99 阅读 · 0 评论 -
leetcode503_3-6每日题:下一个更大元素II
class Solution {public: vector<int> nextGreaterElements(vector<int>& nums) { stack<pair<int,int> > s1; vector<int> answer; map<int,int> m; bool flag = true; for(int i=0;m.原创 2021-03-06 22:33:09 · 85 阅读 · 0 评论 -
leetcode70:爬楼梯
class Solution {public: int climbStairs(int n) { if(n==0) return 0; if(n==1) return 1; int dp[n]; dp[0] = 1; dp[1] = 2; for(int i=2;i<n;i++){ dp[i] = dp[i-1]+dp[i-2]; } ret原创 2021-03-06 22:31:46 · 103 阅读 · 0 评论 -
leetcode232_3-5每日题:用栈实现队列
class MyQueue {public: stack<int> s1,s2; /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { s1.push(x); } /** Removes the el原创 2021-03-05 10:40:19 · 158 阅读 · 1 评论 -
leetcode354_3-4每日题:俄罗斯套娃信封问题
class Solution {public: static bool compare(vector<int> a,vector<int> b){ if(a[0]<b[0]) return true; else if(a[0]>b[0]) return false; else{ if(a[1]<b[1]) return true; else return fal原创 2021-03-04 12:07:00 · 195 阅读 · 1 评论 -
leetcode338_3-3每日题:比特位计数
class Solution {public: vector<int> countBits(int num) { vector<int> res; res.push_back(0); if(num >= 1){ res.push_back(1); } int n = num; while(num!=0){ num>&g原创 2021-03-03 19:07:23 · 112 阅读 · 1 评论 -
leetcode203:移除链表元素
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next)原创 2021-03-03 00:15:51 · 102 阅读 · 0 评论 -
leetcode304_3-2每日题:二维区域和检索
class NumMatrix {public: int** num; NumMatrix(vector<vector<int>>& matrix) { if(!matrix.empty()&&!matrix[0].empty()){ int m = matrix.size(); int n = matrix[0].size(); num = new in原创 2021-03-02 16:16:02 · 87 阅读 · 0 评论 -
leetcode303_3-1每日题:区域和检索
class NumArray {public: vector<int> num; NumArray(vector<int>& nums) :num(nums){ } int sumRange(int i, int j) { int res=0; for(;i<=j;i++){ res+=num[i]; } return res;原创 2021-03-01 16:39:28 · 76 阅读 · 0 评论 -
leetcode896:单调数列
class Solution {public: bool isMonotonic(vector<int>& A) { int flag=0; if(A.size()<=1) return true; for(int i=1;i<A.size();i++){ if(A[i]>A[i-1]){ if(flag==0) flag = 1;原创 2021-02-28 10:52:04 · 68 阅读 · 0 评论 -
leetcode63:不同路径II
class Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { if(obstacleGrid.empty()) return 0; if(obstacleGrid[0].empty()) return 0; if(obstacleGrid[0][0]==1) return 0; int原创 2021-02-27 17:18:46 · 99 阅读 · 0 评论