第八周 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值