牛客代码测试栈深度

it2022-05-07  10

在刷牛客网剑指offer的时候遇到了一个题

题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的次数。

本人通过递归方法解题,发现报了OUTOFSTACKEXCEPTION,于是突发奇想,想看看牛客的栈深度,通过以下代码得到了深度为18886

public class Test{ private int count = 0; public void testAdd(){ count ++; testAdd(); } public void test(){ try{ testAdd(); }catch(Throwable e){ System.out.println(e); System.out.println("栈深度:"+count); } } public static void main(String [] args){ new Test().test(); } }

而测试样例中显然有值大于了这个数

// if(n>18886) throw new RuntimeException();

哈哈,本渣感觉成就满满~

转载于:https://www.cnblogs.com/BBchao/p/8600449.html


最新回复(0)