
LeetCode刷题
当如磐石
这个作者很懒,什么都没留下…
展开
-
米哈游笔试9-19
1.顺时针旋转打印字符('A’到’Z’循环打印)#include <iostream>#include <vector>using namespace std;//AC:顺时针旋转打印字符('A'到'Z'循环打印)int main(){ int M, N; cin >> M >> N; vector<vector<char>> vc(M, vector<char>(N, 0)); char cur = '原创 2020-09-19 22:01:29 · 736 阅读 · 0 评论 -
岛屿问题DFS
695. 岛屿的最大面积200. 岛屿数量463. 岛屿的周长原创 2020-09-16 00:15:49 · 204 阅读 · 0 评论 -
LeetCode402. 移掉K位数字(大疆2021/8/16后端B卷)
402. 移掉K位数字class Solution {public: string removeKdigits(string num, int k) { vector<int> s; string res = ""; for(int i = 0; i < num.size(); i++) { int number = num[i] - '0'; while(s.size() != 0原创 2020-08-16 22:40:15 · 218 阅读 · 0 评论 -
LeetCode79. 单词搜索
79. 单词搜索class Solution {public: unsigned int len1, len2; bool exist(vector<vector<char>>& board, string word) { len1 = board.size(); if(len1 == 0) return false; len2 = board[0].size(); vector<vec原创 2020-07-31 16:46:16 · 90 阅读 · 0 评论 -
LeetCode225. 用队列实现栈
225. 用队列实现栈class MyStack {public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ void push(int x) { if(q1.empty() && q2.empty()) { q1.push(x); }原创 2020-07-23 23:05:58 · 281 阅读 · 0 评论 -
LeetCode63. 不同路径 II
63. 不同路径 IIclass Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int len1 = obstacleGrid.size(); if(len1 < 1) return 0; int len2 = obstacleGrid[0].size(); vector&l原创 2020-07-06 23:55:55 · 82 阅读 · 0 评论 -
LeetCode209. 长度最小的子数组
209. 长度最小的子数组/*class Solution {public: //双指针 //时间O(n), 空间O(1) int minSubArrayLen(int s, vector<int>& nums) { if(s <= 0 || nums.size() == 0) return 0; int ans = INT_MAX; int start = 0, end = 0; int原创 2020-06-28 22:57:23 · 121 阅读 · 0 评论 -
LeetCode328. 奇偶链表
328. 奇偶链表class Solution {public: ListNode* oddEvenList(ListNode* head) { if(!head || !head->next || !head->next->next) return head; ListNode *node1 = head, *node2 = head->next; ListNode *curr1 = node1, *curr2 = nod原创 2020-06-25 22:03:31 · 95 阅读 · 0 评论 -
LeetCode16. 最接近的三数之和(排序+双指针)
16. 最接近的三数之和class Solution {public: int threeSumClosest(vector<int>& nums, int target) { int len = nums.size(); if(len < 3) return 0; sort(nums.begin(), nums.end()); int res = nums[0] + nums[1] + nums[2];原创 2020-06-24 13:59:54 · 111 阅读 · 0 评论 -
LeetCode112. 路径总和
112. 路径总和/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool hasPathSum(TreeN原创 2020-06-21 19:03:46 · 154 阅读 · 0 评论 -
LeetCode124. 二叉树中的最大路径和
124. 二叉树中的最大路径和/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //时间O(n)(n为节点个数), 空间复杂度O(n)(n递归栈深度)class S原创 2020-06-21 18:56:31 · 121 阅读 · 0 评论 -
LeetCode235. 二叉搜索树的最近公共祖先
235. 二叉搜索树的最近公共祖先//递归//注意二叉树为二叉搜索树class Solution {public: //时间O(n)(n为二叉树节点个数), 空间O(logn)(二叉树的高度, 即递归栈的深度) TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root) return nullptr; int rootVal = root-&g原创 2020-06-20 21:20:55 · 87 阅读 · 0 评论 -
LeetCode412. Fizz Buzz
412. Fizz Buzzclass Solution {public: //时间:O(n), 空间: O(1) vector<string> fizzBuzz(int n) { if(n <= 0) return {}; vector<string> ans(n); for(int i = 1; i <= n; i++) { if(i % 3 == 0 && i原创 2020-06-20 19:48:03 · 89 阅读 · 0 评论 -
LeetCode204. 计数质数
204. 计数质数class Solution {public: int countPrimes(int n) { vector<bool> isPrim(n, true); for(int i = 2; i * i < n; i++) { //不能 i * i <= n if(isPrim[i]) { // i 的倍数不可能是素数了/*这样可以把 i 的整数倍都标记为 fa原创 2020-06-20 09:49:52 · 88 阅读 · 0 评论 -
LeetCode10. 正则表达式匹配
10. 正则表达式匹配class Solution {public: bool isMatch(string s, string p) { if(p.empty()) { return s.empty(); } //检查第一个元素是否匹配 bool first_match = !s.empty() && (s[0] == p[0] || p[0] == '.'); //如果下原创 2020-06-20 08:35:05 · 106 阅读 · 0 评论 -
LeetCode125. 验证回文串
125. 验证回文串class Solution {public: bool isPalindrome(string s) { int n = s.size(); int left = 0, right = n - 1; while (left < right) { while (left < right && !isalnum(s[left])) { ++left原创 2020-06-19 10:30:04 · 87 阅读 · 0 评论 -
LeetCode172. 阶乘后的零
172. 阶乘后的零class Solution {public: /* 求n! 0的来源 2 * 5 所以一对2和5即可产生一个0,所以0的个数即为min(阶乘中5的个数和2的个数) 又因为是2的倍数的数一定比是5的倍数的数多 所以2的个数一定>=5的个数 所以只需要统计 5 的个数了 例如 5! = 1 * 2 * 3 * 4 * 5 2 2 2 5 一个2和一个5配对出现0 所以5!末尾只有一个零原创 2020-06-18 23:05:30 · 90 阅读 · 0 评论 -
LeetCode371. 两整数之和
371. 两整数之和/*class Solution {public: //低位 = a^b,进位 = a & b 负数出错 int getSum(int a, int b) { if(a == 0) return b; if(b == 0) return a; int lower; int carry; while(1) { lower = a ^ b; //低位原创 2020-06-17 22:59:25 · 121 阅读 · 0 评论 -
LetCode1014. 最佳观光组合
1014. 最佳观光组合class Solution {public: //时间:O(n), 空间:O(1) int maxScoreSightseeingPair(vector<int>& A) { int ans = 0, mx = A[0] + 0; //每一个j对应的ans为当前A[j] - j加上j前面i(索引i小于j)最大的A[i] + i, 所求结果为max(ans)(j = 1, 2,..., len - 1)原创 2020-06-17 22:34:41 · 130 阅读 · 0 评论 -
LeetCode242. 有效的字母异位词
242. 有效的字母异位词/*class Solution {public: //时间: O(nlogn), 空间O(1) bool isAnagram(string s, string t) { if(s.length() != t.length()) return false; sort(s.begin(), s.end()); sort(t.begin(), t.end()); return s == t; }原创 2020-06-17 21:02:40 · 93 阅读 · 0 评论 -
LeetCode442. 数组中重复的数据(原地哈希)
442. 数组中重复的数据原地哈希class Solution {public: //原地哈希:将vector当做哈希表自行原地哈希 vector<int> findDuplicates(vector<int>& nums) { int len = nums.size(); if(len <= 1) return vector<int>(); vector<int> ans;原创 2020-06-16 22:32:23 · 236 阅读 · 0 评论 -
LeetCode41. 缺失的第一个正数
41. 缺失的第一个正数class Solution {public: /*时间:O(n) while 循环不会每一次都把数组里面的所有元素都看一遍。如果有一些元素在这一次的循环中被交换到了它们应该在的位置,那么在后续的遍历中,由于它们已经在正确的位置上了,代码再执行到它们的时候,就会被跳过。 最极端的一种情况是,在第 1 个位置经过这个 while 就把所有的元素都看了一遍,这个所有的元素都被放置在它们应该在的位置,那么 for 循环后面的部分的 while 的循环体都不会被执原创 2020-06-16 20:38:06 · 76 阅读 · 0 评论 -
LeetCode268. 缺失数字
268. 缺失数字/*class Solution {public: int missingNumber(vector<int>& nums) { int len = nums.size(); if(len == 0) return -1; int sum = 0; for(const int& val : nums) { sum += val; }原创 2020-06-16 19:43:06 · 92 阅读 · 0 评论 -
LeetCode2. 两数相加
2. 两数相加/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: //时间复杂度:O(max(m, n)), 空间o(max(m, n))(m, n为链表节点个数) ListNo原创 2020-06-15 21:59:07 · 175 阅读 · 0 评论 -
LeetCode237. 删除链表中的节点
237. 删除链表中的节点/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//*class Solution {public: //以为题目出错了,把node当成头结点了,以为没给要删除的节点的值,于是自己改了接口hhhhhhhhh原创 2020-06-15 21:16:01 · 183 阅读 · 0 评论 -
LeetCode38. 外观数列
38. 外观数列class Solution {public: string countAndSay(int n) { if(n < 1) return ""; string res = "1"; for(int i = 2; i <= n; i++) { res = helper(res); } return res; }private: string h原创 2020-06-15 21:04:00 · 110 阅读 · 0 评论 -
LeetCode208. 实现 Trie (前缀树)
208. 实现 Trie (前缀树)字典树class Trie {private: bool isEnd; Trie* next[26];public: /** Initialize your data structure here. */ Trie() { isEnd = false; //memset(next, nullptr, sizeof(next)); 原型:memset(void *s,int ch,size_t n原创 2020-06-15 11:46:15 · 103 阅读 · 1 评论 -
LeetCode66. 加一
66. 加一class Solution {public: vector<int> plusOne(vector<int>& digits) { if(digits.size() == 0) return vector<int>(); int len = digits.size(); if(count(digits.begin(), digits.end(), 9) == len) {原创 2020-06-15 09:33:40 · 119 阅读 · 0 评论 -
LeetCode22. 括号生成
22. 括号生成class Solution {public: vector<string> generateParenthesis(int n) { vector<string> res; int l = 0, r = 0; //l: 左括号数,r: 右括号数 dfs(res, "", n, l, r); return res; }private: void dfs(vector<原创 2020-06-14 17:27:55 · 117 阅读 · 0 评论 -
LeetCode17. 电话号码的字母组合
LeetCode17. 电话号码的字母组合dfs回溯class Solution {public: //时间o(4^n), 空间O(4^n + n)(n为digits.length()) vector<string> letterCombinations(string digits) { if(digits.length() == 0) return vector<string>(); vector<vector<char&原创 2020-06-14 17:04:51 · 105 阅读 · 0 评论 -
LeetCode350. 两个数组的交集 II
350. 两个数组的交集 IIclass Solution {public: //时间O(n + m), 空间O(n + m) (可以对较小的vector进行哈希映射,进一步优化空间为O(min(n, m))) vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { int len1 = nums1.size(); int le原创 2020-06-14 14:08:38 · 104 阅读 · 0 评论 -
LeetCode342. 4的幂
342. 4的幂class Solution {public: bool isPowerOfFour(int num) { if(num <= 0) return false; double num1 = sqrt(num); if((num1 - (int)num1) > 1e-13) return false; //判断num是不是2的幂 num = (int)num1; return原创 2020-06-14 11:35:23 · 96 阅读 · 0 评论 -
LeetCode231. 2的幂
231. 2的幂class Solution {public: bool isPowerOfTwo(int n) { if(n <= 0) return false; while(n % 2 == 0) { n /= 2; } return n == 1; }};class Solution {public: bool isPowerOfTwo(int n) {原创 2020-06-14 11:07:43 · 81 阅读 · 0 评论 -
LeetCode326. 3的幂
326. 3的幂class Solution {public: bool isPowerOfThree(int n) { if(n <= 0) return false; double e = (log10(n) / log10(3)) - (int)(log10(n) / log10(3)); return e < 1e-13; }};class Solution {public: bool isPowerOf原创 2020-06-14 10:21:41 · 90 阅读 · 0 评论 -
LeetCode454. 四数相加 II
454. 四数相加 IIclass Solution {public: //转化为两数之和 //时间O(n^2) int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) { int len = A.size(); if(len == 0) return 0;原创 2020-06-14 00:38:11 · 146 阅读 · 0 评论 -
LeetCode74. 搜索二维矩阵
74. 搜索二维矩阵class Solution {public: //binary search:时间O(len1 * len2), 空间O(1) bool searchMatrix(vector<vector<int>>& matrix, int target) { int len1 = matrix.size(); if(len1 == 0) return false; int len2 = matri原创 2020-06-14 00:08:23 · 114 阅读 · 0 评论 -
LeetCode240. 搜索二维矩阵 II
240. 搜索二维矩阵 IIclass Solution {public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int len1 = matrix.size(); if(len1 == 0) return false; int len2 = matrix[0].size(); int row = 0;原创 2020-06-13 23:53:12 · 90 阅读 · 0 评论 -
LeetCode189. 旋转数组
189. 旋转数组class Solution {public: //(aTbT)T == ba (T表示转置) void rotate(vector<int>& nums, int k) { int len = nums.size(); if(len <= 1) return; k = k % len; helper(nums, 0, len - k - 1); helper原创 2020-06-13 23:31:00 · 88 阅读 · 0 评论 -
LeetCode13. 罗马数字转整数
13. 罗马数字转整数class Solution {public: int romanToInt(string s) { int sum = 0; int preNum = getValue(s[0]); for(int i = 1; i < s.length(); i++) { int num = getValue(s[i]); if(preNum < num) {原创 2020-06-13 22:21:37 · 112 阅读 · 0 评论 -
LeetCode19. 删除链表的倒数第N个节点
19. 删除链表的倒数第N个节点/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) {原创 2020-06-13 21:49:41 · 78 阅读 · 0 评论