zoukankan      html  css  js  c++  java
  • 《一道笔试题》找出最连续数字的最大长度

    如下,一串递增数字数组,如12244466666666,找出其中相同数字最长的长度。补全if中的内容。

    int find( int x[], int n)
    {
        int length = 0;
        for (i=1;i<n;i++)
        {
            if (/* 只能在这里填写代码哦 */ )
            {
                length++;
            }
        }
        return length;
    }

    小弟给出一个递归版:

    int find(int x[], int n)
    {
        int length = 0;
        int i;
        for (i=1;i<n;i++)
        {
            if (x[i] == x[i-1] ? 1 :(length = (++length > (i = find(x + length,n - length)) ? length : i))?((i = n - 1) ? 0 : 0):0)
            {
                length++;
            }
        }
        return length;
    }

    一网友的非递归版本:

    int find( int x[], int n)
    {
        int length = 0;
        for (int i=1;i<n;i++)
        {
            if (((i == 1)&&(x[0] == x[1]) ? length++ : 0) || (x[i] == x[i - 1] && (x[i - length] == x[i])) || (i == 1))
            {
                length++;
            }
        }
        return length;
    }

    其实该网友的 x[i] == x[i - 1] 完全可以不要,可以优化为:

    int find( int x[], int n)
    {
        int length = 0;
        for (int i=1;i<n;i++)
        {
            if (((i == 1)&&(x[0] == x[1]) ? length++ : 0) || (x[i - length] == x[i]) || (i == 1))
            {
                length++;
            }
        }
        return length;
    }
  • 相关阅读:
    关于返回上一页功能
    Mybatis Update statement Date null
    SQLite reset password
    Bootstrap Validator使用特性,动态(Dynamic)添加的input的验证问题
    Eclipse使用Maven2的一次环境清理记录
    Server Tomcat v7.0 Server at localhost failed to start
    PowerShell一例
    Server Tomcat v7.0 Server at libra failed to start
    商标注册英语
    A glance for agile method
  • 原文地址:https://www.cnblogs.com/caozengling/p/7007251.html
Copyright © 2011-2022 走看看