- 博客(637)
- 资源 (3)
- 收藏
- 关注
原创 阿里云服务器CPU 100%问题
希望本文的内容对你有所帮助如果可以,麻烦关注一下本站 https://wscoder.com/您的支持,是我的最大动力。
2024-02-14 15:14:28 1542
原创 leetcode 179. 最大数
179. 最大数class Solution {public: static bool cmp(string A, string B) { int min_len = min(A.size(), B.size()); if(A.size() == B.size() || A.substr(0, min_len) != B.substr(0, min_len)) return A > B; if(A.size() > B.size())
2021-04-16 16:59:01 360
原创 leetcode 264. 丑数 II
264. 丑数 IIclass Solution {public: int nthUglyNumber(int n) { vector<int> ugly(n, 0); int ind2 = 0, ind3 = 0, ind5 = 0; ugly[0] = 1; for(int ind = 1; ind < n; ind++) { ugly[ind] = min(min(ugly[ind2
2021-04-11 16:30:02 270
原创 leetcode 263. 丑数
263. 丑数class Solution {public: bool isUgly(int n) { if(n <= 0) return false; while(n % 2 == 0 || n % 3 == 0 || n % 5 == 0) { if(n % 2 == 0) n /= 2; if(n % 3 == 0) n /= 3; if(n % 5 == 0) n /= 5;
2021-04-11 16:13:20 286
原创 leetcode 32. 最长有效括号
32. 最长有效括号class Solution {public: int max(int a, int b) { return a > b ? a : b; } int longestValidParentheses(string s) { vector<int> dp(s.size(), 0); int max_len = 0; bool flag = true; while(flag) {
2021-04-09 20:34:35 163
原创 leetcode 22. 括号生成
22. 括号生成class Solution {public: vector<string> ans; void dfs(string str, int left, int right) { if(left == 0 && right == 0) { ans.push_back(str); return ; } if(left < 0 || right &l
2021-04-09 19:12:51 87
原创 leetcode 154. 寻找旋转排序数组中的最小值 II
154. 寻找旋转排序数组中的最小值 IIclass Solution {public: int findMin(vector<int>& nums, int left, int right) { if(left > right) return INT_MAX; if(left == right || nums[left] < nums[right]) return nums[left]; int mid = (l
2021-04-09 14:21:14 81
原创 leetcode 81. 搜索旋转排序数组 II
81. 搜索旋转排序数组 IIclass Solution {public: bool search(vector<int>& nums, int target, int left, int right) { if(left > right) return false; int mid = (left + right) >> 1; if(nums[mid] == target) return true;
2021-04-09 13:26:49 88
原创 leetcode 80. 删除有序数组中的重复项 II
80. 删除有序数组中的重复项 IIclass Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size() <= 1) return nums.size(); int ii = 0, jj = 1, cnt = 1; while(jj < nums.size()) { if(nums[jj] != nu
2021-04-06 10:42:45 155
原创 leetcode 88. 合并两个有序数组
88. 合并两个有序数组class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int ii = m - 1, jj = n - 1, kk = (m+n) - 1; while(ii >= 0 && jj >= 0) { if(nums1[ii]
2021-04-05 10:41:15 112
原创 leetcode 406. 根据身高重建队列
406. 根据身高重建队列class Solution {public: static bool cmp(const vector<int>& A, const vector<int>& B) { if(A[1] != B[1]) return A[1] < B[1]; return A[0] < B[0]; } vector<vector<int>> reconstru
2021-04-04 17:33:26 135
原创 leetcode 79. 单词搜索
79. 单词搜索class Solution {public: int Y, X; bool exist(vector<vector<char>>& board, string& word, int ind, int yy, int xx) { if(ind == word.size()) return true; if(yy < 0 || yy >= Y || xx < 0 || xx >
2021-04-04 16:20:49 111
原创 leetcode 49. 字母异位词分组
49. 字母异位词分组class Solution {public: vector<vector<string>> groupAnagrams(vector<string>& strs) { vector<vector<string> > ans; map<string, vector<string> > M; for(auto str : strs) {
2021-04-04 16:04:07 118
原创 leetcode 17. 电话号码的字母组合
17. 电话号码的字母组合class Solution {public: string V[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; vector<string> ans; void letterCombinations(const string& digits, int ind, string temp) { if(ind == dig
2021-04-04 15:47:58 150
原创 leetcode 781. 森林中的兔子
781. 森林中的兔子前几天HW笔试原题……class Solution {public: int numRabbits(vector<int>& answers) { vector<bool> visited(answers.size(), false); int ans = 0; for(int ii = 0; ii < answers.size(); ii++) { if(vi
2021-04-04 14:57:25 132
原创 leetcode 438. 找到字符串中所有字母异位词
438. 找到字符串中所有字母异位词class Solution {public: vector<int> findAnagrams(string s, string p) { vector<int> SV(26, 0); vector<int> PV(26, 0); vector<int> ans; for(auto ch : p) PV[ch - 'a']++;
2021-04-03 19:48:11 107
原创 leetcode 581. 最短无序连续子数组
581. 最短无序连续子数组class Solution {public: int findUnsortedSubarray(vector<int>& nums) { int mid_min = INT_MAX, mid_max = INT_MIN; int left = 0, right = nums.size()-1; int ii = 1; while(ii < nums.size() &
2021-04-03 15:12:36 104
原创 leetcode 85. 最大矩形
85. 最大矩形class Solution {public: int maximalRectangle(vector<vector<char>>& matrix) { int Y = matrix.size(); if(Y == 0) return 0; int X = matrix[0].size(); int ans = 0; vector<vector<int&g
2021-04-03 14:45:42 123
原创 leetcode 221. 最大正方形
221. 最大正方形class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { int Y = matrix.size(); int X = matrix[0].size(); int ans = 0; vector<vector<int> > dp(Y+1, vector<int&
2021-04-03 12:00:05 84
原创 leetcode 1143. 最长公共子序列
1143. 最长公共子序列class Solution {public: int max(int a, int b) { return a > b ? a : b; } int longestCommonSubsequence(string text1, string text2) { vector<vector<int> > dp(text1.size()+1, vector<int>(text2.size()+1, 0));
2021-04-03 11:15:43 83
原创 leetcode 面试题 17.21. 直方图的水量
面试题 17.21. 直方图的水量class Solution {public: int trap(vector<int>& height) { stack<int> S; int ans = 0; int last_height = 0; for(int ii = 0; ii < height.size(); ii++) { while(!S.empty() &
2021-04-02 21:08:25 147
原创 leetcode 437. 路径总和 III
437. 路径总和 III/** * 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(
2021-03-31 14:00:42 59
原创 leetcode 56. 合并区间
56. 合并区间class Solution {public: vector<vector<int>> merge(vector<vector<int>>& intervals) { vector<vector<int> > ans; sort(intervals.begin(), intervals.end()); for(int ii = 0, jj; ii &l
2021-03-31 13:27:46 70
原创 leetcode 75. 颜色分类
75. 颜色分类class Solution {public: void sortColors(vector<int>& nums) { int ind0 = 0, ind1 = 0; for(int ii = 0; ii < nums.size(); ii++) { int num = nums[ii]; nums[ii] = 2; if(num < 2)
2021-03-31 13:08:51 61
原创 leetcode 647. 回文子串
647. 回文子串class Solution {public: int ans = 0; void countSubstrings(string& s, int left, int right) { if(left < 0 || right >= s.size() || left > right) return ; if(s[left] == s[right]) { ans++;
2021-03-31 11:11:03 58
原创 leetcode 152. 乘积最大子数组
152. 乘积最大子数组class Solution {public: int calProduct(vector<int>& nums, int left, int right) { if(left >= right) return INT_MIN; int product = 1; while(left < right) product *= nums[left++]; return produ
2021-03-31 10:55:00 68
原创 leetcode 90. 子集 II
90. 子集 IIclass Solution {public: const int SIZE = 21; vector<vector<int> > ans; vector<int> V; void dfs(vector<int>& cnt, int ind) { if(ind >= cnt.size()) { ans.push_back(V);
2021-03-31 10:06:16 58
原创 leetcode 114. 二叉树展开为链表
114. 二叉树展开为链表/** * 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(
2021-03-30 13:50:29 58
原创 leetcode 24. 两两交换链表中的节点
24. 两两交换链表中的节点/** * 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) : va
2021-03-30 13:16:27 61
原创 leetcode 31. 下一个排列
31. 下一个排列class Solution {public: void nextPermutation(vector<int>& nums) { int ii = nums.size() - 2, jj = nums.size() - 1; while(ii >= 0 && nums[ii] >= nums[ii+1]) ii--; if(ii != -1) { whil
2021-03-30 13:05:16 59
原创 leetcode 78. 子集
78. 子集class Solution {public: vector<vector<int> > ans; vector<int> V; void dfs(vector<int>& nums, int ind) { if(ind >= nums.size()) { ans.push_back(V); return ; }
2021-03-30 12:01:22 58
原创 leetcode 84. 柱状图中最大的矩形
84. 柱状图中最大的矩形class Solution {public: int largestRectangleArea(vector<int>& heights) { stack<int> S; heights.push_back(0); S.push(-1); int max_area = 0; for(int ii = 0; ii < heights.size(); i
2021-03-30 11:44:09 76
原创 leetcode 287. 寻找重复数
287. 寻找重复数class Solution {public: int findDuplicate(vector<int>& nums) { int ans = 0; for(uint32_t bit = 1; bit; bit <<= 1) { int x = 0, y = 0; for(int ii = 0; ii < nums.size(); ii++) {
2021-03-30 10:34:28 71
原创 leetcode 74. 搜索二维矩阵
74. 搜索二维矩阵class Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int Y = matrix.size(); int X = matrix[0].size(); int yy = 0, xx = X-1; while(yy < Y && matrix[y
2021-03-30 09:57:31 55
原创 leetcode 86. 分隔链表
86. 分隔链表/** * 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),
2021-03-29 20:37:41 60
原创 leetcode 238. 除自身以外数组的乘积
238. 除自身以外数组的乘积class Solution {public: vector<int> productExceptSelf(vector<int>& nums) { vector<int> ans(nums.size(), 1); for(int ii = nums.size() - 2; ii >= 0; ii--) { ans[ii] = ans[ii+1] * nums
2021-03-29 17:22:10 94
原创 leetcode 538. 把二叉搜索树转换为累加树 1038. 把二叉搜索树转换为累加树
538. 把二叉搜索树转换为累加树/** * 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), ri
2021-03-29 16:54:31 64
原创 leetcode 190. 颠倒二进制位
190. 颠倒二进制位class Solution {public: uint32_t reverseBits(uint32_t n) { uint32_t temp = 1 << 31; uint32_t ans = 0; for(uint32_t bit = 1; bit; bit <<= 1, temp >>= 1) { if(n & bit) ans |= temp;
2021-03-29 16:03:57 49
原创 leetcode 560. 和为K的子数组
560. 和为K的子数组同样的暴力解法,C++过不了,java能过,太扯淡了……class Solution {public: int subarraySum(vector<int>& nums, int k) { int ans = 0; int sum = 0; unordered_map<int, int> M; M[0] = 1; for(auto num : nums)
2021-03-28 20:42:59 92
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人