笔试:成都精灵云
- 今天第一次去体验了下笔试什么感觉,手写代码和平时在电脑上敲键盘就是不一样。
- 笔试的题目倒是简单,比较好理解,但要仔细考虑一些细节。
- 我面的是c++开发岗,就3道题目,每道题分别让写出算法思想和代码例程。
- 第一题是字符串反转类型,第二题是二分查找类型,第三题(选做)是关于LRU算法的,下面主要记录下第一题的解答。
题目:反转字符串
示例:
1.the sky is blue. 变成 blue is sky the.。
2.the sky is blue. the sky is white. 变成 blue is sky the. white is sky the.。
#include#include void myReverse(char* s, int start, int end){ while(start < end){ char t = s[start]; s[start] = s[end]; s[end] = t; ++start; --end; } } void mainReverse(char* s){ if(s == nullptr) return; int n = strlen(s); int left = 0; int right = 0; while(right < n){// 这儿是<号,<=就越界了 if(s[right] == '.'){ myReverse(s, left, right - 1); int i = left; int j = left; while(j <= right){// 注意必须是<=, 因为需要遇到'.'号; if(s[j] == ' ' || s[j] == '.'){ myReverse(s, i, j - 1); if(s[j] == ' ') j += 1;// 因为空格后就是新的单词了 if(s[j] == '.') j += 2;// '.'号后面还有一个空格,需要跳过 i = j; }else{ ++j; } } right += 2; // '.'号后面还有一个空格,需要跳过 left = right; }else{ right += 1; } } } int main() { char s[] = "As saying goes. However. In my view. On the other hand."; printf(" input = %sn", s); mainReverse(s); printf("output = %sn", s); }
最后自己模拟一遍就清楚了,要多想想测试例子,下图是运行结果:
input = As saying goes. However. In my view. On the other hand.
output = goes saying As. However. view my In. hand other the On.
- 上面代码是笔试完后想起来又写了一下,笔试的时候没写出来。。。
- 平常都是在leetcode上刷题,一般处理字符串都是string类,突然遇到char*没反应过来~
- 再接再厉,多多刷题总结。



