04 2020 档案

摘要:1 //f(n) 表示从左到右(forward)的最终结果 2 //b(n) 表示从右到左(backward)的最终结果 3 class Solution 4 { 5 public: 6 int leftToRight(int n) 7 { 8 if (n == 1) return 1; 9 els 阅读全文
posted @ 2020-04-30 14:52 Jinxiaobo0509 阅读(125) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 double mincostToHireWorkers(vector<int>& quality, vector<int>& wage, int K) 5 { 6 vector<pair<double,int>> vec; 7 for 阅读全文
posted @ 2020-04-30 10:36 Jinxiaobo0509 阅读(225) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 void calc(stack<char>& op,stack<int>& num) 4 { 5 int y = num.top(); 6 num.pop(); 7 int x = num.top(); 8 num.pop(); 9 if(op.top( 阅读全文
posted @ 2020-04-29 22:41 Jinxiaobo0509 阅读(205) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<vector<int>> insert(vector<vector<int>>& nums, vector<int>& newInterval) 5 { 6 nums.push_back(newInterval); 7 阅读全文
posted @ 2020-04-29 21:55 Jinxiaobo0509 阅读(148) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int numSubarraysWithSum(vector<int>& nums, int k) 5 { 6 unordered_map<int,int> hash;// 和+次数 7 hash[0] = 1; 8 9 int re 阅读全文
posted @ 2020-04-29 21:13 Jinxiaobo0509 阅读(157) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int projectionArea(vector<vector<int>>& grid) 5 { 6 int m = grid.size(),n = grid[0].size(); 7 int res = 0; 8 for(int 阅读全文
posted @ 2020-04-29 18:09 Jinxiaobo0509 阅读(132) 评论(0) 推荐(0)
摘要:1 // nums[0] = 9; // 1-9 2 // nums[1] = 90*2; // 10-99 3 // nums[2] = 900*3; // 100-999 4 // nums[3] = 9000*4; // 1000-9999 5 // nums[4] = 90000*5; // 阅读全文
posted @ 2020-04-29 16:58 Jinxiaobo0509 阅读(121) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<double> calcEquation(vector<vector<string>> &equations, vector<double> &values, vector<vector<string>> &querie 阅读全文
posted @ 2020-04-28 18:59 Jinxiaobo0509 阅读(137) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 unordered_map<int,vector<int>> hash;// 对应的数 + /下标(可能有重复数)/ 4 public: 5 Solution(vector<int>& nums) 6 { 7 for(int i = 0;i < nums 阅读全文
posted @ 2020-04-28 18:39 Jinxiaobo0509 阅读(83) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int integerReplacement(int n) 5 { 6 return DFS(n); 7 } 8 9 long long DFS(long long n) 10 { 11 if(n == 1) return 0; 12 阅读全文
posted @ 2020-04-28 18:24 Jinxiaobo0509 阅读(80) 评论(0) 推荐(0)
摘要:1 // F[i + 1] - F[i] = sum - n * A[n - 1 - i] 2 class Solution 3 { 4 public: 5 int maxRotateFunction(vector<int>& A) 6 { 7 if(A.empty()) return 0; 8 i 阅读全文
posted @ 2020-04-28 17:53 Jinxiaobo0509 阅读(223) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int longestSubstring(string s, int k) 5 { 6 if(s.empty()) return 0; 7 vector<int> hash(26); 8 for(auto a : s) hash[a 阅读全文
posted @ 2020-04-28 12:19 Jinxiaobo0509 阅读(85) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool validUtf8(vector<int>& data) 5 { 6 int n = 0; 7 for(int i = 0;i < data.size();i ++) 8 { 9 if(n > 0) 10 { 11 if(d 阅读全文
posted @ 2020-04-27 22:54 Jinxiaobo0509 阅读(179) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 char findTheDifference(string s, string t) 5 { 6 unordered_map<char,int> hash; 7 for(auto a : s) hash[a] ++; 8 for(au 阅读全文
posted @ 2020-04-27 17:49 Jinxiaobo0509 阅读(114) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int firstUniqChar(string s) 5 { 6 unordered_map<char,int> hash; 7 for(auto a : s) hash[a] ++; 8 for(int i = 0;i < s.s 阅读全文
posted @ 2020-04-27 16:00 Jinxiaobo0509 阅读(96) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> lexicalOrder(int n) 5 { 6 set<string> hash; 7 vector<int> res; 8 for(int i = 1;i <= n;i ++) 9 { 10 hash.i 阅读全文
posted @ 2020-04-27 15:54 Jinxiaobo0509 阅读(111) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 vector<int> vec; 4 public: 5 Solution(vector<int>& nums) 6 { 7 vec = nums; 8 } 9 10 /** Resets the array to its original config 阅读全文
posted @ 2020-04-27 15:26 Jinxiaobo0509 阅读(239) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool canConstruct(string ransomNote, string magazine) 5 { 6 unordered_map<char,int> hash_r,hash_m; 7 for(auto a : mag 阅读全文
posted @ 2020-04-27 14:20 Jinxiaobo0509 阅读(90) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 ListNode* nod; 5 Solution(ListNode* head) 6 { 7 this->nod = head; 8 } 9 10 int getRandom() 11 { 12 if(nod == NULL) re 阅读全文
posted @ 2020-04-27 14:06 Jinxiaobo0509 阅读(101) 评论(0) 推荐(0)
摘要:1 class RandomizedSet 2 { 3 unordered_map<int,int> hash; 4 vector<int> nums; 5 public: 6 /** Initialize your data structure here. */ 7 RandomizedSet() 阅读全文
posted @ 2020-04-27 11:22 Jinxiaobo0509 阅读(110) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int kthSmallest(vector<vector<int>>& matrix, int k) 5 { 6 priority_queue<int,vector<int>,greater<int>> pq; 7 int n = 阅读全文
posted @ 2020-04-27 09:59 Jinxiaobo0509 阅读(131) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 vector<vector<int>> memo; 4 public: 5 int getMoneyAmount(int n) 6 { 7 memo = vector<vector<int>>(n + 1,vector<int>(n + 1,-1)); 阅读全文
posted @ 2020-04-27 09:24 Jinxiaobo0509 阅读(132) 评论(0) 推荐(0)
摘要:1 /** 2 * Forward declaration of guess API. 3 * @param num your guess 4 * @return -1 if num is lower than the guess number 5 * 1 if num is higher than 阅读全文
posted @ 2020-04-26 19:26 Jinxiaobo0509 阅读(97) 评论(0) 推荐(0)
摘要:1 struct cmp 2 { 3 bool operator ()(vector<int> &a, const vector<int> &b) 4 { 5 // < :大顶堆 6 // > :小顶堆 7 return a[0]+a[1] > b[0]+b[1]; 8 } 9 }; 10 prio 阅读全文
posted @ 2020-04-26 19:01 Jinxiaobo0509 阅读(107) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 struct cmp 4 { 5 bool operator ()(vector<int> &a, const vector<int> &b) 6 { 7 // < :大顶堆 8 // > :小顶堆 9 return a[0]+a[1] > b[0]+b 阅读全文
posted @ 2020-04-26 19:00 Jinxiaobo0509 阅读(119) 评论(0) 推荐(0)
摘要:1 for (int i=arr.size()-1;i>=0;--i) 2 { 3 srand((unsigned)time(NULL)); 4 swap(arr[rand()%(i+1)],arr[i]); 5 } [0,i]中随机选一个数,与第i个数交换 阅读全文
posted @ 2020-04-25 18:56 Jinxiaobo0509 阅读(145) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> findAnagrams(string s, string t) 5 { 6 unordered_map<char, int> need, window; 7 for (char c : t) need[c]+ 阅读全文
posted @ 2020-04-25 11:15 Jinxiaobo0509 阅读(121) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 // 判断 s 中是否存在 t 的排列 5 bool checkInclusion(string t, string s) 6 { 7 unordered_map<char, int> need, window; 8 for (cha 阅读全文
posted @ 2020-04-25 11:02 Jinxiaobo0509 阅读(134) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> maxSlidingWindow(vector<int>& nums, int k) 5 { 6 vector<int> res; 7 deque<int> q; 8 for(int i = 0;i < num 阅读全文
posted @ 2020-04-24 22:34 Jinxiaobo0509 阅读(136) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 string plusOne(string s, int j) // 将 s[j] 向上拨动一次 5 { 6 if(s[j] == '9') s[j] = '0'; 7 else s[j] += 1; 8 return s; 9 } 阅读全文
posted @ 2020-04-24 16:07 Jinxiaobo0509 阅读(169) 评论(0) 推荐(0)
摘要:1 // 计算从起点 start 到终点 target 的最近距离 2 int BFS(Node start, Node target) { 3 Queue<Node> q; // 核心数据结构 4 Set<Node> visited; // 避免走回头路,大部分时候都是必须的,但是像一般的二叉树结 阅读全文
posted @ 2020-04-24 14:55 Jinxiaobo0509 阅读(476) 评论(0) 推荐(0)
摘要:1、利用异或操作 ^ 和空格进行英文字符大小写互换 ('d' ^ ' ') = 'D' ('D' ^ ' ') = 'd' 2、判断两个数是否异号 int x = -1, y = 2; bool f = ((x ^ y) < 0); // true int x = 3, y = 2;bool f = 阅读全文
posted @ 2020-04-24 10:57 Jinxiaobo0509 阅读(116) 评论(0) 推荐(0)
摘要:滑动窗口算法的思路是这样: 1、我们在字符串 S 中使用双指针中的左右指针技巧,初始化 left = right = 0,把索引闭区间 [left, right] 称为一个「窗口」。 2、我们先不断地增加 right 指针扩大窗口 [left, right],直到窗口中的字符串符合要求(包含了 T 阅读全文
posted @ 2020-04-24 10:45 Jinxiaobo0509 阅读(218) 评论(0) 推荐(0)
摘要:1 class UF 2 { 3 public: 4 int count; // 记录连通分量个数 5 vector<int> parent; // 存储若干棵树 6 vector<int> size; // 记录树的“重量” 7 8 UF(int n) 9 { 10 count = n; 11 p 阅读全文
posted @ 2020-04-23 22:45 Jinxiaobo0509 阅读(222) 评论(0) 推荐(0)
摘要:1 bool check(int x) {/* ... */} // 检查x是否满足某种性质 2 3 // 区间[l, r]被划分成[l, mid]和[mid + 1, r]时使用: 4 int bsearch_1(int l, int r) 5 { 6 while (l < r) 7 { 8 in 阅读全文
posted @ 2020-04-23 16:34 Jinxiaobo0509 阅读(151) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 int base = 1337; 4 5 int mypow(int a, int k) 6 { 7 if (k == 0) return 1; 8 a %= base; 9 10 if (k % 2 == 1) // k 是奇数 11 { 12 ret 阅读全文
posted @ 2020-04-23 14:47 Jinxiaobo0509 阅读(171) 评论(0) 推荐(0)
摘要:1 //最长上升子序列的变种 2 class Solution 3 { 4 public: 5 vector<int> largestDivisibleSubset(vector<int>& nums) 6 { 7 vector<int> ans;//保存结果 8 if(nums.size() == 阅读全文
posted @ 2020-04-23 10:08 Jinxiaobo0509 阅读(188) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isPerfectSquare(int num) 5 { 6 if(num == 1) return true; 7 for(int i = 1;i <= num/2;i ++) 8 { 9 if((long long)i 阅读全文
posted @ 2020-04-22 23:31 Jinxiaobo0509 阅读(108) 评论(0) 推荐(0)
摘要:1 // 而贝祖定理告诉我们,ax+by=z 有解当且仅当z是x,y的最大公约数的倍数。 2 // 因此我们只需要找到x,y的最大公约数并判断z是否是它的倍数即可。 3 4 //纯粹的数学问题 5 class Solution 6 { 7 public: 8 bool canMeasureWater 阅读全文
posted @ 2020-04-22 23:20 Jinxiaobo0509 阅读(87) 评论(0) 推荐(0)
摘要:1 int gcd(int a, int b) 2 { 3 return b ? gcd(b, a % b) : a; 4 } 阅读全文
posted @ 2020-04-22 23:18 Jinxiaobo0509 阅读(88) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int countNumbersWithUniqueDigits(int n) 5 { 6 if(n >= 11) return 8877691; 7 vector<int> nums(11); 8 int res = 0; 9 nu 阅读全文
posted @ 2020-04-22 23:01 Jinxiaobo0509 阅读(126) 评论(0) 推荐(0)
摘要:1 class Twitter 2 { 3 public: 4 unordered_map<int,priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>> u;//用户 -> (出现的次数,推文) 小根堆 阅读全文
posted @ 2020-04-22 22:29 Jinxiaobo0509 阅读(129) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> intersect(vector<int>& nums1, vector<int>& nums2) 5 { 6 vector<int> res; 7 unordered_map<int,int> hash1,h 阅读全文
posted @ 2020-04-22 19:31 Jinxiaobo0509 阅读(96) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 5 { 6 vector<int> res; 7 unordered_map<int,int> hash 阅读全文
posted @ 2020-04-22 19:25 Jinxiaobo0509 阅读(97) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> topKFrequent(vector<int>& nums, int k) 5 { 6 vector<int> res; 7 unordered_map<int,int> hash; 8 for(auto a 阅读全文
posted @ 2020-04-22 19:19 Jinxiaobo0509 阅读(97) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 unordered_set<char> hash = {'a','e','i','o','u','A','E','I','O','U'}; 4 public: 5 string reverseVowels(string s) 6 { 7 int n = 阅读全文
posted @ 2020-04-22 19:08 Jinxiaobo0509 阅读(122) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 void reverseString(vector<char>& s) 5 { 6 int n = s.size(); 7 for(int i = 0;i < n/2;i ++) swap(s[i],s[n - i - 1]); 8 阅读全文
posted @ 2020-04-22 18:57 Jinxiaobo0509 阅读(92) 评论(0) 推荐(0)
摘要:1 //以3来进行划分 2 class Solution 3 { 4 public: 5 int integerBreak(int n) 6 { 7 if(n <= 3) return n - 1; //必须切一刀 8 //n >= 5 2*(n-2) > n 3*(n-3) > n 且3*(n-3 阅读全文
posted @ 2020-04-22 18:53 Jinxiaobo0509 阅读(164) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> countBits(int num) 5 { 6 vector<int> res; 7 for(int i = 0;i <= num;i ++) 8 { 9 int count = 0; 10 for(int 阅读全文
posted @ 2020-04-22 18:12 Jinxiaobo0509 阅读(90) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-22 11:44 Jinxiaobo0509 阅读(101) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool increasingTriplet(vector<int>& nums) 5 { 6 int len = nums.size(); 7 if (len < 3) return false; 8 int small = INT 阅读全文
posted @ 2020-04-22 11:26 Jinxiaobo0509 阅读(102) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 //欧拉路径 5 vector<string> findItinerary(vector<vector<string>> &tickets) 6 { 7 unordered_map<string, multiset<string>> 阅读全文
posted @ 2020-04-22 10:07 Jinxiaobo0509 阅读(410) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 //欧拉路径 5 vector<string> findItinerary(vector<vector<string>> &tickets) 6 { 7 unordered_map<string, multiset<string>> 阅读全文
posted @ 2020-04-22 10:06 Jinxiaobo0509 阅读(135) 评论(0) 推荐(0)
摘要:1 //空节点的个数 = 非空节点个数 + 1 2 class Solution 3 { 4 vector<string> res; 5 void spilt(string s,char c) 6 { 7 istringstream iss(s); 8 string temp; 9 while(ge 阅读全文
posted @ 2020-04-21 22:21 Jinxiaobo0509 阅读(167) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 阅读全文
posted @ 2020-04-21 17:58 Jinxiaobo0509 阅读(112) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isPowerOfFour(int num) 5 { 6 return (num > 0) && ((num & (-num)) == num) && (num % 3 == 1); 7 } 8 }; 阅读全文
posted @ 2020-04-21 17:44 Jinxiaobo0509 阅读(90) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isPowerOfThree(int n) 5 { 6 return n > 0 && 1162261467 % n == 0; 7 } 8 }; 阅读全文
posted @ 2020-04-21 17:25 Jinxiaobo0509 阅读(78) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 void wiggleSort(vector<int>& nums) 5 { 6 int n = nums.size(); 7 sort(nums.begin(),nums.end()); 8 vector<int> temp; 9 阅读全文
posted @ 2020-04-21 16:48 Jinxiaobo0509 阅读(90) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int coinChange(vector<int>& coins, int amount) 5 { 6 vector<long long> dp(amount + 1,INT_MAX); 7 dp[0] = 0;//总金额为0元所需 阅读全文
posted @ 2020-04-21 14:42 Jinxiaobo0509 阅读(105) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int bulbSwitch(int n) 5 { 6 return sqrt(n); 7 } 8 }; 阅读全文
posted @ 2020-04-21 11:42 Jinxiaobo0509 阅读(119) 评论(0) 推荐(0)
摘要:1、暴力求解 1 class Solution 2 { 3 public: 4 int maxProduct(vector<string>& words) 5 { 6 int n = words.size(); 7 int res = 0; 8 for(auto& a : words) sort(a 阅读全文
posted @ 2020-04-21 10:54 Jinxiaobo0509 阅读(143) 评论(0) 推荐(0)
摘要:1 //跟264题差不多 2 class Solution 3 { 4 public: 5 int nthSuperUglyNumber(int n, vector<int>& primes) 6 { 7 vector<int> dp(n); 8 dp[0] = 1; 9 vector<int> p 阅读全文
posted @ 2020-04-20 23:32 Jinxiaobo0509 阅读(160) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) 5 { 6 vector<int> ans; 7 if(n == 1) return {0}; 8 v 阅读全文
posted @ 2020-04-20 22:34 Jinxiaobo0509 阅读(143) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 unordered_map<string,vector<char>> hash; 4 public: 5 bool pyramidTransition(string bottom, vector<string>& allowed) 6 { 7 for(a 阅读全文
posted @ 2020-04-20 14:39 Jinxiaobo0509 阅读(207) 评论(0) 推荐(0)
摘要:1 /** 2 * // This is the interface that allows for creating nested lists. 3 * // You should not implement it, or speculate about its implementation 4 阅读全文
posted @ 2020-04-20 10:27 Jinxiaobo0509 阅读(120) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 string decodeString(string s) 5 { 6 int len = s.size(); 7 int num = 0; 8 stack<int> numstack; 9 stack<string> strstac 阅读全文
posted @ 2020-04-20 10:18 Jinxiaobo0509 阅读(116) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<string> letterCasePermutation(string S) 5 { 6 vector<string> res; 7 backTracking(res,S,0); 8 return res; 9 } 1 阅读全文
posted @ 2020-04-19 17:55 Jinxiaobo0509 阅读(169) 评论(0) 推荐(0)
摘要:1 //hold 持股1、昨天持股,今天什么都不做 2、昨天冻结,今天买入 2 //sold 不持股1、昨天不持股,今天什么都不做 2、昨天持股,今天卖出 3 //rest 冻结1、昨天持股,今天卖出 4 class Solution 5 { 6 public: 7 int maxProfit(ve 阅读全文
posted @ 2020-04-19 17:35 Jinxiaobo0509 阅读(118) 评论(0) 推荐(0)
摘要:1 //1与2交替出现,从下标2开始 2 class Solution 3 { 4 public: 5 int magicalString(int n) 6 { 7 string s = "122"; 8 for(int i = 2,k = 1;i < n;i ++,k = 3 - k) 9 { 1 阅读全文
posted @ 2020-04-19 10:02 Jinxiaobo0509 阅读(103) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 string convertToBase7(int num) 5 { 6 string res; 7 bool a = true; 8 if(num < 0) num = -num,a = false; 9 while(num) 10 阅读全文
posted @ 2020-04-18 22:51 Jinxiaobo0509 阅读(103) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 string removeKdigits(string num, int k) 5 { 6 if(num.size() == k) return "0"; 7 stack<char> stk;//单调栈 8 string res; 9 阅读全文
posted @ 2020-04-18 21:31 Jinxiaobo0509 阅读(103) 评论(0) 推荐(0)
摘要:给你一个数组 [2,1,2,4,3],你返回数组 [4,2,4,-1,-1]。 解释:第一个 2 后面比 2 大的数是 4; 1 后面比 1 大的数是 2;第二个 2 后面比 2 大的数是 4; 4 后面没有比 4 大的数,填 -1;3 后面没有比 3 大的数,填 -1。 1、右边第一个数比它大,从 阅读全文
posted @ 2020-04-18 21:30 Jinxiaobo0509 阅读(118) 评论(0) 推荐(0)
摘要:1 bool cmp(vector<int>& a,vector<int>& b) 2 { 3 //第一个数从小到大排列 如果第一个数相等,则第二个数从小到大排列 4 return a[0] < b[0] || ((a[0] == b[0]) && (a[1] < b[1])); 5 } 6 7 c 阅读全文
posted @ 2020-04-18 17:11 Jinxiaobo0509 阅读(120) 评论(0) 推荐(0)
摘要:1 bool cmp(vector<int>& a,vector<int>& b) 2 { 3 //第一个数从大到小排列 如果第一个数相等,则第二个数从小到大排列 4 return a[0] > b[0] || ((a[0] == b[0]) && (a[1] < b[1])); 5 } 6 7 c 阅读全文
posted @ 2020-04-18 16:47 Jinxiaobo0509 阅读(114) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int wiggleMaxLength(vector<int>& nums) 5 { 6 //删除相邻且相同的元素 7 nums.erase(unique(nums.begin(),nums.end()),nums.end()); 8 阅读全文
posted @ 2020-04-18 16:03 Jinxiaobo0509 阅读(94) 评论(0) 推荐(0)
摘要:1 //BFS + 贪心 2 //维护一个区间[l,r],在这里面可以找到能够跳到最大位置,step++,同时更新l,r 3 class Solution 4 { 5 public: 6 int jump(vector<int>& nums) 7 { 8 if(nums.size() < 2) re 阅读全文
posted @ 2020-04-18 14:57 Jinxiaobo0509 阅读(90) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int findContentChildren(vector<int>& g, vector<int>& s) 5 { 6 sort(g.rbegin(),g.rend()); 7 sort(s.rbegin(),s.rend()); 阅读全文
posted @ 2020-04-18 11:24 Jinxiaobo0509 阅读(69) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isSubsequence(string s, string t) 5 { 6 int n = s.size(); 7 int i = 0; 8 for(int j = 0;j < t.size();j ++) 9 { 10 阅读全文
posted @ 2020-04-18 11:06 Jinxiaobo0509 阅读(105) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool lemonadeChange(vector<int>& bills) 5 { 6 unordered_map<int,int> hash; 7 for(auto a : bills) 8 { 9 if(a == 5) has 阅读全文
posted @ 2020-04-18 10:37 Jinxiaobo0509 阅读(147) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 int temp; 4 public: 5 vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) 6 { 7 int m = ima 阅读全文
posted @ 2020-04-17 22:54 Jinxiaobo0509 阅读(196) 评论(0) 推荐(0)
摘要:1 struct Node 2 { 3 Node* next[2] = {nullptr}; 4 }; 5 6 class Solution 7 { 8 public: 9 void insert(int num, Node* root) 10 { 11 for (int i = 30; i >= 阅读全文
posted @ 2020-04-17 21:39 Jinxiaobo0509 阅读(197) 评论(0) 推荐(0)
摘要:1 //0-31位中 每一位0的个数与1的个数乘积之和 2 class Solution 3 { 4 public: 5 int totalHammingDistance(vector<int>& nums) 6 { 7 int res = 0; 8 for(int i = 0;i < 32;i + 阅读全文
posted @ 2020-04-17 17:05 Jinxiaobo0509 阅读(137) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int getSum(int a, int b) 5 { 6 if(!b) return a; 7 int sum = a ^ b,carry = (unsigned int)(a & b) << 1;//LeetCode编译器不支持 阅读全文
posted @ 2020-04-17 16:02 Jinxiaobo0509 阅读(89) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int findComplement(int num) 5 { 6 int res = 0,t = 0; 7 while(num) 8 { 9 res += !(num & 1) << t; 10 num >>= 1,t ++; 11 阅读全文
posted @ 2020-04-17 11:30 Jinxiaobo0509 阅读(98) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 unordered_set<int> hash = {2,3,5,7,11,13,17,19}; 5 int countPrimeSetBits(int L, int R) 6 { 7 int res = 0; 8 for(int i 阅读全文
posted @ 2020-04-17 10:59 Jinxiaobo0509 阅读(186) 评论(0) 推荐(0)
摘要:1 //确定前三个数 2 class Solution 3 { 4 public: 5 vector<int> splitIntoFibonacci(string num) 6 { 7 vector<int> res; 8 int n = num.size(); 9 for(int i = 0;i 阅读全文
posted @ 2020-04-17 09:37 Jinxiaobo0509 阅读(154) 评论(0) 推荐(0)
摘要:1 class NumArray 2 { 3 vector<int> num; 4 public: 5 NumArray(vector<int>& nums) 6 { 7 num = nums; 8 } 9 10 void update(int i, int val) 11 { 12 num[i] 阅读全文
posted @ 2020-04-16 23:29 Jinxiaobo0509 阅读(102) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isAdditiveNumber(string num) 5 { 6 int n = num.size(); 7 for(int i = 0;i < n - 2;i++) 8 { 9 if(i != 0 && num[0] 阅读全文
posted @ 2020-04-16 23:10 Jinxiaobo0509 阅读(115) 评论(0) 推荐(0)
摘要:1 class NumMatrix 2 { 3 vector<vector<int>> s; 4 public: 5 NumMatrix(vector<vector<int>>& matrix) 6 { 7 if(matrix.empty() || matrix[0].empty()) return 阅读全文
posted @ 2020-04-16 17:27 Jinxiaobo0509 阅读(144) 评论(0) 推荐(0)
摘要:1 class NumArray 2 { 3 vector<int> sums; 4 public: 5 6 NumArray(vector<int>& nums) 7 { 8 if(nums.size() == 0) return; 9 int n = nums.size(); 10 sums = 阅读全文
posted @ 2020-04-16 16:35 Jinxiaobo0509 阅读(114) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 //完全背包问题 5 int change(int amount, vector<int>& coins) 6 { 7 vector<int>dp(amount + 1,0); 8 dp[0] = 1; 9 for(int i = 0 阅读全文
posted @ 2020-04-16 14:53 Jinxiaobo0509 阅读(131) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int minDistance(string word1, string word2) 5 { 6 int m = word1.size(); 7 int n = word2.size(); 8 9 vector<vector<int 阅读全文
posted @ 2020-04-16 10:36 Jinxiaobo0509 阅读(110) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int rob(vector<int>& nums) 5 { 6 if(nums.size() == 0) return 0; 7 if(nums.size() == 1) return nums[0]; 8 if(nums.size 阅读全文
posted @ 2020-04-16 10:14 Jinxiaobo0509 阅读(103) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 // 前i个字符串构成 == 前i-1个字符串构成(只有一个字符1-9) 5 // +前i-2个字符串构成(只有两个字符10-26) 6 int numDecodings(string s) 7 { 8 int n = s.size( 阅读全文
posted @ 2020-04-16 09:58 Jinxiaobo0509 阅读(180) 评论(0) 推荐(0)
摘要:1 // 统计个数,相同位置相同数字的个数 >A的个数 2 // 不同位置相同数字的个数 >B的个数 3 class Solution 4 { 5 public: 6 string getHint(string secret, string guess) 7 { 8 int n = secret.s 阅读全文
posted @ 2020-04-15 16:38 Jinxiaobo0509 阅读(118) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool canWinNim(int n) 5 { 6 //如果不是4的倍数,必赢 7 //如果是4的倍数,必输 8 return n % 4 != 0; 9 } 10 }; 阅读全文
posted @ 2020-04-15 15:29 Jinxiaobo0509 阅读(87) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 vector<string> res; 4 void spilt(string s,char c,vector<string> &res) 5 { 6 istringstream iss(s); 7 string temp; 8 while(getlin 阅读全文
posted @ 2020-04-15 15:09 Jinxiaobo0509 阅读(115) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 int dx[8] = {1,1,0,-1,-1,-1,0,1}; 4 int dy[8] = {0,1,1,1,0,-1,-1,-1}; 5 public: 6 void gameOfLife(vector<vector<int>>& board) 7 阅读全文
posted @ 2020-04-15 14:40 Jinxiaobo0509 阅读(119) 评论(0) 推荐(0)
摘要:1 /* 2 * Below is the interface for Iterator, which is already defined for you. 3 * **DO NOT** modify the interface for Iterator. 4 * 5 * class Iterat 阅读全文
posted @ 2020-04-15 13:57 Jinxiaobo0509 阅读(96) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 void moveZeroes(vector<int>& nums) 5 { 6 int n = nums.size(); 7 int begin = 0; 8 for(int i = 0;i < n;i ++) 9 { 10 if( 阅读全文
posted @ 2020-04-15 12:44 Jinxiaobo0509 阅读(83) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int numSquares(int n) 5 { 6 queue<pair<int,int>> Q; 7 Q.push(make_pair(n,0)); 8 vector<bool> visit(n+1,false); 9 //层序 阅读全文
posted @ 2020-04-15 11:41 Jinxiaobo0509 阅读(138) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int hIndex(vector<int>& nums) 5 { 6 sort(nums.begin(),nums.end()); 7 int l = 0,r = nums.size(); 8 while(l < r) 9 { 10 阅读全文
posted @ 2020-04-15 10:35 Jinxiaobo0509 阅读(94) 评论(0) 推荐(0)
摘要:1 //[3,0,1] + [0,1,2,3] = [0,0,1,1,2,3,3] ——> 求异或 2 class Solution 3 { 4 public: 5 int missingNumber(vector<int>& nums) 6 { 7 int n = nums.size(); 8 i 阅读全文
posted @ 2020-04-15 10:26 Jinxiaobo0509 阅读(137) 评论(0) 推荐(0)
摘要:1 //动态方程dp[i]=min(dp[p_2]*2,dp[p_3]*3,dp[p_5]*5) 2 class Solution 3 { 4 public: 5 int nthUglyNumber(int n) 6 { 7 vector<int> dp(n); 8 dp[0] = 1; 9 int 阅读全文
posted @ 2020-04-14 19:30 Jinxiaobo0509 阅读(86) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isUgly(int num) 5 { 6 if (num == 0) return false; 7 while (num % 2 == 0) num /= 2; 8 while (num % 3 == 0) num /= 阅读全文
posted @ 2020-04-14 19:27 Jinxiaobo0509 阅读(94) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> singleNumber(vector<int>& nums) 5 { 6 int diff = 0; 7 vector<int> res(2,0); 8 for (int num : nums) diff ^ 阅读全文
posted @ 2020-04-12 23:10 Jinxiaobo0509 阅读(104) 评论(0) 推荐(0)
摘要:1 //很简单 2 class Solution 3 { 4 public: 5 int addDigits(int num) 6 { 7 while(num > 9) 8 { 9 int temp = 0; 10 while(num) 11 { 12 temp += num % 10; 13 nu 阅读全文
posted @ 2020-04-12 22:38 Jinxiaobo0509 阅读(117) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-12 22:31 Jinxiaobo0509 阅读(149) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isAnagram(string s, string t) 5 { 6 unordered_map<char,int> hash_s,hash_t; 7 for(auto a : s) hash_s[a] ++; 8 for 阅读全文
posted @ 2020-04-12 20:42 Jinxiaobo0509 阅读(125) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> diffWaysToCompute(string input) 5 { 6 vector<int> res; 7 for(int i=0; i<input.size(); i++) 8 { 9 if(input 阅读全文
posted @ 2020-04-12 20:38 Jinxiaobo0509 阅读(143) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool searchMatrix(vector<vector<int>>& matrix, int target) 5 { 6 if(matrix.empty() || matrix[0].empty()) return false 阅读全文
posted @ 2020-04-12 18:46 Jinxiaobo0509 阅读(120) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> productExceptSelf(vector<int>& nums) 5 { 6 int n = nums.size(); 7 vector<int> A(n,1),B(n,1); 8 vector<int 阅读全文
posted @ 2020-04-12 18:36 Jinxiaobo0509 阅读(149) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-12 18:02 Jinxiaobo0509 阅读(127) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 阅读全文
posted @ 2020-04-12 17:44 Jinxiaobo0509 阅读(105) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isPowerOfTwo(int n) 5 { 6 return n > 0 && (n & n - 1) == 0; 7 } 8 }; 1 class Solution 2 { 3 public: 4 bool isPow 阅读全文
posted @ 2020-04-12 16:04 Jinxiaobo0509 阅读(120) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int kthSmallest(TreeNode* root, int k) 5 { 6 stack<TreeNode*> s; 7 TreeNode *cur = root; 8 while(!s.empty() || cur) 9 阅读全文
posted @ 2020-04-12 13:58 Jinxiaobo0509 阅读(183) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> majorityElement(vector<int>& nums) 5 { 6 vector<int> res; 7 if (nums.empty()) return res; 8 // 初始化两个候选人ca 阅读全文
posted @ 2020-04-12 13:31 Jinxiaobo0509 阅读(139) 评论(0) 推荐(0)
摘要:1 //简单题 2 class Solution 3 { 4 public: 5 vector<string> summaryRanges(vector<int>& nums) 6 { 7 vector<string> res; 8 if(nums.empty()) return res; 9 ve 阅读全文
posted @ 2020-04-12 11:59 Jinxiaobo0509 阅读(154) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int calculate(string s) 5 { 6 s += "+";//1、最后一位加一个"+" 7 if (s.empty()) return 0; 8 stack<int> vals; 9 stack<char> ops 阅读全文
posted @ 2020-04-12 11:35 Jinxiaobo0509 阅读(131) 评论(0) 推荐(0)
摘要:1 class MyQueue 2 { 3 stack<int> s1,s2; 4 public: 5 MyQueue() {} 6 7 void push(int x) 8 { 9 s1.push(x); 10 } 11 12 int pop() 13 { 14 while(!s1.empty() 阅读全文
posted @ 2020-04-11 23:17 Jinxiaobo0509 阅读(116) 评论(0) 推荐(0)
摘要:1 class MyStack 2 { 3 queue<int> q1;//数据队列 4 queue<int> q2;//临时队列 5 public: 6 MyStack() {} 7 8 void push(int x) 9 { 10 q1.push(x); 11 } 12 13 int pop( 阅读全文
posted @ 2020-04-11 22:36 Jinxiaobo0509 阅读(93) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-11 19:49 Jinxiaobo0509 阅读(105) 评论(0) 推荐(0)
摘要:1 //从四个方向比较,只要任意一个方向不满足,就不重叠 2 class Solution 3 { 4 public: 5 int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) 6 { 7 long long 阅读全文
posted @ 2020-04-11 18:17 Jinxiaobo0509 阅读(312) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-11 17:32 Jinxiaobo0509 阅读(150) 评论(0) 推荐(0)
摘要:1 //dp(i, j)=min(dp(i−1, j), dp(i−1, j−1), dp(i, j−1))+1 2 class Solution 3 { 4 public: 5 int maximalSquare(vector<vector<char>>& matrix) 6 { 7 if(mat 阅读全文
posted @ 2020-04-11 16:29 Jinxiaobo0509 阅读(134) 评论(0) 推荐(0)
摘要:1 //用multimap就可以解决 2 class Solution 3 { 4 public: 5 bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) 6 { 7 if(nums.empty()) return 阅读全文
posted @ 2020-04-11 14:09 Jinxiaobo0509 阅读(138) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool containsNearbyDuplicate(vector<int>& nums, int k) 5 { 6 unordered_map<int,int> hash; 7 for(int i = 0;i < nums.si 阅读全文
posted @ 2020-04-10 16:26 Jinxiaobo0509 阅读(118) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool containsDuplicate(vector<int>& nums) 5 { 6 unordered_map<int,int> hash; 7 for(auto a : nums) 8 { 9 if(++ hash[a] 阅读全文
posted @ 2020-04-10 16:17 Jinxiaobo0509 阅读(64) 评论(0) 推荐(0)
摘要:1 // [1,3],[4,7],[9,9] 2 // L[1] = 3,L[4] = 7,L[9] = 9; 3 // R[3] = 1,R[7] = 4,R[9] = 9; 4 class SummaryRanges 5 { 6 map<int,int> L,R; 7 unordered_set 阅读全文
posted @ 2020-04-10 15:15 Jinxiaobo0509 阅读(169) 评论(0) 推荐(0)
摘要:1 class MedianFinder 2 { 3 priority_queue<int,vector<int>,greater<int>> up;//小根堆 4 priority_queue<int> down;//大根堆 5 public: 6 MedianFinder() {} 7 8 vo 阅读全文
posted @ 2020-04-10 11:08 Jinxiaobo0509 阅读(109) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 struct cmp 4 { 5 bool operator()(pair<string,int> const &left,pair<string,int> const &right) 6 { 7 if(left.second == right.seco 阅读全文
posted @ 2020-04-09 22:21 Jinxiaobo0509 阅读(153) 评论(0) 推荐(0)
摘要:1 struct cmp 2 { 3 bool operator()(pair<string,int> const &left,pair<string,int> const &right) 4 { 5 if(left.second == right.second) return left.first 阅读全文
posted @ 2020-04-09 22:04 Jinxiaobo0509 阅读(140) 评论(0) 推荐(0)
摘要:1 class UnionFind 2 { 3 public: 4 int count; // 记录连通分量个数 5 vector<int> parent; // 存储若干棵树 6 vector<int> size; // 记录每棵树包含的节点数 7 8 UnionFind(int n) 9 { 1 阅读全文
posted @ 2020-04-09 21:27 Jinxiaobo0509 阅读(157) 评论(0) 推荐(0)
摘要:1 class UnionFind 2 { 3 public: 4 vector<int> parent; // 存储若干棵树 5 int count; 6 UnionFind(int n) 7 { 8 count = n; 9 for (int i = 0; i < n; i++) parent. 阅读全文
posted @ 2020-04-09 21:25 Jinxiaobo0509 阅读(178) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 vector<int> p; 4 5 int find(int x) 6 { 7 if(p[x] != x) p[x] = find(p[x]); 8 return p[x]; 9 } 10 public: 11 int findCircleNum(ve 阅读全文
posted @ 2020-04-09 20:46 Jinxiaobo0509 阅读(103) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int subarraySum(vector<int>& nums, int k) 5 { 6 unordered_map<int,int> hash; 7 hash[0] = 1; 8 9 int res = 0; 10 for(i 阅读全文
posted @ 2020-04-09 19:15 Jinxiaobo0509 阅读(118) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int subarraysDivByK(vector<int>& nums, int k) 5 { 6 unordered_map<int,int> hash; 7 hash[0] = 1; 8 9 int res = 0; 10 f 阅读全文
posted @ 2020-04-09 19:14 Jinxiaobo0509 阅读(229) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-09 18:18 Jinxiaobo0509 阅读(120) 评论(0) 推荐(0)
摘要:1 class MyHashMap 2 { 3 public: 4 const static int N = 20011; 5 vector<list<pair<int,int>>> hash; 6 7 MyHashMap() 8 { 9 hash = vector<list<pair<int,in 阅读全文
posted @ 2020-04-09 17:36 Jinxiaobo0509 阅读(297) 评论(0) 推荐(0)
摘要:1 //开放寻址法 2 class MyHashMap 3 { 4 public: 5 const static int N = 2000003; 6 int hash_key[N], hash_value[N]; 7 8 MyHashMap() 9 { 10 memset(hash_key, -1 阅读全文
posted @ 2020-04-09 17:32 Jinxiaobo0509 阅读(730) 评论(0) 推荐(0)
摘要:1 //开放寻址法 2 class MyHashMap 3 { 4 public: 5 const static int N = 2000003; 6 int hash_key[N], hash_value[N]; 7 8 MyHashMap() 9 { 10 memset(hash_key, -1 阅读全文
posted @ 2020-04-09 17:15 Jinxiaobo0509 阅读(121) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int rob(vector<int>& nums) 5 { 6 int n = nums.size(); 7 if(n == 0) return 0; 8 else if(n == 1) return nums[0]; 9 else 阅读全文
posted @ 2020-04-09 13:48 Jinxiaobo0509 阅读(152) 评论(0) 推荐(0)
摘要:1 //数组是连续的 2 class Solution 3 { 4 public: 5 int maxSubarraySumCircular(vector<int>& A) 6 { 7 // 单调队列 8 int n = A.size(); 9 deque<int> q; 10 for(int i 阅读全文
posted @ 2020-04-08 22:50 Jinxiaobo0509 阅读(171) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> maxSlidingWindow(vector<int>& nums, int k) 5 { 6 vector<int> res; 7 deque<int> q; 8 for(int i = 0;i < num 阅读全文
posted @ 2020-04-08 16:29 Jinxiaobo0509 阅读(126) 评论(0) 推荐(0)
摘要:1 //自底向上层层叠加 2 class Solution 3 { 4 public: 5 int trap(vector<int>& height) 6 { 7 int res = 0; 8 stack<int> stk; 9 10 for(int i = 0;i < height.size(); 阅读全文
posted @ 2020-04-08 16:04 Jinxiaobo0509 阅读(111) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int largestRectangleArea(vector<int>& heights) 5 { 6 int n = heights.size(); 7 vector<int> left(n),right(n); 8 9 //用单 阅读全文
posted @ 2020-04-08 15:27 Jinxiaobo0509 阅读(114) 评论(0) 推荐(0)
摘要:1 //括号序列合法 <==> 所有前缀和>=0,且总和等于0 2 3 // start当前枚举的这一段的开头 4 // cnt前缀和 5 // (=1 6 // )=-1 7 // 1、cnt<0 => start = i + 1,cnt = 0 8 // 2、cnt>0 => 继续做 9 // 阅读全文
posted @ 2020-04-07 22:43 Jinxiaobo0509 阅读(192) 评论(0) 推荐(0)
摘要:括号序列合法 <==> 所有前缀和>=0,且总和等于0 阅读全文
posted @ 2020-04-07 22:41 Jinxiaobo0509 阅读(177) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 string minWindow(string s, string t) 5 { 6 unordered_map<char,int> hash; 7 for(auto c : t) hash[c]++; 8 int cnt = has 阅读全文
posted @ 2020-04-04 23:01 Jinxiaobo0509 阅读(185) 评论(0) 推荐(0)
摘要:1 class WordDictionary 2 { 3 bool is_end; //是否以该单词结尾 4 WordDictionary* son[26]; //该节点儿子的个数 5 6 public: 7 WordDictionary() 8 { 9 is_end = false; 10 for 阅读全文
posted @ 2020-04-04 20:36 Jinxiaobo0509 阅读(395) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 vector<int> res; 4 public: 5 vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) 6 { 7 vector<vector<int> 阅读全文
posted @ 2020-04-04 19:19 Jinxiaobo0509 阅读(150) 评论(1) 推荐(0)
摘要:1 //跟LeetCode3类似 2 class Solution 3 { 4 public: 5 int minSubArrayLen(int s, vector<int>& nums) 6 { 7 //如果为空,直接返回0 8 if(nums.empty()) return 0; 9 int r 阅读全文
posted @ 2020-04-04 18:46 Jinxiaobo0509 阅读(173) 评论(0) 推荐(0)
摘要:1 // 将先修关系构成一张图,由每个数对的第二个数字向第一个数字连边。 2 // 首先将所有入度为0的点进队,准备拓扑排序。 3 // 宽搜过程中,将当前结点所关联的结点的入度减1;若发现新的入度为0的结点,则将其进队。 4 // 最后如果遍历了所有结点,则说明可以满足要求;否则,先修关系存在环。 阅读全文
posted @ 2020-04-04 17:52 Jinxiaobo0509 阅读(2071) 评论(0) 推荐(0)
摘要:1 // 将先修关系构成一张图,由每个数对的第二个数字向第一个数字连边。 2 // 首先将所有入度为0的点进队,准备拓扑排序。 3 // 宽搜过程中,将当前结点所关联的结点的入度减1;若发现新的入度为0的结点,则将其进队。 4 // 最后如果遍历了所有结点,则说明可以满足要求;否则,先修关系存在环。 阅读全文
posted @ 2020-04-04 17:46 Jinxiaobo0509 阅读(142) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isIsomorphic(string s, string t) 5 { 6 unordered_map<char,char> hash; 7 if(s.size() != t.size()) return false;// 阅读全文
posted @ 2020-04-04 16:06 Jinxiaobo0509 阅读(146) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int countPrimes(int n) 5 { 6 vector<bool> isPrime(n+1,true); //首先全部为true 7 int count = 0; 8 9 for(int i = 2; i < n;++ 阅读全文
posted @ 2020-04-04 15:48 Jinxiaobo0509 阅读(112) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 阅读全文
posted @ 2020-04-04 15:28 Jinxiaobo0509 阅读(130) 评论(0) 推荐(0)
摘要:1 //不是快乐数的数称为不快乐数(unhappy number),所有不快乐数的数位平方和计算, 2 //最後都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中。 3 class Solution 4 { 5 public: 6 bool isH 阅读全文
posted @ 2020-04-04 15:18 Jinxiaobo0509 阅读(113) 评论(0) 推荐(0)
摘要:1 //n & n-1 总是去掉二进制最低位的1 从最高位&起来会快很多 2 class Solution 3 { 4 public: 5 int rangeBitwiseAnd(int m, int n) 6 { 7 //m <= n && m <= n - 1 ——> m <= n - 1 8 阅读全文
posted @ 2020-04-04 15:10 Jinxiaobo0509 阅读(121) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int numIslands(vector<vector<char>>& grid) 5 { 6 if(grid.size() == 0 || grid[0].size() == 0) return 0; 7 int m = grid 阅读全文
posted @ 2020-04-04 14:51 Jinxiaobo0509 阅读(119) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int hammingWeight(uint32_t n) 5 { 6 int res = 0; 7 for(int i = 0;i < 32;i ++) res += (n >> i) & 1; 8 return res; 9 } 阅读全文
posted @ 2020-04-03 23:06 Jinxiaobo0509 阅读(89) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 uint32_t reverseBits(uint32_t n) 5 { 6 uint32_t res = 0; 7 for(int i = 0;i < 32;i ++) //得到从最低位到最高位的每一位数 8 { 9 res += 阅读全文
posted @ 2020-04-03 23:04 Jinxiaobo0509 阅读(88) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 void rotate(vector<int>& nums, int k) 5 { 6 int n = nums.size(); 7 k = k % n; 8 reverse(nums.begin(),nums.begin() + n 阅读全文
posted @ 2020-04-03 22:44 Jinxiaobo0509 阅读(88) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<string> findRepeatedDnaSequences(string s) 5 { 6 int n = s.size(); 7 vector<string> res; 8 unordered_map<strin 阅读全文
posted @ 2020-04-03 22:33 Jinxiaobo0509 阅读(157) 评论(0) 推荐(0)
摘要:1 bool compare(int a,int b)//int比较 2 { 3 string str1=to_string(a); 4 string str2=to_string(b); 5 return (str1+str2) > (str2+str1); 6 } 7 8 class Solut 阅读全文
posted @ 2020-04-03 22:22 Jinxiaobo0509 阅读(134) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool makesquare(vector<int>& nums) 5 { 6 if(nums.size()<4) return false; 7 int sum=0; 8 for(int i=0;i<nums.size();i++ 阅读全文
posted @ 2020-04-03 21:13 Jinxiaobo0509 阅读(174) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int trailingZeroes(int n) 5 { 6 int res = 0; 7 while(n >= 5) 8 { 9 res += n / 5; 10 n /= 5; 11 } 12 return res; 13 } 阅读全文
posted @ 2020-04-03 17:17 Jinxiaobo0509 阅读(112) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int titleToNumber(string s) 5 { 6 unordered_map<char,int> hash; 7 for(int i = 1;i <= 26;i ++) hash['A' + i - 1] = i; 阅读全文
posted @ 2020-04-03 16:44 Jinxiaobo0509 阅读(123) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int majorityElement(vector<int>& nums) 5 { 6 unordered_map<int,int> hash; 7 for(auto a : nums) hash[a]++; 8 for(auto 阅读全文
posted @ 2020-04-03 16:34 Jinxiaobo0509 阅读(105) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 string convertToTitle(int n) 5 { 6 unordered_map<int,char> hash; 7 for(int i = 1;i <= 26;i ++) hash[i] = 'A' + i - 1; 阅读全文
posted @ 2020-04-03 16:31 Jinxiaobo0509 阅读(150) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> twoSum(vector<int>& numbers, int target) 5 { 6 int l = 0,r = numbers.size() - 1; 7 while(l < r) 8 { 9 if( 阅读全文
posted @ 2020-04-03 16:06 Jinxiaobo0509 阅读(121) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 //小数部分如果余数重复出现两次就表示该小数是循环小数了 5 string fractionToDecimal(int numerator, int denominator) 6 { 7 if(denominator==0) retu 阅读全文
posted @ 2020-04-03 16:02 Jinxiaobo0509 阅读(139) 评论(0) 推荐(0)
摘要:1 class MinStack 2 { 3 stack<int> data; 4 stack<int> min_data; 5 public: 6 /** initialize your data structure here. */ 7 MinStack() {} 8 9 void push(i 阅读全文
posted @ 2020-04-03 14:34 Jinxiaobo0509 阅读(97) 评论(0) 推荐(0)
摘要:1 //利用模板 2 3 //以最后一个值来划分 4 //如果比它小,在右边,更新r 5 //如果比它大,在左边,更新l 6 7 //由于有重复值的情况, 需要加入 mid 元素等于 hi 元素的情况 8 //此时应该将 hi 减 1 防止重复数字是最小元素 9 class Solution 10 阅读全文
posted @ 2020-04-03 14:25 Jinxiaobo0509 阅读(151) 评论(0) 推荐(0)
摘要:1 //1、如果nums[i]为正 2 // min_val[i] = min(min_val[i - 1] * nums[i],nums[i]); 3 // max_val[i] = max(max_val[i - 1] * nums[i],nums[i]); 4 5 //2、如果nums[i]为 阅读全文
posted @ 2020-04-03 12:18 Jinxiaobo0509 阅读(123) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int evalRPN(vector<string>& tokens) 5 { 6 stack<int> s; 7 int n = tokens.size(); 8 for(int i = 0;i < n;i ++) 9 { 10 i 阅读全文
posted @ 2020-04-03 11:45 Jinxiaobo0509 阅读(96) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 阅读全文
posted @ 2020-04-03 11:29 Jinxiaobo0509 阅读(134) 评论(0) 推荐(0)
摘要:1 //双向链表(同时存储key和val)+hash表 2 //很容易犯错的一点是:处理链表节点的同时不要忘了更新哈希表中对节点的映射。 3 class LRUCache 4 { 5 int n;//大小 6 list<pair<int,int>> li;//链表 7 unordered_map<i 阅读全文
posted @ 2020-04-02 22:27 Jinxiaobo0509 阅读(149) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-02 19:03 Jinxiaobo0509 阅读(152) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-02 19:00 Jinxiaobo0509 阅读(159) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 阅读全文
posted @ 2020-04-02 18:54 Jinxiaobo0509 阅读(155) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 阅读全文
posted @ 2020-04-02 18:03 Jinxiaobo0509 阅读(94) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool wordBreak(string s, vector<string>& wordDict) 5 { 6 vector<bool> dp(s.size()+1, false); 7 unordered_set<string> 阅读全文
posted @ 2020-04-02 17:54 Jinxiaobo0509 阅读(156) 评论(0) 推荐(0)
摘要:1 //用bits数组统计nums中所有数转化成二进制后,每个位上1的个数 2 //将bits数组中能够整除3的值置0,不能整除3的值置1 3 //将bits转换成真的二进制数 4 class Solution 5 { 6 public: 7 int singleNumber(vector<int> 阅读全文
posted @ 2020-04-02 17:26 Jinxiaobo0509 阅读(164) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int singleNumber(vector<int>& nums) 5 { 6 int res = 0; 7 for(auto a : nums) res ^= a; 8 return res; 9 } 10 }; 阅读全文
posted @ 2020-04-02 17:06 Jinxiaobo0509 阅读(90) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 阅读全文
posted @ 2020-04-02 16:10 Jinxiaobo0509 阅读(178) 评论(0) 推荐(0)
摘要:1 /** 2 * // This is the interface that allows for creating nested lists. 3 * // You should not implement it, or speculate about its implementation 4 阅读全文
posted @ 2020-04-02 15:07 Jinxiaobo0509 阅读(139) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int canCompleteCircuit(vector<int>& gas, vector<int>& cost) 5 { 6 int n = gas.size(); 7 for(int i = 0;i < n;i ++) 8 { 阅读全文
posted @ 2020-04-02 13:42 Jinxiaobo0509 阅读(109) 评论(0) 推荐(0)
摘要:1 /* 2 // Definition for a Node. 3 class Node { 4 public: 5 int val; 6 Node* next; 7 Node* random; 8 9 Node(int _val) { 10 val = _val; 11 next = NULL; 阅读全文
posted @ 2020-04-02 11:53 Jinxiaobo0509 阅读(139) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<vector<string>>result; 5 vector<string>temp; 6 7 bool isPalindrome(string s) //是否为回文串 8 { 9 int i=0,j=s.size() 阅读全文
posted @ 2020-04-02 11:18 Jinxiaobo0509 阅读(129) 评论(0) 推荐(0)
摘要:1 //1、从四条边出发,找到与"O"相连的,并把当前"O"置为"#" 2 //2、把"O"改为"X",把"#"改为"O" 3 class Solution 4 { 5 public: 6 void solve(vector<vector<char>>& board) 7 { 8 if (board 阅读全文
posted @ 2020-04-02 10:59 Jinxiaobo0509 阅读(161) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-02 10:13 Jinxiaobo0509 阅读(125) 评论(0) 推荐(0)
摘要:1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), 阅读全文
posted @ 2020-04-01 23:01 Jinxiaobo0509 阅读(118) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int ladderLength(string beginWord, string endWord, vector<string>& wordList) 5 { 6 //hash用于去重 7 unordered_set<string> 阅读全文
posted @ 2020-04-01 22:24 Jinxiaobo0509 阅读(115) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool isPalindrome(string s) 5 { 6 string str; 7 for(int i = 0;i < s.size();i ++) 8 { 9 if(isdigit(s[i])) str.push_bac 阅读全文
posted @ 2020-04-01 19:48 Jinxiaobo0509 阅读(100) 评论(0) 推荐(0)
摘要:1 /* 2 [7, 1, 5, 6] 第二天买入,第四天卖出,收益最大(6-1),所以一般人可能会想,怎么判断不是第三天就卖出了呢? 3 这里就把问题复杂化了,根据题目的意思,当天卖出以后,当天还可以买入,所以其实可以第三天卖出,第三天买入,第四天又卖出((5-1)+ (6-5) 6 - 1)。 阅读全文
posted @ 2020-04-01 18:55 Jinxiaobo0509 阅读(139) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int maxProfit(vector<int>& prices) 5 { 6 if(prices.empty()) return 0; 7 int n = prices.size(); 8 int buy = prices[0]; 阅读全文
posted @ 2020-04-01 18:31 Jinxiaobo0509 阅读(126) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> getRow(int k) 5 { 6 int n = k + 1; 7 vector<vector<int>> ans; 8 for(int i = 0;i < n;i ++) 9 { 10 ans.push 阅读全文
posted @ 2020-04-01 17:37 Jinxiaobo0509 阅读(92) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<vector<int>> generate(int n) 5 { 6 vector<vector<int>> ans; 7 for(int i = 0;i < n;i ++) 8 { 9 ans.push_back(ve 阅读全文
posted @ 2020-04-01 17:24 Jinxiaobo0509 阅读(111) 评论(0) 推荐(0)
摘要:1 /* 2 // Definition for a Node. 3 class Node { 4 public: 5 int val; 6 Node* left; 7 Node* right; 8 Node* next; 9 10 Node() : val(0), left(NULL), righ 阅读全文
posted @ 2020-04-01 17:15 Jinxiaobo0509 阅读(80) 评论(0) 推荐(0)