栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

记一道小笔试题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

记一道小笔试题

前言

笔试:成都精灵云

  • 今天第一次去体验了下笔试什么感觉,手写代码和平时在电脑上敲键盘就是不一样。
  • 笔试的题目倒是简单,比较好理解,但要仔细考虑一些细节。
  • 我面的是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*没反应过来~
  • 再接再厉,多多刷题总结。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号