第八周 4.18 --- 4.24
在看面试金典这本书,把书上的题目都写一写
恩,好好加油呀>.<
8.1 数组与字符串
1.将n个字符串拼接在一起
1 //将n个字符串拼接在一起 2 //2016.4.18 3 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 10 const int maxn = 1005; 11 char s[105][maxn]; 12 int n; 13 string ans; 14 15 void solve(){ 16 ans.clear(); 17 for(int i = 1;i <= n;i++) ans += s[i]; 18 printf("%s\n",ans.c_str()); 19 } 20 21 int main(){ 22 while(scanf("%d",&n) != EOF){ 23 for(int i = 1;i <= n;i++) scanf("%s",s[i]); 24 solve(); 25 26 } 27 return 0; 28 }
View Code
2.实现一个字符串的翻转
其实我想的是,输入之后直接逆序输出可以么
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 string line; 9 10 void solve(){ 11 vector<char> ans; 12 int len = line.length(); 13 for(int i = 0;i < len;i++) ans.push_back(line[len-i-1]); 14 for(int i = 0;i < len;i++) printf("%c",ans[i]); 15 16 } 17 18 int main(){ 19 getline(cin,line); 20 solve(); 21 return 0; 22 }
View Code
书上给的是用指针来实现的
3.一个字符串可不可以通过重排列得到另一个字符串
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn = 1e5+5; 8 char s[maxn],t[maxn]; 9 int a[maxn],b[maxn]; 10 11 void solve(){ 12 int lens = strlen(s); 13 int lent = strlen(t); 14 if(lens != lent){ 15 puts("No"); 16 return; 17 } 18 for(int i = 0;i < lens;i++) a[s[i]]++; 19 for(int i = 0;i < lent;i++) b[t[i]]++; 20 for(int i = 0;i < 256;i++){ 21 if(a[i] != b[i]){ 22 puts("No"); 23 return; 24 } 25 } 26 puts("Yes"); 27 } 28 29 int main(){ 30 while(scanf("%s",s) != EOF){ 31 scanf("%s",t); 32 solve(); 33 } 34 return 0; 35 }
View Code
4.将字符串中的空格替换成 %20
1 //将字符串中的空格替换成为%20 2 //2016.4.18 3 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 #include<vector> 9 using namespace std; 10 11 string str; 12 13 void solve(){ 14 int len = str.length(); 15 vector<char> ans; 16 for(int i = 0;i < len;i++){ 17 if(str[i] != ' ') ans.push_back(str[i]); 18 else{ 19 ans.push_back('%'); 20 ans.push_back('2'); 21 ans.push_back('0'); 22 } 23 } 24 for(int i = 0;i < ans.size();i++) printf("%c",ans[i]); 25 printf("\n"); 26 } 27 28 int main(){ 29 getline(cin,str); 30 solve(); 31 }
View Code
5.压缩一个字符串
1 //压缩一个字符串 2 //2016.4.18 3 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 10 const int maxn = 1e5+5; 11 char s[maxn],t[maxn]; 12 13 string solve(){ 14 int len = strlen(s); 15 string tmp; 16 for(int i = 0;i < len;){ 17 int j = i; 18 while(j < len && s[j] == s[i]) j++; 19 tmp += s[i]; 20 int x = j-i; 21 tmp += x+'0'; 22 i = j; 23 } 24 int lenn = tmp.length(); 25 if(lenn == len) tmp = s; 26 return tmp; 27 } 28 29 30 31 int main(){ 32 while(scanf("%s",s) != EOF){ 33 string ans; 34 ans = solve(); 35 printf("ans = %s\n",ans.c_str()); 36 } 37 return 0; 38 }
View Code
6.将一个矩形旋转 90度
我想的是直接交换横纵坐标,,书上写的有点麻烦
不知道是不是自己理解错了
1 //将矩阵旋转90度 2 //2016.4.18 3 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 9 int g[505][505],a[505][505]; 10 int n,m; 11 12 int main(){ 13 while(scanf("%d %d",&n,&m) != EOF){ 14 for(int i = 1;i <= n;i++){ 15 for(int j = 1;j <= m;j++){ 16 scanf("%d",&g[i][j]); 17 a[j][n-i] = g[i][j]; 18 } 19 } 20 for(int i = 1;i <= m;i++){ 21 for(int j = 1;j <= n;j++) printf("%d ",a[i][j]); 22 printf("\n"); 23 } 24 } 25 return 0; 26 }
View Code
7.清空 0 所在的行和列
1 //清空 0 所在的行和列 2 //2016.4.18 3 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 10 int g[505][505]; 11 int n,m; 12 int hang[505],lie[505]; 13 14 void solve(){ 15 memset(hang,0,sizeof(hang)); 16 memset(lie,0,sizeof(lie)); 17 for(int i = 1;i <= n;i++){ 18 for(int j = 1;j <= m;j++){ 19 if(g[i][j] == 0){ 20 hang[i] = 1; 21 lie[j] = 1; 22 } 23 } 24 } 25 for(int i = 1;i <= n;i++){ 26 for(int j = 1;j <= m;j++){ 27 if(hang[i] || lie[j]) g[i][j] = 0; 28 } 29 } 30 for(int i = 1;i <= n;i++){ 31 for(int j = 1;j <= m;j++) printf("%d ",g[i][j]); 32 printf("\n"); 33 } 34 } 35 36 int main(){ 37 while(scanf("%d %d",&n,&m) != EOF){ 38 for(int i = 1;i <= n;i++){ 39 for(int j = 1;j <= m;j++){ 40 scanf("%d",&g[i][j]); 41 } 42 } 43 solve(); 44 } 45 return 0; 46 }
View Code
8.给出 字符串 s,t 和一个函数(用来判断一个字符串是不是另一个字符串子串 的函数),这个函数只能用一次,判断t 能否通过旋转 得到 s
旋转的意思是 ,asdf ,可以旋转成 dfas
即为 xy 旋转为 yx 一定是 xyxy的子串
所以判断strstr(s+s,t) 是不是 NULL就可以了
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn = 5005; 8 char s[maxn],t[maxn]; 9 10 int main(){ 11 while(scanf("%s",s) != EOF){ 12 scanf("%s",t); 13 char *p; 14 int len = strlen(s); 15 for(int i = len;i < 2*len;i++) s[i] = s[i-len]; 16 s[2*len] = '\0'; 17 p = strstr(s,t); 18 if(p != NULL) puts("Yes"); 19 else puts("No"); 20 21 } 22 return 0; 23 }
View Code
8.2 链表
1.删除链表中的重复节点
1 //删除链表中的重复节点 2 //2016.4.18 3 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 10 int vis[1005],n,b[1005]; 11 12 struct node{ 13 int num; 14 struct node *next; 15 }; 16 17 struct node *create(int n){ 18 struct node *p,*q,*head; 19 head = (struct node*)malloc(sizeof(struct node)); 20 q = head; 21 q->next = NULL; 22 for(int i = 0;i < n;i++){ 23 p = (struct node*)malloc(sizeof(struct node)); 24 p->next = NULL; 25 cin >> p->num; 26 q->next = p; 27 q = p; 28 } 29 return head; 30 } 31 32 void print(struct node *head){ 33 struct node *p,*q; 34 p = head->next; 35 while(p != NULL){ 36 cout << p->num << "\n"; 37 p = p->next; 38 } 39 } 40 41 void del(struct node *head,int k){ 42 struct node *p,*q; 43 if(k == 1){//删除第一个节点 44 q = head->next; 45 head->next = q->next; 46 free(q); 47 } 48 else{ 49 int cnt = 0; 50 q = head; 51 struct node *pre; 52 for(;;){ 53 pre = q; 54 q = q->next; 55 cnt++; 56 if(cnt == k) break; 57 } 58 if(k < n){//删除中间的节点 59 pre->next = q->next; 60 free(q); 61 } 62 else{//删除尾部的节点 63 pre->next = NULL; 64 free(q); 65 } 66 } 67 } 68 69 void solve(struct node *head){ 70 struct node *p,*q; 71 p = head->next; 72 int cnt = 0; 73 memset(b,0,sizeof(b)); 74 memset(vis,0,sizeof(vis)); 75 while(p != NULL){ 76 vis[p->num]++; 77 p = p->next; 78 } 79 p = head->next; 80 while(p != NULL){ 81 cnt++; 82 if(vis[p->num] > 1){ 83 vis[p->num]--; 84 b[cnt] = 1; 85 } 86 p = p->next; 87 } 88 printf("---begin---\n"); 89 print(head); 90 } 91 92 93 int main(){ 94 struct node *p; 95 n = 5; 96 p = create(5); 97 print(p); 98 99 solve(p); 100 for(int i = n;i >= 1;i--){ 101 printf("b[%d] = %d\n",i,b[i]); 102 if(b[i]) del(p,i); 103 } 104 //del(p,4); 105 print(p); 106 107 return 0; 108 }
View Code
2.找出链表中的倒数第 k 个节点
直接遍历
3.删除单向链表中的某个节点
感觉就是删除,,,书上的做法不是太理解
4.以给定值 x 将给定的链表划分成两个部分,再合并起来
不会写,于是去搜了下
发现是 leetcode 上的题目,,扒了一份代码,还改了半天
因为抄的那份代码是没有那个空 的head 指针的TAT
1 //以给定值 x 将链表分成两个部分,再将这两个部分连接起来 2 //2016.4.19 3 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 10 struct ListNode{ 11 int val; 12 ListNode *next; 13 ListNode(int x):val(x),next(NULL){} 14 }; 15 16 class Solution{ 17 public: 18 struct ListNode* partition(struct ListNode* head,int x){ 19 if(head == NULL || head->next == NULL) return head; 20 ListNode *left = new ListNode(-1); 21 ListNode *right = new ListNode(-1); 22 ListNode *ltail = left,*rtail = right; 23 ListNode *pre = head; 24 while(pre){ 25 if(pre->val < x){ 26 ltail->next = pre; 27 ltail = ltail->next; 28 } 29 else{ 30 rtail->next = pre; 31 rtail = rtail->next; 32 } 33 pre = pre->next; 34 } 35 if(right->next){ 36 ltail->next = right->next; 37 rtail->next = NULL; 38 } 39 left = left->next; 40 return left; 41 } 42 }; 43 44 struct ListNode* create(int n){ 45 struct ListNode *p,*q,*head; 46 head = (struct ListNode*)malloc(sizeof(struct ListNode)); 47 cin >> head->val; 48 q = head; 49 q->next = NULL; 50 for(int i = 0;i < n-1;i++){ 51 p = (struct ListNode*)malloc(sizeof(struct ListNode)); 52 p->next = NULL; 53 cin >> p->val; 54 q->next = p; 55 q = p; 56 } 57 return head; 58 } 59 60 void print(struct ListNode* head){ 61 struct ListNode *p; 62 p = head; 63 while(p != NULL){ 64 cout << p->val <<"\n"; 65 p = p->next; 66 } 67 } 68 69 int main(){ 70 Solution z; 71 struct ListNode *ans,*tmp; 72 tmp = create(6); 73 print(tmp); 74 ans = z.partition(tmp,2); 75 printf("---ans---\n"); 76 print(ans); 77 78 }
View Code
5.将两个链表里面保存的数加起来,将结果存在链表里面
1 //链表实现两个数相加 2 //2016.4.19 3 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 #include<vector> 9 using namespace std; 10 11 struct node{ 12 int num; 13 struct node *next; 14 }; 15 16 vector<int> cl,cr,cc; 17 18 struct node* create(int n,vector<int>& v){ 19 struct node *head,*p,*q; 20 head = (struct node*)malloc(sizeof(struct node)); 21 q = head; 22 q->next = NULL; 23 for(int i = 0;i < n;i++){ 24 p = (struct node*)malloc(sizeof(struct node)); 25 cin >> p->num; 26 v.push_back(p->num); 27 p->next = NULL; 28 q->next = p; 29 q = p; 30 } 31 return head; 32 } 33 34 void print(struct node *head){ 35 struct node *p; 36 p = head->next; 37 while(p != NULL){ 38 cout << p->num << "\n"; 39 p = p->next; 40 } 41 } 42 43 void solve(vector<int>& v){ 44 int lens = cl.size(),lent = cr.size(); 45 reverse(cl.begin(),cl.end()); 46 reverse(cr.begin(),cr.end()); 47 for(int i = 0,g = 0;;i++){ 48 if(g == 0 && i >= lens && i >= lent) break; 49 int x = g; 50 if(i < lens) x += cl[i]; 51 if(i < lent) x += cr[i]; 52 v.push_back(x%10); 53 g = x/10; 54 } 55 reverse(v.begin(),v.end()); 56 } 57 58 struct node *make(){ 59 struct node *head,*p,*q; 60 head = (struct node*)malloc(sizeof(struct node)); 61 q = head; 62 q->next = NULL; 63 int sz = cc.size(); 64 for(int i = 0;i < sz;i++){ 65 p = (struct node*)malloc(sizeof(struct node)); 66 p->next = NULL; 67 p->num = cc[i]; 68 q->next = p; 69 q = p; 70 } 71 return head; 72 } 73 74 int main(){ 75 struct node *l,*r,*ans; 76 l = create(5,cl); 77 printf("---l---\n"); 78 print(l); 79 for(int i = 0;i < cl.size();i++) printf("%d",cl[i]); 80 printf("\n"); 81 82 r = create(5,cr); 83 printf("---r---\n"); 84 print(r); 85 for(int i = 0;i < cr.size();i++) printf("%d",cr[i]); 86 printf("\n"); 87 88 solve(cc); 89 for(int i = 0;i < cc.size();i++) printf("%d",cc[i]); 90 printf("\n"); 91 92 ans = make(); 93 printf("---ans ---\n"); 94 print(ans); 95 printf("\n"); 96 }
View Code
4.19
什么都没有干的样子
4.20
怎么说也是人生中第一次面试,记录下吧...
短信通知的是下午3点到,怕找不到路,于是提前到了一个小时.....
其实就觉得不可能过的....
然后就开始等,前面两个男生在讨论KMP,于是我也掏出爪机瞅了两眼
然后就开始面了
开始问我两个指针....不会
然后.....果然问了KMP,给了一个字符串,让算一下next 数组
还问了个,给出 n 个数,求最小的k个......
到这里之后的问题就都不会了...
问了个linux的东西,不会....
又问了 些计算机网络.....
面试官拿着我那张简历翻了好几遍,,感觉又没有什么项目可以问.....
于是就GG了....
意料之中的GG
青蛙说,就当出去散步了。
还是自己太弱了。
4.21
......
4.22
leetcode 141 Linked List Cycle
判断一个链表是否有环
一个慢指针每次走1步,一个快指针每次走 2步
如果有任何一个指针走到NULL,都说明没有环,如果两个指针相遇了,就说明有环
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 struct ListNode{ 8 int val; 9 ListNode *next; 10 ListNode(int x) : val(x),next(NULL){} 11 }; 12 13 class Solution{ 14 public: 15 bool hasCycle(ListNode *head){ 16 if(head == NULL) return false; 17 ListNode *slow = head; 18 ListNode *fast = head; 19 while(true){ 20 if(slow->next != NULL){ 21 slow = slow->next; 22 } 23 else return false; 24 if(fast->next != NULL && fast->next->next != NULL){ 25 fast = fast->next->next; 26 } 27 else return false; 28 29 if(slow == fast) return true; 30 } 31 return false; 32 } 33 };
View Code
leetcode 142 Linked List Cycle II
判断一个链表是不是有环,有环的话输出环的起点
从上一题快慢指针相遇的点开始,再放一个慢指针从 head 开始,这两个慢指针相遇的地方就是环开始的地方
1 class Solution{ 2 public: 3 ListNode *detectCycle(ListNode *head){ 4 ListNode *slow,*fast; 5 slow = head;fast = head; 6 int ok = 0; 7 while(slow && fast && fast->next){ 8 slow = slow->next; 9 fast = fast->next->next; 10 if(slow == fast){ 11 ok = 1; 12 break; 13 } 14 } 15 if(ok == 0) return NULL; 16 fast = head; 17 while(slow != fast){ 18 slow = slow->next; 19 fast = fast->next; 20 } 21 return slow; 22 } 23 };
View Code
leetcode 234 Palindrome Linked List
判断链表是不是回文的
1 class Solution{ 2 public: 3 bool isPalindrome(ListNode* head){ 4 ListNode *p; 5 vector<int> c; 6 p = head; 7 while(p){ 8 c.push_back(p->val); 9 p = p->next; 10 } 11 int sz = c.size()/2; 12 int l = 0,r = c.size()-1; 13 for(int i = 0;i < sz;i++){ 14 if(c[l] != c[r]){ 15 return false; 16 } 17 l++;r--; 18 } 19 return true; 20 } 21 };
View Code
4.23
早上起床补昨晚的bc
div2,,感觉还是好不容易能够做3道题目(是题目太简单....)
最后1003 fst 了,TLE 了,因为判断个数的时候用的 set ,是 log 级别的吧,如果直接维护一个cnt 就是O(1)的了
诶...
hdu 5672 String
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<vector> 6 #include<set> 7 using namespace std; 8 9 typedef long long LL; 10 const int maxn = 1e6+5; 11 int a[maxn],n,m,k,num[maxn]; 12 char s[maxn]; 13 14 void solve(){ 15 n = strlen(s+1); 16 int l = 1,r = 1; 17 LL ans = 0LL; 18 int cnt = 0; 19 memset(num,0,sizeof(num)); 20 while(l <= n){ 21 while(r <= n && cnt < k){ 22 if(!num[s[r]]) cnt++; 23 num[s[r]]++; 24 r++; 25 26 //printf("=== l = %d r = %d\n",l,r); 27 } 28 //printf("---l = %d r = %d z.size() = %d zz.size() = %d\n",l,r,z.size(),zz.size()); 29 if(cnt == k){ 30 LL tmp = 1LL+ 1LL*(n-r+1); 31 ans += tmp; 32 //printf("l = %d r = %d tmp = %I64d\n",l,r,tmp); 33 num[s[l]]--; 34 if(num[s[l]] == 0) cnt--; 35 36 } 37 l++; 38 } 39 printf("%I64d\n",ans); 40 } 41 42 int main(){ 43 int T; 44 scanf("%d",&T); 45 while(T--){ 46 scanf("%s",s+1); 47 scanf("%d",&k); 48 solve(); 49 50 } 51 return 0; 52 }
View Code
转载于:https://www.cnblogs.com/wuyuewoniu/p/5403821.html
第八周 4.18 --- 4.24相关推荐
- 读书笔记——第八周学习笔记
第八周学习笔记 一.数据行 1. 利用逗号分隔将数据导入数据库中 将excel保存为后缀为.csv的文件 在数据库中插入表时跟之前有一点不同 BULK INSERT tb_Course FROM 'C ...
- 20155305乔磊2016-2017-2《Java程序设计》第八周学习总结
20155305乔磊2016-2017-2<Java程序设计>第八周学习总结 教材学习内容总结 通用API 日志API 1.java.util.logging包提供了日志功能相关类与接口, ...
- XJTU_ 西安交通大学2020大学计算机作业-第八周
XJTU_ 西安交通大学2020大学计算机作业-第八周 XJTU_ 西安交通大学2020大学计算机作业-第八周 注:所有题所有用例均已通过. 文章没上传到github,反正没人点星星:https:// ...
- WHUT第八周训练整理
WHUT第八周训练整理 写在前面的话:我的能力也有限,错误是在所难免的!因此如发现错误还请指出一同学习! 索引 (难度由题目自身难度与本周做题情况进行分类,仅供新生参考!) 零.并查集与最小生成树 一 ...
- 2019夏第八周学习编辑总结
这个作业属于哪个课程 C语言程序设计二 这个作业要求在哪里 2019春季学期第八周作业 我的课程目标 掌握常用字符串函数以及使用指针操作字符串的方法,掌握动态内存分配. 这个作业在哪个具体方面帮助我实 ...
- 20172311《程序设计与数据结构》第八周学习总结
20172311<程序设计与数据结构>第八周学习总结 教材学习内容总结 第十二章 优先队列与堆 堆(heap)就是具有两个附加属性的一颗二叉树 1.它是一颗完全树 2.对每一个节点,它小于 ...
- 2017-2018-20172309 《程序设计与数据结构》第八周学习总结
2017-2018-20172309 <程序设计与数据结构>第八周学习总结 一.教材学习内容总结 相信其它很多同学都是以小顶堆来介绍这一章内容,所以我将以大顶堆来介绍这章内容. 1.1 堆 ...
- 20172311 2017-2018-2 《程序设计与数据结构》第八周学习总结
20172311 2017-2018-2 <程序设计与数据结构>第八周学习总结 教材学习内容总结 本周对JAVA中的多态性进行了学习 多态性引用能够随时间变化指向不同类型的对象,是通过后绑 ...
- 20172327 2018-2019-1 《程序设计与数据结构》第八周学习总结
20172327 2018-2019-1 <程序设计与数据结构>第八周学习总结 教材学习内容总结 第十二章 优先队列与堆 堆 1.最小堆(minheap):对是一个完全二叉树,其中的每个结 ...
- 《程序设计与数据结构》第八周学习总结
学号 20172326 <程序设计与数据结构>第八周学习总结 教材学习内容总结 后绑定在程序执行时执行 多态性可由继承与接口实现 排序有选择法排序与插入法排序 搜索分为线性搜索与二分搜索 ...
最新文章
- 华硕WL-500W无线路由器使用感受
- ElasticSearch_查询过滤排序
- 如果觉得职业看不到头,趁早换工作吧
- MySql 事务+异常处理+异常抛出
- python中curve fit_在python中拟合多变量curve_fit
- 具有ReadWriteLock的Java并发
- html列表滑动字母索引,js实现做通讯录的索引滑动显示效果和滑动显示锚点效果...
- Spring Boot笔记-普通异常错误截取及构造错误页面
- thinkphp3.2笔记(1)目录,控制器及url模式,地址解析
- ResNet家族迎来新王者!一套模型改进多项视觉任务
- 通信接口主要的5种类型_5种常见的住宅建筑结构类型,你真的了解吗?
- 游戏开发之C++面向对象模型(C++类中成员变量和成员函数的存储原理及this指针)(C++基础)
- macOS Monterey中最新的「通用控制」是什么?苹果设备如何使用通用控制功能!
- 嵌入式仿真用Qt播放器和录像机
- 二、appium的原理
- 详解数字音频接口DAI
- Oracle中的dual表
- 智能停车场[简易版]
- Python实现BP神经网络ANN单隐层分类模型项目实战
- 改造开源刻录软件InfraRecorde
热门文章
- chrome 插件开发各种功能demo_Chrome扩展开发-编写一个浏览器插件
- linux shell cut -d ‘:‘ -f1,3
- c#垂直投影法_形象理解“梯度”与“法向量”的关系
- 基于SSM的酒水商城系统
- 基于Java的敬老院管理系统
- 2020-06-28
- javascript offsetLeft,Left,clientLeft 各种浏览器位置相关属性
- php curl_setopt 登录 获取数据
- Spring JdbcTemplate 调用 Oracle 存储过程 与 Oracle 驱动下载
- CentOS 7.2 卸载 Mysql 、Windowns 上卸载 Mysql