摘要:这题还是挺就经典的,我的解决思路如下: 高度为H,则有H行需要打印,每行打印的内容大同小异,所以只需要H次循环就好了。大体框架就解决了,然后考虑细节。 第一行有H个星星 第二行有H+2个星星 第三行有H+4个星星 可知 第X行有H+(X-1)*2个星星 第H行有H+(H-1)*2个星星,也就是3H-
阅读全文
摘要:这题有一个前人总结的终极优化版本。 思想其实也很简单。学起来也很快。优化了如果变成下一年的情况和闰年判断 #include <stdio.h> #include <stdbool.h> int main( ) { int A[13] = {0,31,28,31,30,31,30,31,31,30,3
阅读全文
摘要:这也是一道经典的算法题。 其实也是用两个数组。还有判断是否闰年。 两个个循环,外面一个是月份循环,内部一个是每个月的天数循环,然后计数器Count++就行,直到和天数相同就跳出循环,打印就行。 #include <stdio.h> int judge ( int year ) { if (year
阅读全文
摘要:这题算是非常经典的题目了。 无非就是判断闰年然后计算天数而已。 用两个month数组记录月份天数 一三五七八十腊是31天,二月份非闰年28天,闰年29 天,其余都是30天就好了。 #include <stdio.h> int judge( int year ) { if( year % 400 ==
阅读全文
摘要:图形问题的万金解决方法就是创建一个二维数组,然后将填数组,最后打印数组就行了。其本质还是找出图形的规律。 首先来找规律,先从外形上来找。 奇数高,看图形,是上下左右对称的。所以只找上半区的规律。 然后首行比其他行少两个字符也就是多两个空格,最外层都是A,数组可以提前都赋值。只需要管中间部分 从中间部
阅读全文
摘要:这题其实就是多了一个字符串转化成数字而已。 用一个字符串数组和字符串比较函数就可以得出数字月份然后就简单了。 然后最后一个难点就是确定是星期几,可以根据今天的日期的星期当作固定点,找相差几天然后得出具体星期。 #include <stdio.h> #include <stdbool.h> #incl
阅读全文
摘要:因为考研机试的原因,C和C++最好都准备一下,所以有C++版本。 #include <iostream> #include <cstring> #include <map> using namespace std ; int cmp(int year,int mouth,int day){ if(y
阅读全文
摘要:这题思路可以说是太简单了。 用一个数组表示树,值为1表示有树,值为零表示无就行。、 最后统计1的个数即为剩下的树。 #include <stdio.h> #include <malloc.h> void move (int A[],int head,int tail){ for( ; head <=
阅读全文
摘要:#include<iostream> using namespace std; void move (int A[],int head, int tail){ for( ; head <= tail ; head++ ){ A[head]=0; } } int main( ){ int l = 0
阅读全文
摘要:这题出的只能说是无语。思路还是很简单的。 只要用一个的tag标记上次是哪个按键即可,然后tag和现在对比,要是相同就多加2。 #include<iostream> #include <map> using namespace std; int main(){ map<char,int>Map={ {
阅读全文
摘要:#include<stdio.h> int main(){ char A[4][8]={ 'a','d','g','j','m','p','t','w', 'b','e','h','k','n','q','u','x', 'c','f','i','l','o&
阅读全文
摘要:c++还是方便啊,直接调用库函数就可以实现排序了。不用自己实现排序函数了。 #include<iostream> #include<algorithm> using namespace std; int main(){ int A[101]={0}; int n = 0 ; while(cin >>
阅读全文
摘要:写个快排就完事了。实在不行,写个选择排序也很简单。 #include<stdio.h> int devide(int A[],int head,int tail){ if(head==tail) return head; int t = A[head] ; while(head < tail){ w
阅读全文
摘要:建两个数组就好了,一个存奇数一个存偶数,然后sort一下,最后输出。 #include <iostream> #include <algorithm> using namespace std; bool comp(int left,int right){ if(left > right) retur
阅读全文
摘要:#include <stdio.h> int divide(int A[],int head,int tail){ if(head==tail) return head; int t=A[head]; while(head<tail){ while(head<tail && A[tail]>t )
阅读全文
摘要:sort实在是太好用了。活用sort,一切排序题目都可以秒杀。 #include <iostream> #include <algorithm> using namespace std; struct node{ int num; int date; }; typedef struct node s
阅读全文
摘要:#include<stdio.h> typedef struct node{ int num; int data; }student; int divide1(student A[],int head,int tail){ if(head==tail) return head; int t=A[he
阅读全文
摘要:用C++库函数sort秒杀了,建一个结构体就好了,同时储存输入次序。 #include<iostream> #include<algorithm> #include<cstdlib> using namespace std; struct node{ int num; char x[20]; int
阅读全文
摘要:创建一个结构体,然后按要求快排就行了。 #include <stdio.h> #include <stdlib.h> typedef struct node{ int num; char S[100]; int score; }student; int divide1(student* A,int
阅读全文
摘要:摆了几天,重新再来学习。 ‘ 把数据输入数组,然后遍历数组就行了,没什么难度。 #include<iostream> #include<cstdlib> using namespace std; int main(){ int n; while(cin >> n){ int* A=(int*)mal
阅读全文
摘要:#include<stdio.h> #include<stdlib.h> int main(){ int n = 0; while(scanf("%d",&n)!=EOF){ int* A=(int*)malloc(sizeof(int )*n); for(int i = 0 ; i < n ;i+
阅读全文
摘要:二分查找,没什么好说的。关键在于排成有序数组。然而C++调用sort就可以了。 #include<iostream> #include<algorithm> #include<cstdlib> using namespace std; bool judge(int* A, int n ,int t)
阅读全文
摘要:C写个快排就行了。 #include<stdio.h> #include<stdlib.h> #include<stdbool.h> int divide(int* A,int head, int tail){ if(head==tail) return head ; int x =A[head];
阅读全文
摘要:练习使用向量vector容器。 遍历每个数取余就好了。然后记录下来。 #include<iostream> #include<vector> using namespace std; int main() { vector<int> B ; vector<int> C ; for (int i =
阅读全文
摘要:、 #include<stdio.h> int sum(int x){ int sum = 0; for(int i = 1; i < x ;i++){ if(x%i==0) sum+=i; } return sum ; } int main(){ int A[60]={0}; int B[60]=
阅读全文
摘要:\ 这题思路还是挺多的。 如果用数学的角度考虑。知道了n,p,m自然就知道下一个要出队的人的编号。然后一个个输出就行了。 还可以用循环链表做。 还可以用队列。出队在入队。 #include<iostream> #include<queue> using namespace std; int main
阅读全文
摘要:做个循环列表就行了。 逻辑上想想还是很简单的。 然而在实践的时候需要考虑许多边界情况。每次循环的时候要考虑头节点的问题。 #include<stdio.h> #include<stdlib.h> struct node{ int data; struct node* next; }; typedef
阅读全文
摘要:#include<iostream> #include<queue> using namespace std; void delect(queue<int>* q,int x){ if(q->empty()) return; for(int i=0;i<q->size();i++){ int t=q
阅读全文
摘要:h 很简单的题目,不管是用数组还是用栈都非常简单。 #include<iostream> #include<stack> using namespace std; int main(){ int n; while(cin >> n){ stack<long> s; while(n!=0){ int
阅读全文
摘要:用数组倒序输出就行了。 #include<stdio.h> #include<stdlib.h> int main(){ int n; while(scanf("%d",&n)!=EOF){ long* a=(long*)malloc(sizeof(long)*n); for(int i=0;i<n
阅读全文
摘要:括号匹配用栈是解决是最简单那的。 遇到左括号就入栈。遇到右括号就出栈,然后看是否匹配。这里再用一个map把括号数字化会更简单。 class Solution { public: bool isValid(string s) { map<char,int> m={ {'(',1},{')',-1},
阅读全文
摘要:写个数组当作栈用就行了。 bool isValid(char* s) { int t[100000]={0}; int top=0; int flag=1; for(int i=0;i<strlen(s);i++){ if(s[i]=='('){ t[top++]=1; }else if(s[i]=
阅读全文
摘要:用一个vector来记录每个位置打印打印情况, 结果:
阅读全文
摘要:#include<stdio.h> int main(){ char s[101]; while(scanf("%s",s)!=EOF){ printf("%s\n",s); char tem[101]; int a[101]={0}; int top=0; int i=0; for(;s[i]!=
阅读全文
摘要:、 这是目前阶段做的最难最吃力的题目。调试了一遍又一遍去看逻辑上出现的各种问题。。。 #include<iostream> #include<string> #include<stack> #include<map> using namespace std; int main(){ map<char
阅读全文
摘要:递归一下。 #include<iostream> using namespace std; long compute(int n){ if(n==1) return 1; return n* compute(n-1); } int main(){ int n; while( cin >> n){ c
阅读全文
摘要:递归C和C++一样,就写个C++了。 #include<iostream> using namespace std; void move(int n,char a,char b,char c){ if(n<=0) return; move(n-1,a,c,b); cout << n << ":"<<
阅读全文
摘要:#include<iostream> using namespace std; int compute(int n){ if(n==1) return 1; if(n==0) return 0; return compute(n-1)+ compute(n-2); } int main(){ int
阅读全文
摘要:递归判断当前节点和n的关系就好了。如果小于等于n那就是存在。 #include<iostream> using namespace std; int count(int i,int n){ if(i>n) return 0; return count(2*i,n)+ count(2*i+1,n)+1
阅读全文
摘要:思路是先构造出树,然后在后序遍历整个树。 #include<iostream> #include<string> using namespace std; struct Tnode{ char data; struct Tnode* left; struct Tnode* right; }; typ
阅读全文
摘要:这个题目思路其实就是先序遍历的变形。 相当于沿着先序遍历的顺序跟着构建二叉树就行。然后中序遍历这个树。 #include<iostream> #include<string> using namespace std; struct tnode{ char data; struct tnode* le
阅读全文
摘要:考二叉搜索树的插入。 #include<iostream> using namespace std; struct node{ int data; struct node* left; struct node* right; }; typedef struct node tree; int main
阅读全文
摘要:先把BST建立起,然后递归遍历判断树就好了。 #include<iostream> #include<string> using namespace std; struct node{ char data; struct node* left; struct node* right; }; type
阅读全文
摘要:这题难点就是什么是复数的模了吧。 然后C++写个优先队列(大根堆)+操作符重载就行了。 #include<iostream> #include<string> #include<queue> #include<math.h> using namespace std; struct node{ int
阅读全文
摘要:C的思路就是,先把用数据录入,然后按要求选出最大(用选择排序是最简单的),最后输出。 #include<stdio.h> #include<math.h> struct node{ int a; int b; long sum; }; typedef struct node num; int cha
阅读全文
摘要:用(优先队列)小根堆,先构建哈夫曼树,然后在递归遍历输出WPL。 #include<iostream> #include<queue> using namespace std; struct node{ int data; struct node* left; struct node* right;
阅读全文
摘要:用map做查找就行了。 #include<iostream> #include<string> #include<map> using namespace std; struct node{ string name; string x; int age; }; typedef struct node
阅读全文
摘要:简单的结构体查找。要注意C语言中文字符占多个字节,输入输出中文要用%s。 #include<stdio.h> #include<string.h> struct node{ char n[1001]; char name[200]; char x[4]; int age; }; typedef st
阅读全文
摘要:构建一个map,还是查找问题。 麻烦点就是要 分解输入的过程 #include<iostream> #include<string> #include<map> using namespace std; int main(){ string a,b; map<string,string> m; wh
阅读全文
摘要:#include<stdio.h> #include<string.h> struct node{ char a[100]; char b[100]; }; typedef struct node dir; dir s[100000]; int main(){ char A[200]; int to
阅读全文
摘要:其实可以看作一个图,源点是5,5的邻边分别是6,4,10(三种走法),利用BFS,最先遍历17为最少时间。 #include<iostream> #include<queue> #include<vector> using namespace std; struct node{ int data;
阅读全文
摘要:这题就是找N的倍数m,M要求是由1和0组成且非0。 可以用图来看,从1出发临边是1和0,然后广度遍历,第一个能能整除N的数输出就行。 #include<iostream> #include<queue> using namespace std; int main(){ int n=-1; while
阅读全文
摘要:题目看半天看不懂。 題目把我恶心坏了。看网上说按字典顺序输出,到底是什么意思半天没搞懂。 #include<iostream> #include<string> using namespace std; int d[8][2]={ {-1,-2},{1,-2},{-2,-1},{2,-1},{-2,
阅读全文
摘要:最近实在是太偷懒了,再这样下去复试直接寄。要努努力了。 简单一点可以用并查集,不嫌麻烦DFS/BFS都可以判断是否连通。 #include<stdio.h> void swap(int* x,int* y){ if(*x >*y){ int t=*x; *x=*y; *y=t; } } int fa
阅读全文
摘要:求图的最小生成树。克鲁斯卡尔算法来解决。就是选择n-1条最小边且无回路。 回路判断用并查集就行。 即要加入的边(两个节点)具有相同的父节点说明如果这两个节点本来就存在路径,再加入一条边就会产生回路,舍去。 #include<iostream> #include<algorithm> using na
阅读全文
摘要:考迪杰斯特拉算法。 #include<stdio.h> struct node{ int n1; int n2; int weight; }; typedef struct node edge; edge e[1000]; void init_dist(int dist[],int n){ for(
阅读全文
摘要:#include<iostream> using namespace std; int main(){ int n=0; int f[90]; f[1]=1; f[2]=2; while(cin >> n){ for(int i=3;i<=n;i++){ f[i]=f[i-1]+f[i-2]; }
阅读全文
摘要:题目例子给的很好,还有不要遗漏全是负数的情况。 #include<stdio.h> #include<math.h> int main(){ long long n=0; while(scanf("%ld",&n)!=EOF){ long long sum=0; long long max=0; i
阅读全文
摘要:这个解决问题的思路使用动态规划,即用已知状态去得到未知状态。 思路逻辑是这样 sum[i]记录以A[i]为末上升子序列的和的最大值 然后从j 从 0-i-1 遍历 如果A[j]<A[i] 那么 sum[i]=sum[j]+A[i]; 然后找出sum[i]中的的最大值,就是以A[i]为末上升子序列的和
阅读全文
摘要:建议直接网上看思路.... #include<stdio.h> int max(int i,int j){ if(i>j) return i; return j; } int maxlength[1001][1001]; int main(){ int n,m; while(scanf("%d %d
阅读全文
摘要:原本想排下序的,但要求返回下标,又感觉要添加其他东西了,太麻烦了。暴力找就好了。
阅读全文
摘要:刷力扣还有点不太习惯,主要是C++只学了皮毛。 看了官方活用map就是好啊。 把字母都排好序 然后判断就好了。 map<string,vector<string>> m; for(int i=0;i<strs.size();i++){ string tem=strs[i]; sort(tem.beg
阅读全文
摘要:o(n)现在水平不够。 采用先快排序,再找。O(nlogn),注意每次划分枢纽选择中间节点(中间节点和首节点互换) int divide(int* nums,int head,int tail){ int x=nums[(head+tail)/2]; nums[(head+tail)/2]=nums
阅读全文
摘要:就是让你判断输入受限的双端队列的输出的正确性。 其实就是模拟双端队列出队的过程,要不左边出队,要不右边出队,而入队已经一定了。 用一个数组模拟输入受限的双端队列就行了。 但是写这题可太难受了,写了我大概2个半小时,各种各种小错误,没考虑周全的地方。 #include<iostream> using
阅读全文
摘要:类似像一个筛选过程,如果非0就加入数组,不非0就不加。 void moveZeroes(int* nums, int numsSize) { int x=0,y=0; while(x<numsSize){ if(y>=numsSize) break; if(nums[y]!=0){ nums[x]=
阅读全文
摘要:原本想o(n2)遍历的,结果超时了,果然没这么简单就解决。 class Solution { public: int s(vector<int> height,int i,int j){ int s1=min(height[i],height[j])*(j-i); return s1; } int
阅读全文
摘要:先排序,再暴力找就好了。如果当前元素大于0或者前两个元素和大于0就不用找了。然后结果超时了。 然后借鉴了双指针的解法,发现双指针其实就是把单向循环优化成双向循环。 class Solution { public: vector<vector<int>> threeSum(vector<int>& n
阅读全文
摘要:因为还是双指针的题目。 我想到的短板效应,看两头,能接住的水也就是取决于最短的一方。 每次看两头后,在里面找比最短还短的地方,那个就是有水的地方。找到水后在把它填平,防止重复找到。然后两头往中间靠就行了。 int min(int i,int j){ if(i>j) return j; return
阅读全文
摘要:思路就是从头开始找,然后每次在从重复节点的后一个找。 class Solution { public: int lengthOfLongestSubstring(string s) { int i=0,j=0,nowmax=1; int max=1; if(s.size()==0||s.size()
阅读全文
摘要:今天出成绩了,感觉徘徊在被刷的边缘,要好好努力了。 这题我想法试建立hash映射成有序的数字,只要字符串个数相同,并且映射和相同那么就是异位串。 后来这个想法是错的。以为假设已经已知一个和,和组成这个和的个数,但这个子数并不唯一,比如10=1+2+7。10=2+3+5。 这样就会误判。就算能找到唯一
阅读全文
摘要:现在开始刷代码随想录里的题了。 int search(int* nums, int numsSize, int target) { int head=0,tail=numsSize-1; while(head<=tail){ int mid=(head+tail)/2; if(nums[mid]<t
阅读全文
摘要:原本我想用头尾交换的双指针的,但是又要判断头尾是否相等,感觉不干净的感觉。 就换成了类似筛选的前后双指针。一个是指向要放的位置,一个指向查看的位置。 int removeElement(int* nums, int numsSize, int val) { int i=0,j=0; while(j<
阅读全文
摘要:int searchInsert(int* nums, int numsSize, int target) { int head=0,tail=numsSize-1; while(head<=tail){ int mid=(head+tail)/2; if(nums[mid]<target){ he
阅读全文
摘要:/** * Note: The returned array must be malloced, assume caller calls free(). */ int* searchRange(int* nums, int numsSize, int target, int* returnSize)
阅读全文
摘要:int mySqrt(int x) { return sqrt(x); }
阅读全文
摘要:bool isPerfectSquare(int num) { if(num==1) return true; if(num==2) return false; long head=2,tail=num-1; while(head<=tail){ long mid=(head+tail)/2; lo
阅读全文
摘要:int removeDuplicates(int* nums, int numsSize) { if(numsSize==0||numsSize==1) return numsSize; int i=0,j=0; int pre=-999,n=0; while(j<numsSize){ if(num
阅读全文
摘要:这题学到了很多。 malloc后要初始化。 申请字符串要N+1个单位 字符串以0结尾等等 char* final(char* s,int n){ char* tem=(char*)malloc(sizeof(char)*(n+1)); for(int i=0;i<=n;i++) { tem[i] =
阅读全文
摘要:学习了下用qsort解决。 /** * Note: The returned array must be malloced, assume caller calls free(). */ int cmp(const void* a,const void* b){ return *(int*)a-*(
阅读全文
摘要:滑动窗口的妙用!! int minSubArrayLen(int target, int* nums, int numsSize) { int sum=nums[0];//区间head到tail的和 int head=0,tail=0; int minn=numsSize; int tag=0; i
阅读全文
摘要:int totalFruit(int* fruits, int fruitsSize) { if(fruitsSize<=2) return fruitsSize; int a[2]={-1,-1};//蓝子空 int max=0,n=1; int head=0, tail=0;//从head摘到t
阅读全文
摘要:int hash(char c){ return c-'A'+1; } bool judge_Same(int a[],int b[]){ for(int i=0;i<200;i++){ if(b[i]!=0 && b[i]>a[i]) return false; } return true ; }
阅读全文
摘要:/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array a
阅读全文
摘要:写了个递归 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* delect(struct ListNode
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode*
阅读全文
摘要:没必要纠结太麻烦的方法。 简单的就两种,一个就是新拿出来空间来装。 要不然就是直接前后数据换一下就行了。 然后需要考虑表空的情况。 结果: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeNthFromEnd(struct List
阅读全文
摘要:利用链表的特性,如果相交的话,后面就不可能岔开! 你可以想象把他们有同一个尾巴,然后从尾巴往前看。 所以只要知道两个链表的长度,就可以在同一起跑线上一起比较了。 /** * Definition for singly-linked list. * struct ListNode { * int va
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode *detectCycle(struct ListNode
阅读全文
摘要:int hash(char c){ return c-'a'; } bool isAnagram(char* s, char* t) { int a[26]={0}; int b[26]={0}; int i=0; while(s[i]!=0){ a[hash(s[i++])]++; } i=0;
阅读全文
摘要:\ int hash(char c){ return c-'a'; } bool canConstruct(char* ransomNote, char* magazine) { if(!ransomNote) return true; if(!magazine) return false; int
阅读全文
摘要:/** * Note: The returned array must be malloced, assume caller calls free(). */ int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size
阅读全文
摘要:/** * Note: The returned array must be malloced, assume caller calls free(). */ int min(int i,int j){ if(i<j) return i; return j; } int* intersect(int
阅读全文
摘要:没有意识到这是一个环。 原理就是,其区间是有限的(int型变量存在最大值pow(2,31)-1),而循环却是无限的,那么一定会发生重复的情况。 如果不重复,那么和区间有限矛盾。 int c_sum(int n){ int sum=0; while(n!=0){ int t=n%10; n/=10;
阅读全文
摘要:自己写了一个hash表。 原来学过的数据结构关于hash的那章还是有实际用的,不是书架子。 typedef struct node{ int sum; int count; struct node* repeatnext; }hash; void init_hashtable(hash h[]){
阅读全文
摘要:void reverseString(char* s, int sSize) { int head=0,tail=sSize-1; while(head<=tail){ char t=s[head]; s[head]=s[tail]; s[tail]=t; head++; tail--; } } 1
阅读全文
摘要:void reverse_string(char* s,int head,int tail){ while(head<=tail){ char t=s[head]; s[head]=s[tail]; s[tail]=t; head++; tail--; } } char* reverseStr(ch
阅读全文
摘要:#include<stdio.h> int main(){ char c[10000]; scanf("%s",c); int i=0; while(c[i]!=0){ int t=c[i]-'0'; if(0<=t&&t<=9){ printf("number"); }else{ printf("
阅读全文
摘要:现在解决一个中等难度的题得要30min,争取在复试的时候提升到只有15min!!!! void revesestring(char* s,int head,int tail){ while(head<=tail){ char temp=s[head]; s[head]=s[tail]; s[tail
阅读全文