zoukankan      html  css  js  c++  java
  • [2013百度软件研发笔试题] 求字符串中连续出现同样字符的最大值

    题目完整描写叙述为:用递归的方式实现一个求字符串中连续出现同样字符的最大值。如aaabbcc,连续出现a的最大值为3,abbc,连续出现字符最大的值为2。


    下面是我想出来的方法:

    #include <iostream>
    using namespace std;
    
    #define MAX(a, b) (a) > (b) ?

    (a) : (b) int Get(char *s, int n, int m)  //字符指针, 当前最长串, max最长串 {     if(*(s+1) == '')         return MAX(n, m);     if(*s == *(s+1))         return Get(s+1, n+1, m);     return Get(s+1, 1, MAX(n, m)); } int main() {     printf("%d ", Get("abbc", 1, 1));     printf("%d ", Get("aaabbcc", 1, 1));     getchar();     return 0; }


    以及还有一种递归的方法:

    #include <iostream>
    using namespace std;
    
    int Get(char *s)
    {
        int ans = 0;
        char *t = s;
        if(*s == '')
            return ans;
        while(*s != '' && *t == *s)
            ans++, s++;
        t = s;
        int tmp = Get(t);
        return ans > tmp ? ans : tmp;
    }
    
    int main()
    {
        printf("%d
    ", Get("abbc"));
        printf("%d
    ", Get("aaabbcc"));
    
        getchar();
        return 0;
    }
    
    不知道为什么一定要是递归实现。。。

    可能是要考察写递归的能力吧。无所谓,反正笔试面试题都非常怪异就是了-.-

    假设你有其它更好的方法,请赐教!


  • 相关阅读:
    shell中的 echo命令
    shell中的运算符
    shell中的替换
    shell中的元字符
    springcloud-sleuth的使用
    springcloud-sleuth之zipkun运行和概念介绍
    springcloud-sleuth是什么
    消息总线(bus)和消息驱动(stream)的区别
    springcloud-stream之持久化
    springcloud-stream之消费者重复消费
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7113158.html
Copyright © 2011-2022 走看看