![](https://img-blog.csdnimg.cn/direct/7305052cb659465e9dcb582d8ee71a64.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法题
算法题
不负长风
这个作者很懒,什么都没留下…
展开
-
CyclicRotation
def solution(A, K): # write your code in Python 3.6 n = len(A) if n == 0 or n == 1: return A s = K % n for i in range(s): A = [A[n-1]] + A[:n] del A[n] return A原创 2021-02-26 10:44:33 · 170 阅读 · 0 评论 -
LeetCode 盒子中小球的最大数量
class Solution: def countBalls(self, lowLimit: int, highLimit: int) -> int: cnt = {} for i in range(lowLimit, highLimit + 1): s = sum(int(digit) for digit in str(i)) if s in cnt.keys(): cnt[s]原创 2021-01-31 14:13:44 · 222 阅读 · 0 评论 -
LeetCode 计算力扣银行的钱
class Solution {public: int totalMoney(int n) { vector<int> m(8, 0); for (int i = 1; i < 8; i++) { m[i] = i; } int money = 0; int index = 1; for (int i = 0; i < n; i++)原创 2021-01-28 19:43:11 · 173 阅读 · 0 评论 -
LeetCode 找到最高海拔
class Solution {public: int largestAltitude(vector<int>& gain) { vector<int> h; h.push_back(0); for (int i = 0; i < gain.size();i++) { h.push_back(gain[i] + h[i]); } int原创 2021-01-24 16:02:41 · 134 阅读 · 0 评论 -
LeetCode 交换链表中的节点
class Solution {public: ListNode* swapNodes(ListNode* head, int k) { int l = 1; ListNode* p = head; while (p->next != NULL) { p = p->next; l += 1; } int v1, v2; in原创 2021-01-24 16:01:44 · 77 阅读 · 0 评论 -
LeetCode 解码异或后的数组
class Solution {public: vector<int> decode(vector<int>& encoded, int first) { vector<int> ans; ans.push_back(first); for (int i = 0; i < encoded.size(); i++) { ans.push_back(ans[i]原创 2021-01-24 16:00:05 · 197 阅读 · 0 评论 -
LeetCode 将数组分成三个子数组的方案数
class Solution {public: int waysToSplit(vector<int>& nums) { int n = nums.size(); vector<int> presum(n + 1, 0); int MOD = 1e9 + 7; // 计算前缀和 for (int i = 1; i <= n; i++) {原创 2021-01-05 23:04:58 · 446 阅读 · 0 评论 -
LeetCode 大餐计数
class Solution {public: int countPairs(vector<int>& deliciousness) { // 每个键值对的键是元素,每个键值对的值目前为止该元素出现的个数。 unordered_map<int, int> m; int ans = 0; int MOD = 1e9 + 7; for (auto d : deliciousness)原创 2021-01-05 20:10:50 · 190 阅读 · 0 评论 -
LeetCode 卡车上的最大单元数
class Solution {public: static int cmp(vector<int> a, vector<int> b) { return a[1] > b[1]; } int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) { sort(boxTypes.begin(), boxTypes.e原创 2021-01-03 10:39:36 · 256 阅读 · 0 评论 -
剑指 Offer 29. 顺时针打印矩阵
class Solution {public: vector<int> spiralOrder(vector<vector<int>>& matrix) { if (matrix.size() == 0 || matrix[0].size() == 0) { return {}; } vector<int> order; i原创 2021-01-02 10:15:32 · 72 阅读 · 0 评论 -
剑指 Offer 09. 用两个栈实现队列
class CQueue {public: stack<int> a; stack<int> b; CQueue() { } void appendTail(int value) { a.push(value); } int deleteHead() { if (b.empty()) { while (!a.empty())原创 2021-01-02 09:40:28 · 82 阅读 · 0 评论 -
LeetCode35. 搜索插入位置
class Solution {public: int searchInsert(vector<int>& nums, int target) { int l = 0, r = nums.size() - 1; while (l <= r) { int mid = (l + r) / 2; if (nums[mid] == target) {原创 2020-11-03 18:41:28 · 87 阅读 · 0 评论 -
LeetCode524. 通过删除字母匹配到字典里最长单词
class Solution {public: static bool cmp(string a, string b) { return a.size() == b.size() ? a < b:a.size() > b.size(); } string findLongestWord(string s, vector<string>& d) { if (s.size() == 0) {原创 2020-11-02 09:02:48 · 72 阅读 · 0 评论 -
LeetCode207. 课程表
class Solution {public: vector<vector<int>> edges; vector<int> visited; vector<int> ans; bool valid = true; bool canFinish(int numCourses, vector<vector<int>>& prerequisites) { visited原创 2020-10-27 14:30:49 · 79 阅读 · 0 评论 -
LeetCode210. 课程表 II
class Solution {public: vector<vector<int>> edges; vector<int> visited; vector<int> ans; bool valid = true; vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {原创 2020-10-27 14:28:12 · 72 阅读 · 0 评论 -
LeetCode1042. 不邻接植花
class Solution {public: vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) { vector<vector<int>> G(n, vector<int>()); for (int i = 0;i < paths.size();i++) { G[path原创 2020-10-27 10:05:51 · 158 阅读 · 0 评论 -
LeetCode1456. 定长子串中元音的最大数目
class Solution {public: int maxVowels(string s, int k) { int current_cnt = 0; for (int i = 0; i < k; i++) { if (isValid(s[i])) { current_cnt++; } }原创 2020-10-27 09:16:23 · 151 阅读 · 0 评论 -
LeetCode1423. 可获得的最大点数
class Solution {public: int maxScore(vector<int>& cardPoints, int k) { int current_sum = 0; int n = cardPoints.size(); for (int i = 0; i < n - k; i++) { current_sum += cardPoints[i]; }原创 2020-10-26 19:08:12 · 131 阅读 · 0 评论 -
LeetCode567. 字符串的排列
class Solution {public: bool checkInclusion(string s1, string s2) { if (s1.size() > s2.size()) { return false; } int s1map[26] = { 0 }; int s2map[26] = { 0 }; for (int i = 0; i < s1.原创 2020-10-26 18:35:35 · 56 阅读 · 0 评论 -
LeetCode剑指 Offer 48. 最长不含重复字符的子字符串
class Solution {public: int maxFun(int a, int b) { return a > b ? a : b; } int lengthOfLongestSubstring(string s) { int n = s.size(); if (n < 2) { return n; } in原创 2020-10-26 17:14:05 · 71 阅读 · 0 评论 -
LeetCode剑指 Offer 59 - II. 队列的最大值
class MaxQueue {public: deque<int> dq; queue<int> q; MaxQueue() { } int max_value() { if (dq.empty()) { return -1; } else { return dq.front(); }原创 2020-10-26 16:46:22 · 67 阅读 · 0 评论 -
LeetCode剑指 Offer 59 - I. 滑动窗口的最大值
class Solution {public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> res; if (nums.size() == 0) { return res; } deque<int> dq;原创 2020-10-26 16:29:40 · 107 阅读 · 0 评论 -
LeetCode239. 滑动窗口最大值
class Solution {public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> res; deque<int> dq; for (int i = 0;i < k; i++) { while (!dq.empty() &原创 2020-10-26 14:17:18 · 81 阅读 · 0 评论 -
LeetCode424. 替换后的最长重复字符
class Solution {public: int characterReplacement(string s, int k) { int map[26] = { 0 }; int left = 0; int right = 0; int max_cnt = 0; int res = 0; while (right < s.size()) { map原创 2020-10-23 09:33:04 · 76 阅读 · 0 评论 -
LeetCode1004. 最大连续1的个数 III
class Solution {public: int longestOnes(vector<int>& A, int K) { int n = A.size(); int left = 0; int right = 0; int max_cnt = 0; int map[2] = { 0 }; int res = 0; while (原创 2020-10-23 09:25:59 · 91 阅读 · 0 评论 -
LeetCode485. 最大连续1的个数
class Solution {public: int findMaxConsecutiveOnes(vector<int>& nums) { int max_cnt = 0; int cnt = 0; for (int i = 0; i < nums.size(); i++) { if (nums[i] != 1) {原创 2020-10-22 16:38:43 · 69 阅读 · 0 评论 -
LeetCode16. 最接近的三数之和
class Solution {public: int threeSumClosest(vector<int>& nums, int target) { int n = nums.size(); sort(nums.begin(), nums.end()); int best = nums[0] + nums[1] + nums[2]; for (int first = 0; first < n - 2原创 2020-10-22 15:28:44 · 55 阅读 · 0 评论 -
LeetCode15. 三数之和
class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> ans; int n = nums.size(); if (n < 3) { return ans; } sort原创 2020-10-22 15:04:49 · 83 阅读 · 0 评论 -
LeetCode剑指 Offer 30. 包含min函数的栈
class MinStack {public: /** initialize your data structure here. */ stack<int> x_stack; stack<int> min_stack; MinStack() { min_stack.push(INT_MAX); } void push(int x) { x_stack.push(x); min_stac原创 2020-10-22 11:06:14 · 70 阅读 · 0 评论 -
LeetCode155. 最小栈
class MinStack {public: /** initialize your data structure here. */ stack<int> x_stack; stack<int> min_stack; MinStack() { min_stack.push(INT_MAX); } void push(int x) { x_stack.push(x); min_stac原创 2020-10-22 10:59:45 · 85 阅读 · 0 评论 -
LeetCode167. 两数之和 II - 输入有序数组
class Solution {public: vector<int> twoSum(vector<int>& numbers, int target) { int l = 0, r = numbers.size() - 1; while (l < r) { int sum = numbers[l] + numbers[r]; if (sum == target)原创 2020-10-19 16:55:47 · 53 阅读 · 0 评论 -
LeetCode剑指 Offer 27. 二叉树的镜像
class Solution {public: TreeNode* mirrorTree(TreeNode* root) { modifyTree(root); return root; } void modifyTree(TreeNode* root) { if (root == NULL) { return; } modifyTree(root-&g原创 2020-10-16 15:22:11 · 72 阅读 · 0 评论 -
LeetCode406. 根据身高重建队列
class Solution {public: static bool cmp(vector<int> a, vector<int> b) { return a[0] == b[0] ? a[1] <= b[1] : a[0] > b[0]; } vector<vector<int>> reconstructQueue(vector<vector<int>>& p原创 2020-10-16 15:08:51 · 102 阅读 · 0 评论 -
LeetCode55. 跳跃游戏
class Solution {public: bool canJump(vector<int>& nums) { int n = nums.size(); int right_most = 0; for (int i = 0; i < n; i++) { if (i <= right_most) {原创 2020-10-16 14:37:30 · 70 阅读 · 0 评论 -
LeetCode122. 买卖股票的最佳时机 II
class Solution {public: int maxProfit(vector<int>& prices) { int i = 0; int valley = -1; int peak = -1; int max_profit = 0; while (i < prices.size() - 1) { // find valley原创 2020-10-16 14:09:07 · 74 阅读 · 0 评论 -
LeetCode3. 无重复字符的最长子串
class Solution {public: int lengthOfLongestSubstring(string s) { int size = s.size(); if(size < 2) { return size; } unordered_set<char> charset; int slow = 0, fast = 0; in原创 2020-09-15 18:50:19 · 113 阅读 · 0 评论 -
LeetCode62. 不同路径
简单的动态规划问题。class Solution {public: int uniquePaths(int m, int n) { vector<vector<int>> matrix(n, vector<int>(m)); for (int i = 0; i < m; i++) { matrix[0][i] = 1; } for原创 2020-09-15 15:58:04 · 73 阅读 · 0 评论 -
LeetCode42. 接雨水
位置0和位置size-1不会积累雨水,计算的时候不需要考虑。class Solution {public: int trap(vector<int>& height) { int size = height.size(); if(!size) { return 0; } vector<int> left_max(size), right_max(size);原创 2020-09-15 09:37:41 · 105 阅读 · 0 评论 -
LeetCode49. 字母异位词分组
class Solution {public: vector<vector<string>> groupAnagrams(vector<string>& strs) { vector<vector<string>> ans; if (strs.size() == 0) { return ans; } unordered_map原创 2020-09-14 10:11:36 · 63 阅读 · 0 评论 -
LeetCode面试题 17.10. 主要元素
这道题主要使用摩尔投票法。class Solution {public: int majorityElement(vector<int>& nums) { int major = nums[0]; int count = 1; for (int i = 1;i < nums.size();i++) { if (nums[i] == major) {原创 2020-09-14 09:34:47 · 160 阅读 · 0 评论