文章分类 -  coding

算法竞赛
摘要:845. 八数码 #include <bits/stdc++.h> using namespace std; unordered_map<string, int> mp; string start; bool flag; void bfs(){ queue<string> q; q.push(sta 阅读全文
posted @ 2025-03-21 21:32 awei040519 阅读(25) 评论(0) 推荐(0)
摘要:30. 串联所有单词的子串 class Solution { public: vector<int> findSubstring(string s, vector<string>& words) { vector<int> res; int n = s.size(), m = words.size( 阅读全文
posted @ 2025-03-15 18:07 awei040519 阅读(18) 评论(0) 推荐(0)
摘要:29. 两数相除 class Solution { public: int divide(int dividend, int divisor) { typedef long long LL; vector<LL> exp; bool is_minus = false; if(dividend < 0 阅读全文
posted @ 2025-03-15 17:43 awei040519 阅读(15) 评论(0) 推荐(0)
摘要:K 个一组翻转链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode 阅读全文
posted @ 2025-03-15 16:46 awei040519 阅读(22) 评论(0) 推荐(0)
摘要:23. 合并 K 个升序链表 使用堆 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * 阅读全文
posted @ 2025-03-15 16:22 awei040519 阅读(21) 评论(0) 推荐(0)
摘要:22. 括号生成 class Solution { public: vector<string> ans; vector<string> generateParenthesis(int n) { dfs(n, 0, 0, ""); return ans; } void dfs(int n, int 阅读全文
posted @ 2025-03-15 16:12 awei040519 阅读(13) 评论(0) 推荐(0)
摘要:18. 四数之和 O(n^3) class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { sort(nums.begin(), nums.end()); vector<vector<in 阅读全文
posted @ 2025-03-15 14:31 awei040519 阅读(22) 评论(0) 推荐(0)
摘要:16. 最接近的三数之和 class Solution { public: int threeSumClosest(vector<int>& nums, int target) { //存放差值和数值和 pair<int, int> res(INT_MAX, INT_MAX); sort(nums. 阅读全文
posted @ 2025-03-15 11:23 awei040519 阅读(13) 评论(0) 推荐(0)
摘要:15. 三数之和 class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res; sort(nums.begin(), nums.end()); for(int i 阅读全文
posted @ 2025-03-15 10:59 awei040519 阅读(18) 评论(0) 推荐(0)
摘要:11. 盛最多水的容器 从两边向中间,最短的板子决定了最大存水量,所以应该移动最短的,如果移动最长的,随着距离变短,最大存水量的高度不变,会使得最大存水量变小 class Solution { public: int maxArea(vector<int>& height) { int res = 阅读全文
posted @ 2025-03-15 10:22 awei040519 阅读(12) 评论(0) 推荐(0)
摘要:50. Pow(x, n) n很大,故而使用快速幂 class Solution { public: double myPow(double x, int n) { bool is_minus = n < 0; double res = 1; for(long long k = abs((long 阅读全文
posted @ 2025-03-14 16:48 awei040519 阅读(15) 评论(0) 推荐(0)
摘要:48. 旋转图像 先沿着副对角线翻转,然后水平翻转 class Solution { public: void rotate(vector<vector<int>>& matrix) { int n = matrix.size(); for(int i = 0; i < n; ++i){ for(i 阅读全文
posted @ 2025-03-14 16:32 awei040519 阅读(15) 评论(0) 推荐(0)
摘要:47. 全排列 II 对于相同的数,只用第一个没有用过的 class Solution { public: vector<vector<int>> ans; vector<int> path; vector<bool> st; vector<vector<int>> permuteUnique(ve 阅读全文
posted @ 2025-03-14 16:11 awei040519 阅读(14) 评论(0) 推荐(0)
摘要:46. 全排列 class Solution { public: vector<vector<int>> ans; vector<int> path; vector<bool> st; vector<vector<int>> permute(vector<int>& nums) { int n = 阅读全文
posted @ 2025-03-14 15:58 awei040519 阅读(16) 评论(0) 推荐(0)
摘要:45. 跳跃游戏 II class Solution { public: int jump(vector<int>& nums) { int n = nums.size(); vector<int> f(n, 0); //f[i]表示从起点跳到i的最小步数 //找到每段距离的最大右边界 for(in 阅读全文
posted @ 2025-03-14 15:45 awei040519 阅读(19) 评论(0) 推荐(0)
摘要:44. 通配符匹配 f[0][j]可能会匹配,而f[i][0]一定不匹配,所以i从0开始,而j从1开始即可 class Solution { public: bool isMatch(string s, string p) { int n = s.size(), m = p.size(); s = 阅读全文
posted @ 2025-03-13 16:14 awei040519 阅读(19) 评论(0) 推荐(0)
摘要:43. 字符串相乘 class Solution { public: string multiply(string num1, string num2) { vector<int> A, B; int n = num1.size(), m = num2.size(); for(int i = n - 阅读全文
posted @ 2025-03-13 15:53 awei040519 阅读(20) 评论(0) 推荐(0)
摘要:42. 接雨水 每当插入一个,就要把前面比它小的给删去,因为能存水,最后存有绿色的 class Solution { public: int trap(vector<int>& height) { stack<int> stk; int res = 0; for(int i = 0; i < hei 阅读全文
posted @ 2025-03-13 15:38 awei040519 阅读(19) 评论(0) 推荐(0)
摘要:60. 排列序列 使用next_permutation O(n!k) class Solution { public: string getPermutation(int n, int k) { string res; for(int i = 1; i <= n; ++i) res += to_st 阅读全文
posted @ 2025-03-12 21:43 awei040519 阅读(28) 评论(0) 推荐(0)
摘要:57. 插入区间 class Solution { public: vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) { vector<vector<int>> res; int 阅读全文
posted @ 2025-03-12 21:19 awei040519 阅读(14) 评论(0) 推荐(0)