![](https://img-blog.csdnimg.cn/20210109204438888.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法刷题
https://codetop.cc/#/home
vector<>
这个作者很懒,什么都没留下…
展开
-
【九章算法】-动态规划
本文是B站课程的一个笔记https://www.bilibili.com/video/BV1xb411e7ww?from=search&seid=12976071769454038544动态规划题特点1.科技公司面试必考2.题目类型多,没有固定模板3.难度中上4.必须掌握什么题目可以用动态规划来解1.计数:如:有多少种方式2.求最值:如:最长上升子序列,最大数字和3.存在性:如:能不能选出k个数解题步骤例题:https://leetcode-cn..原创 2021-01-15 21:11:43 · 2055 阅读 · 2 评论 -
【java基础】使用java写算法题的注意事项
本篇博客记录用Java 刷 leetcode的以一些小trips,持续更新中~原创 2023-04-06 22:09:18 · 466 阅读 · 0 评论 -
【数组】LeetCode76-最小覆盖子串-滑动窗口
【数组】LeetCode76-最小覆盖子串-滑动窗口原创 2022-12-11 21:28:36 · 149 阅读 · 0 评论 -
【数组】LeetCode1109-航班预订统计-查分数组
1109 航班预订统计原创 2022-12-21 19:20:42 · 136 阅读 · 0 评论 -
【二维数组】LeetCode48-旋转图像
旋转图像原创 2022-12-22 11:03:48 · 145 阅读 · 0 评论 -
【数组】LeetCode26-删除有序数组中的重复项
LeetCode26-删除有序数组中的重复项原创 2022-12-05 21:35:19 · 172 阅读 · 0 评论 -
【数组】LeetCode304-二维区域和检索-前缀和
二维区域和检索-前缀和原创 2022-12-16 16:49:17 · 197 阅读 · 0 评论 -
【数组】LeetCode283-移动零
移动零原创 2022-12-07 19:38:26 · 137 阅读 · 0 评论 -
【数组】LeetCode567-字符串的排序-滑动窗口
字符串排序-滑动窗口原创 2022-12-12 22:05:46 · 181 阅读 · 0 评论 -
【LeetCode】斐波那契额数列
写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下原创 2022-10-31 13:13:31 · 147 阅读 · 0 评论 -
【单链表】LeetCode234-回文链表
【方法一:折腾链表】第一步:求单链表总长度第二步:分割成两个长度相等的单链表第三步:反转前半段链表第四步:依次遍历这两个单链表,判断是否相等注意牛客中给的链表的结构都是没有头节点的,自己加一个头节点会好操作很多bool isPail(ListNode* head) { // write code here int length=0; int k=0; ListNode* p=head; ListN..原创 2021-01-14 20:03:31 · 175 阅读 · 0 评论 -
【单链表】-反转链表的前N个结点
反转链表的前N个结点原创 2022-12-03 17:05:11 · 361 阅读 · 0 评论 -
【单链表】头插法和尾插法创建单链表
编程注意事项:1.尾插法注意输入数据结束之后尾指针标记为空2.复习:头插法的重要应用是链表逆置typedef struct LNode { int data; struct LNode *next;}LNode,*LinkList;LinkList createHead() { LinkList L = (LNode*)malloc(sizeof(LNode)); L->next = NULL; int x; while (true) { cin >&g.原创 2020-08-12 15:18:55 · 1484 阅读 · 0 评论 -
【单链表】-剑指22-2009考研真题-查找链表中倒数第k个位置上的节点
2009统考真题思路很巧妙两个指针:p指针先向前走k步,然后pq一起移动,这样,q永远都在p前面k个位置,所以当p到达链表尾部的时候,q就是尾部之前的k位置,也就是倒数k位置。 bool findTail_K(LinkList List,int k) { LNode *p = List->next; LNode *q = List->next; int step = 0; while (step<k) { p = p->next; .原创 2020-07-21 20:47:52 · 1022 阅读 · 0 评论 -
【单链表】LeetCode86-分割链表
LeetCode 86 分割链表原创 2022-12-01 15:28:24 · 145 阅读 · 0 评论 -
【单链表】LeetCode876-链表的中间结点
LeetCode 876原创 2022-12-01 21:44:16 · 149 阅读 · 0 评论 -
【单链表】LeetCode148-排序链表
148 排序链表原创 2022-12-21 19:12:13 · 149 阅读 · 0 评论 -
【剑指】-66-构造乘积数组
这道题难处在于要不能用除法且用双重循环会超时思路:用left[i]存储a[i]左侧所有数据的乘积,right[i]表示a[i]右侧所有数据的乘积,那么res[i]=right[i]*left[i]class Solution {public: vector<int> constructArr(vector<int>& a) { int n=a.size(); vector<int> res(n); ...原创 2021-03-13 20:04:17 · 135 阅读 · 0 评论 -
【王道机试】-矩阵-矩阵乘法-哈尔滨工业大学
求矩阵乘法还挺有意思的//#include "pch.h"#include <iostream>#include <vector>using namespace std;int main() { vector<vector<int>> num1(2, vector<int>(3)); vector<vector<int>> num2(3, vector<int>(2)); for (...原创 2021-03-13 18:19:35 · 172 阅读 · 0 评论 -
【王道机试】-栈-Zero-complexity transposition-上海交通大学
老实说没看懂题,搞得这么高大上,原来就是逆序输出而已#include <iostream>#include <vector>#include <algorithm>javascript:void(0);using namespace std;bool cmp(int a,int b){ if(a<0)a=0-a; if(b<0)b=0-b; return a>b;}int main(){ in..原创 2021-03-12 17:15:45 · 162 阅读 · 0 评论 -
【leetcode】-470-用Rand7()实现Rand10()
基本上属于全数学问题了,看了很久才看懂。以下转载自大佬kkbill的高赞解答。Part 1假设已知rand2()可以均匀的生成[1,2]的随机数,现在想均匀的生成[1,4]的随机数,该如何考虑?令两个rand2()相加,再做一些必要的边角处理。如下:rand2() + rand2() = ? ==> [2,4] 1 + 1 = 2 1 + 2 = 3 2 + 1 = 3 2 + 2 = 4...转载 2021-03-11 11:39:58 · 235 阅读 · 0 评论 -
【王道机试】-栈-堆栈的使用-吉林大学
//#include "pch.h"#include <iostream>#include <stack>#include <queue>using namespace std;int main() { int n; cin >> n; char opp; int numm; stack<int> st; queue<char> op; queue<int> num; for (int i =...原创 2021-03-11 11:19:23 · 184 阅读 · 0 评论 -
【王道机试】-向量-完数VS盈数-北京大学
注意输出格式,字符后面要有空格,不然通过不了。求一个数n的所有因数,就是遍历所有1~n-1的数求余判断#include <iostream>#include <vector>using namespace std;int main(){ int count; vector<int> wan; vector<int> ying; for(int num=2;num<=60;num++){ ..原创 2021-03-11 10:53:17 · 115 阅读 · 0 评论 -
【王道机试】-字符串匹配-string matching-上海交通大学
熟悉find函数的用法#include <iostream>#include <string>using namespace std;int main(){ string t; string p; cin>>t>>p; int k=0; int count=0; k=t.find(p); while(k!=-1){ count++; k=t.find(p,..原创 2021-03-10 11:43:48 · 218 阅读 · 0 评论 -
【王道机试】-字符串-首字母大写-北京大学
标注为较难的题,但是感觉就那样吧,记住转大写的函数。学校的复试题和leetcode难度相差太大了。#include <iostream>#include <string>using namespace std;int main(){ string s; getline(cin,s); if(s.length()!=0) s[0]=toupper(s[0]); for(int i=0;i<s.length();i++..原创 2021-03-10 11:20:21 · 142 阅读 · 0 评论 -
【王道机试】-字符串-密码翻译-北京大学
标注的是中等题,但是还是太简单了#include <iostream>#include <string>#include <vector>using namespace std;int main(){ string s; getline(cin,s); for(int i=0;i<s.length();i++) { if(s[i]=='z')s[i]='a'; ..原创 2021-03-10 11:06:16 · 128 阅读 · 0 评论 -
【王道机试】-查找-找位置-华中科技大学
花了好些时间才写出来,有更简单的方法是干脆用了两个标记,一个标记是否访问过,一个标记是否是第一次访问。我这个未免有点复杂了。#include <iostream>#include <vector>#include <algorithm>using namespace std;int main() { string s; cin>>s; int n = s.length(); vector<int> flag(n,0)..原创 2021-03-09 16:13:40 · 191 阅读 · 0 评论 -
【王道机试】-查找-找最小数-北邮
说实话用上sort之后都没有什么挑战性了,完全是熟悉一下怎么敲c++,后面还是做中等难度的题目吧。#include <iostream>#include <vector>#include <algorithm>using namespace std;struct num{ int x; int y;};bool cmp(num A,num B){ if(A.x!=B.x)return A.x<B.x; e...原创 2021-03-09 15:09:12 · 133 阅读 · 0 评论 -
【王道机试】-查找-查找-北邮
#include <iostream>#include <vector>#include <algorithm>#include <string>using namespace std;int main(){ int n; int m; cin>>n; vector<int> num(n); vector<int> a(m); for(int i=0;i...原创 2021-03-09 15:01:01 · 117 阅读 · 0 评论 -
【王道机试】-查找-找x-哈尔滨工业大学
#include <iostream>#include <vector>#include <algorithm>using namespace std;int main() { int n; cin >> n; vector<int> num(n); for (int i = 0; i < n; i++) { cin >> num[i]; } int temp; cin >> tem...原创 2021-03-09 14:52:28 · 139 阅读 · 0 评论 -
【王道机试】-排序-奥运排序问题-浙江大学
这绝对是我写过逻辑最复杂的一个题目,主要注意并列排序函数,另外由于奖牌人口比和金牌人口比,涉及到除法,而这一个题目又严格要求排序,所以最后想的办法是用浮点数来存储。原本想将四个并列排序函数整合整一个,但由于我用的是结构体存储排序结构,而非数组,并没有想到很好的整合方式。虽然看上去代码庞大,但是逻辑清晰!#include <iostream>#include <vector>#include <algorithm>using namespace std;...原创 2021-03-08 16:11:35 · 238 阅读 · 0 评论 -
【王道机试】-排序-整数奇偶排序-北京大学
#include <iostream>#include <algorithm>#include <vector>using namespace std;int main(){ vector<int> num(10); while(cin>>num[0]>>num[1]>>num[2]>>num[3]>>num[4]>>num[5]>>num[6]...原创 2021-03-08 10:15:40 · 131 阅读 · 0 评论 -
【王道机试】-排序-特殊排序-华中科技大学
//#include "pch.h"#include <iostream>#include <algorithm>#include <vector>using namespace std;int main() { int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) { cin >> nums[i]; } sort(nu...原创 2021-03-08 10:08:55 · 163 阅读 · 0 评论 -
【王道机试】-排序-成绩排序-清华大学
最主要的是cmp函数,其他倒是没有什么特别之处#include "pch.h"#include <iostream>#include <vector>#include <algorithm>using namespace std;struct student { int id; int score;};bool cmp(student A, student B) { if(A.score!=B.score)return A.score <..原创 2021-03-07 11:36:46 · 207 阅读 · 0 评论 -
【王道机试】-暴力解题-模拟-hello world for U-浙江大学
解2x+y-2=N这个方程,x必须小于等于y,这样有三种情况,x=y,x=y-1,x=y-2(x不可能等于y-3,因为那样就和x=y情况等价了) 原本以为最难的是求出n1,n2是多少,但是没想到被牛客搞到崩溃,现在真是对牛客的编辑器感到特别无语甚至愤怒,哎...下面的代码测试牛客上面所谓通不过的测试用例都是正常的!但是就是通过不了,我服了。//#include "pch.h"#include <iostream>#include <string>#inclu...原创 2021-03-07 11:04:54 · 173 阅读 · 0 评论 -
【王道机试】-暴力求解-枚举-与7无关的数-北京大学
熟练掌握正整数求各个位数还是很有必要的#include <iostream>using namespace std;bool with7(int num) { if (num%7==0) return false; //判断每个位 while (num != 0) { int temp = num % 10; if (temp == 7) return false; num = num / 10; } return true;}int ma..原创 2021-03-06 11:32:15 · 146 阅读 · 0 评论 -
【王道机试】-暴力求解-枚举-对称平方数1-清华大学
将int类型转化为string的方法要记住,这样方便了很多哟~#include <iostream>#include <string>#include <sstream>using namespace std;bool tos(int num){ //将int类型转化为string stringstream ss; ss<<num; string s1 = ss.str(); //开始判断string..原创 2021-03-06 10:43:35 · 127 阅读 · 0 评论 -
【王道机试】-暴力求解-枚举-反序数-清华大学
#include <iostream>using namespace std;int main(){ for(int a=1;a<10;a++){ for(int b=0;b<10;b++){ for(int c=0;c<10;c++){ for(int d=1;d<10;d++){ int num=a*1000+b*100+c*10+d; ...原创 2021-03-06 10:13:54 · 145 阅读 · 0 评论 -
【王道机试】-暴力求解-枚举-abc-清华大学
本来还在纠结abc是否各不相同,仔细看题目并没有具体要求。#include <iostream>#include<vector>using namespace std;int main(){ for(int a=0;a<10;a++){ for(int b=0;b<10;b++){ for(int c=0;c<10;c++){ int temp=a*100+b*10+c..原创 2021-03-06 10:08:26 · 152 阅读 · 0 评论 -
【leetcode】-113-路径总和
刚开始并不知道temp.pop_back();用来干什么。当右子树遍历完成之后进行回退,回退的时候要删除当前遍历结点,保证temp里面的路径始终是从根节点出发的。否则路径就会有重复。 vector<vector<int>> res; vector<int> temp; void dfs(TreeNode* root, int sum){ if(root==nullptr)return; temp.pus..原创 2021-03-05 13:39:40 · 125 阅读 · 0 评论