如何使用递归查找相同数字的序列? - 爪哇

发布于 2025-01-25 19:11:41 字数 844 浏览 1 评论 0原文

我有一个分配,例如,从用户那里获得了一个数字输入,例如:“ 57779227” 我需要返回最长的相同数字序列。在此示例中,最长的序列是“ 777”,返回应为3(因为数字“ 7”是连续的。

到目前为止,我编写了迭代方法。 ***在这种方法中不使用循环,只有递归。 ***

迭代示例:

public static int maxSequence(int num) {
        int max = 1;                        //initiate
        int currentCount = 1;
        int prevDigit = 11;//Because num%10 != 11 Always!
        int currentDigit;
        
        while (num!=0) {
            currentDigit = num%10;
            if (prevDigit == currentDigit)
                currentCount++;
            else if (currentCount > max)
                max = currentCount;
            
            if (prevDigit != currentDigit) //initiate for the next Iteration
                currentCount = 1;
            prevDigit = currentDigit;
            num = num/10;
        }
        return max;
    }

I have an assignment where i get an number input from the user, for example : "57779227"
and i need to return the longest sequence of identical numbers. For this example, the longest sequence is "777" and the return should be 3 (as the amount of times the number "7" is in a row.

So far I wrote an iteration method.
***No loops to be used in this method, ONLY RECURSION. ***

Iteration example :

public static int maxSequence(int num) {
        int max = 1;                        //initiate
        int currentCount = 1;
        int prevDigit = 11;//Because num%10 != 11 Always!
        int currentDigit;
        
        while (num!=0) {
            currentDigit = num%10;
            if (prevDigit == currentDigit)
                currentCount++;
            else if (currentCount > max)
                max = currentCount;
            
            if (prevDigit != currentDigit) //initiate for the next Iteration
                currentCount = 1;
            prevDigit = currentDigit;
            num = num/10;
        }
        return max;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2025-02-01 19:11:41

当先前digit!= current -digit时,将开始一个新的计数

public static int maxSequence(int num) {
        int previousMax = 1;
        int currentMax = 1;
        int previousDigit = num % 10;
        num /= 10;
        
        while (num != 0) {
            int currentDigit = num % 10;
            if (previousDigit == currentDigit) {
                currentMax++;
            } else {
                if (previousMax < currentMax) {
                    previousMax = currentMax;
                }
                currentMax = 1;
                previousDigit = currentDigit;
            }
            num /= 10;
        }
        
        return Math.max(currentMax, previousMax);
    }

When previousDigit != currentDigit then a new count will be start

public static int maxSequence(int num) {
        int previousMax = 1;
        int currentMax = 1;
        int previousDigit = num % 10;
        num /= 10;
        
        while (num != 0) {
            int currentDigit = num % 10;
            if (previousDigit == currentDigit) {
                currentMax++;
            } else {
                if (previousMax < currentMax) {
                    previousMax = currentMax;
                }
                currentMax = 1;
                previousDigit = currentDigit;
            }
            num /= 10;
        }
        
        return Math.max(currentMax, previousMax);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文