每日算法题目
leetcode题目解法及多种语言的解法,每天7题打卡
不能say的秘密
这个作者很懒,什么都没留下…
展开
-
Leetcode 554~561题
第五百五十四题:class Solution {public: int leastBricks(vector<vector<int>>& wall) { unordered_map<int, int> hash; for (auto& line: wall) { for (int i = 0, s = 0; i < (int)line.size() - 1; i ++ ) {原创 2021-04-20 22:31:26 · 95 阅读 · 1 评论 -
Leetcode 542~553题
第五百四十二题:#define x first#define y secondclass Solution {public: vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) { if (matrix.empty() || matrix[0].empty()) return matrix; int n = matrix.size(原创 2021-04-19 21:51:10 · 95 阅读 · 0 评论 -
Leetcode 532~541题
第五百三十二题:class Solution {public: int findPairs(vector<int>& nums, int k) { sort(nums.begin(), nums.end()); int res = 0; for (int i = 0, j = 0; i < nums.size(); i ++ ) { while (i + 1 < nums.size() &am原创 2021-04-18 22:56:20 · 56 阅读 · 0 评论 -
Leetcode 523~530题
第五百二十三题:class Solution {public: bool checkSubarraySum(vector<int>& nums, int k) { if (!k) { for (int i = 1; i < nums.size(); i ++ ) if (!nums[i - 1] && !nums[i]) return true原创 2021-04-17 22:41:55 · 101 阅读 · 0 评论 -
Leetcode 516~522题
第五百一十六题:class Solution {public: int longestPalindromeSubseq(string s) { int n = s.size(); vector<vector<int>> f(n, vector<int>(n)); for (int len = 1; len <= n; len ++ ) for (int i = 0; i + len原创 2021-04-15 22:08:10 · 124 阅读 · 0 评论 -
Leetcode 506~515题
第五百零六题:class Solution {public: vector<string> findRelativeRanks(vector<int>& nums) { vector<pair<int, int>> q; for (int i = 0; i < nums.size(); i ++ ) q.push_back({nums[i], i}); sort(q.begin(), q原创 2021-04-10 22:57:17 · 87 阅读 · 0 评论 -
Leetcode 497~504题
第四百九十七题:class Solution {public: int n; vector<vector<int>> rects; vector<int> s; Solution(vector<vector<int>>& _rects) { rects = _rects; n = rects.size(); s.push_back(0);原创 2021-04-09 22:42:04 · 116 阅读 · 0 评论 -
Leetcode 488~496题
第四百八十八题:class Solution {public: unordered_map<string, int> f; unordered_map<char, int> cnt; int ans = 6; int findMinStep(string board, string hand) { for (auto c: hand) cnt[c] ++ ; f[board] = 0; dfs(bo原创 2021-04-08 21:36:11 · 103 阅读 · 0 评论 -
Leetcode 479~486题
第四百七十九题:class Solution {public: int largestPalindrome(int n) { typedef long long LL; if (n == 1) return 9; int maxv = pow(10, n) - 1; for (int i = maxv; ;i -- ) { auto a = to_string(i); auto b =原创 2021-04-07 22:29:01 · 71 阅读 · 0 评论 -
Leetcode 472~478题
第四百七十二题:class Solution {public: unordered_set<string> hash; bool check(string& word) { int n = word.size(); vector<int> f(n + 1, INT_MIN); f[0] = 0; for (int i = 0; i <= n; i ++ ) {原创 2021-04-02 22:12:58 · 115 阅读 · 0 评论 -
Leetcode 462~470题
第四百六十二题:class Solution {public: int minMoves2(vector<int>& nums) { int n = nums.size(); sort(nums.begin(), nums.end()); int res = 0; for (int i = 0; i < n; i ++ ) res += abs(nums[i] - nums[n /原创 2021-04-01 23:05:22 · 191 阅读 · 0 评论 -
Leetcode 455~461题
第四百五十五题:class Solution {public: int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end()); int res = 0; for (int i = 0, j = 0; i < g.size(); i +原创 2021-03-27 20:10:31 · 83 阅读 · 0 评论 -
Leetcode 448~454题
第四百四十八题:class Solution {public: vector<int> findDisappearedNumbers(vector<int>& nums) { for (auto x: nums) { x = abs(x); if (nums[x - 1] > 0) nums[x - 1] *= -1; } vector<int> re原创 2021-03-26 22:54:45 · 781 阅读 · 2 评论 -
Leecode 440~447题
第四百四十题:class Solution {public: int f(int prefix, int n) { long long p = 1; auto A = to_string(n), B = to_string(prefix); int dt = A.size() - B.size(); int res = 0; for (int i = 0; i < dt; i ++ ) {原创 2021-03-24 21:40:55 · 118 阅读 · 0 评论 -
Leetcode 432~438题
第四百三十二题:class AllOne {public: struct Node { Node *left, *right; int val; unordered_set<string> S; Node (int _val) { val = _val; left = right = NULL; } }*left, *right; unor原创 2021-03-21 14:44:07 · 155 阅读 · 0 评论 -
Leetcode 420-430题
第四百二十题:class Solution {public: int strongPasswordChecker(string s) { int a = 0, b = 0, c = 0, n = s.size(), k = 0; for (auto x: s) { if (x >= '0' && x <= '9') a = 1; else if (x >= 'a' &&原创 2021-03-20 12:07:34 · 123 阅读 · 0 评论 -
Leetcode 412~419题
第四百一十二题:class Solution {public: vector<string> fizzBuzz(int n) { vector<string> res; for (int i = 1; i <= n; i ++ ) if (i % 3 == 0 && i % 5 == 0) res.push_back("FizzBuzz"); else if (i % 3原创 2021-03-18 18:17:00 · 135 阅读 · 0 评论 -
LeetCode 403~410题
第四百零三题int f[2000][2000];class Solution {public: unordered_map<int, int> hash; vector<int> stones; int dp(int i, int j) { if (f[i][j] != -1) return f[i][j]; f[i][j] = 0; for (int k = max(1, j - 1); k <原创 2021-03-05 09:10:10 · 87 阅读 · 1 评论 -
LeetCode 396~402题
第三百九十六题class Solution {public: int maxRotateFunction(vector<int>& A) { typedef long long LL; LL sum = 0, cur = 0; for (auto c: A) sum += c; int n = A.size(); for (int i = 0; i < n; i ++ ) cur += i原创 2021-03-04 08:19:48 · 139 阅读 · 1 评论 -
LeetCode 389~395题
第三百八十九题class Solution {public: char findTheDifference(string s, string t) { int x = 0; for (auto c: s) x ^= c; for (auto c: t) x ^= c; return x; }};class Solution { public char findTheDifference(String s, St原创 2021-03-01 10:32:30 · 141 阅读 · 0 评论 -
LeetCode 382~388题
第三百八十二题/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* h; /** @param head The linked list's head.原创 2021-02-25 10:51:46 · 113 阅读 · 0 评论 -
LeetCode 374~381题
第三百七十四题/** * Forward declaration of guess API. * @param num your guess * @return -1 if num is lower than the guess number * 1 if num is higher than the guess number * otherwise return 0 * int guess(int num); *原创 2021-02-24 21:00:18 · 88 阅读 · 0 评论 -
LeetCode 363~373题
第三百六十三题class Solution {public: vector<vector<int>> s; int get(int x1, int y1, int x2, int y2) { return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]; } int maxSumSubmatrix(vector<vector<int>&g原创 2021-02-24 10:13:14 · 173 阅读 · 0 评论 -
LeetCode 347~357题
第三百四十七题:class Solution {public: vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> cnt; for (auto x: nums) cnt[x] ++ ; int n = nums.size(); vector<int> s(n + 1);原创 2021-02-21 09:30:37 · 148 阅读 · 0 评论 -
LeetCode 337~345题
第三百三十七题:/** * 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: int rob(TreeNode* root原创 2021-02-19 08:38:43 · 82 阅读 · 0 评论 -
LeetCode 329~336题
第三百二十九题:class Solution {public: int n, m; vector<vector<int>> f, w; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; int dp(int x, int y) { auto& v = f[x][y]; if (v != -1) return v; v = 1;原创 2021-02-18 09:05:14 · 118 阅读 · 0 评论 -
LeetCode 319~328题
第三百一十九题:class Solution {public: int bulbSwitch(int n) { return sqrt(n); }};class Solution { public int bulbSwitch(int n) { return (int)Math.sqrt(n); }}class Solution: def bulbSwitch(self, n: int) -> int:原创 2021-02-18 08:45:04 · 94 阅读 · 0 评论 -
LeetCode 309~318题
第三百零九题:class Solution {public: int maxProfit(vector<int>& prices) { if (prices.empty()) return 0; int n = prices.size(), INF = 1e8; vector<vector<int>> f(n, vector<int>(3, -INF)); f[0][1] =原创 2021-02-16 07:46:39 · 171 阅读 · 0 评论 -
LeetCode 299~307题
第二百九十九题:class Solution {public: string getHint(string secret, string guess) { unordered_map<char, int> hash; for (auto c: secret) hash[c] ++ ; int tot = 0; for (auto c: guess) if (hash[c]) {原创 2021-02-15 11:16:30 · 132 阅读 · 0 评论 -
LeetCode 284~297题
第二百八十四题:/* * Below is the interface for Iterator, which is already defined for you. * **DO NOT** modify the interface for Iterator. * * class Iterator { * struct Data; * Data* data; * Iterator(const vector<int>& nums); *原创 2021-02-04 09:44:50 · 110 阅读 · 0 评论 -
LeetCode 273~283题
第二百七十三题:class Solution {public: string num0_19[20] = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eigh原创 2021-02-03 10:55:34 · 107 阅读 · 0 评论 -
LeetCode 242~268题
第二百四十二题:class Solution {public: bool isAnagram(string s, string t) { unordered_map<char, int> a, b; for (auto c: s) a[c] ++ ; for (auto c: t) b[c] ++ ; return a == b; }};class Solution { public boolean原创 2021-02-02 11:34:55 · 66 阅读 · 0 评论 -
LeetCode 235~241题
第二百三十五题:/** * 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: TreeNode* lowestCommo原创 2021-01-31 11:03:35 · 96 阅读 · 0 评论 -
LeeCode 228~234题:
第二百二十八题:class Solution {public: vector<string> summaryRanges(vector<int>& nums) { vector<string> res; for (int i = 0; i < nums.size(); i ++ ) { int j = i + 1; while (j < nums.size()原创 2021-01-30 08:29:49 · 117 阅读 · 0 评论 -
LeetCode 221~227题
第二百二十一题:class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { if (matrix.empty() || matrix[0].empty()) return 0; int n = matrix.size(), m = matrix[0].size(); vector<vector<int>>原创 2021-01-29 07:48:19 · 128 阅读 · 0 评论 -
LeetCode 214~220题:
第二百一十四题:class Solution {public: string shortestPalindrome(string s) { string t(s.rbegin(), s.rend()); int n = s.size(); s = ' ' + s + '#' + t; vector<int> ne(n * 2 + 2); for (int i = 2, j = 0; i <= n *原创 2021-01-28 13:20:06 · 121 阅读 · 1 评论 -
LeetCode 207~213题:
第二百零七题:class Solution {public: bool canFinish(int n, vector<vector<int>>& edges) { vector<vector<int>> g(n); vector<int> d(n); for (auto& e: edges) { int b = e[0], a = e[1];原创 2021-01-26 10:51:28 · 127 阅读 · 0 评论 -
LeetCode 200~206题
第二百题:class Solution {public: vector<vector<char>> g; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; int numIslands(vector<vector<char>>& grid) { g = grid; int cnt = 0; for (int i = 0; i原创 2021-01-24 11:41:11 · 67 阅读 · 0 评论 -
LeetCode 187~199题
第一百八十七题:class Solution {public: vector<string> findRepeatedDnaSequences(string s) { unordered_map<string, int> cnt; for (int i = 0; i + 10 <= s.size(); i ++ ) cnt[s.substr(i, 10)] ++ ; vector<stri原创 2021-01-23 21:18:29 · 112 阅读 · 0 评论 -
LeetCode168~179t题:
第一百六十八题:class Solution {public: string convertToTitle(int n) { int k = 1; for (long long p = 26; n > p; p *= 26) { n -= p; k ++ ; } n -- ; string res; while (k -- ) {原创 2021-01-22 08:59:08 · 155 阅读 · 0 评论