随笔分类 - LeetCode
leetcode刷题
摘要:【leetcode】1438. 绝对差不超过限制的最长连续子数组 给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于 limit 。 如果不存在满足条件的子数组,则返回 0 。 很好的一道关于滑动窗口的题
阅读全文
摘要:原地交换: 思路很简单先对角线对称交换,再左右对称交换就可以得到旋转90度。 线性代数证明方法:等我复习完orz class Solution { public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); fo
阅读全文
摘要:买卖股票的最佳时机含手续费 一个简单DP 用一个二维数组dp[n][2], 其中dp[i][0]代表前i个元素此时无股票的最大利润,dp[i][1]代表前i个元素此时有股票的最大利润。 最后答案求解的就是dp[n - 1][0]就是答案。 class Solution { public: int m
阅读全文
摘要:对二叉树进行中序遍历,用maxCount记录最大值。如果存在count==maxCount,压进vector之中。如果count更大,更新vector。 /** * Definition for a binary tree node. * struct TreeNode { * int val; *
阅读全文
摘要:递归算法easy题 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), lef
阅读全文
摘要:class LRUCache { private: int capacity; list<pair<int, int>> cache; unordered_map<int, list<pair<int, int>> :: iterator> map; public: LRUCache(int cap
阅读全文
摘要:直接模拟就好了 class Solution { public: int isPrefixOfWord(string sentence, string searchWord) { int ans = 1; for(int j = 0; j < searchWord.size(); ++j) { if
阅读全文
摘要:直接用DFS进行搜索即可 class Solution { public: int numIslands(vector<vector<char>>& grid) { if(grid.empty()) { return 0; } int ans = 0; for(int i = 0; i < grid
阅读全文
摘要:思路:模拟 class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { int a[10]; for(int i = 0; i < board.size(); ++i) { memset(a, 0, sizeo
阅读全文
摘要:思路:也没啥好说的了,就是模拟然后注意输入可能为空的判断。运用运算符重载先sort一波。应该有更简单的解法,待我研究出来再更新。 class Solution { public: static bool cmp(pair<int, int> P1, pair<int, int> P2) { if(P
阅读全文
摘要://这里采用dfs算法 class Solution { public: vector<string> generateParenthesis(int n) { vector<string> res; func(res, "", 0, 0, n); return res; } void func(v
阅读全文
摘要:class Solution { public: vector<string> wordBreak(string s, vector<string>& wordDict) { if (m.count(s)) return m[s]; //判断map中有没有s if (s.empty()) retur
阅读全文

浙公网安备 33010602011771号