简体   繁体   English

为什么时间限制超过问题在一个代码中显示,而在另一个代码中传递测试用例?

[英]why Time Limit exceed problem is showing in one code while passing test cases in other?

I was solving the next palindrome problem but failed to understand why my code 1 is showing time limit exceeded and not running properly while code2 has passed all test cases?我正在解决下一个回文问题,但未能理解为什么我的代码 1 显示超出时间限制并且在代码 2 已通过所有测试用例时无法正常运行? I have used the same concept while doing both the codes but just used procedure in the second case.我在执行这两个代码时使用了相同的概念,但在第二种情况下只使用了过程。

Here are the two codes :这是两个代码:

code 1代码 1

        int no=sc.nextInt();
        boolean check=true;
        int rem,temp,rev=0;
        while(check)
        {
            no++;
            temp=no;
            while(temp>0)
            {
                 rem=temp%10;
                 temp=temp/10;
                 rev=rev*10+rem;

            }
            if(rev==no)
            {
                check=false;
                System.out.println(no);
                break;
            }
        
        }

            
        }
        catch(Exception e)
        {
            System.out.println(e);
        }```



code 2
```    public static void main(String[] args) {
        // TODO code application logic here
        Scanner sc=new Scanner(System.in);
        //Scanner sc=new Scanner(System.in);
        int no=sc.nextInt();
        boolean check=true;
        int rem,temp,temp2=1,rev=0;
        System.out.println(no);
        while(temp2!=0)
        {
            //no=no+1;
            if(pallin(no))
            {
                System.out.println(no);
                temp2=0;
            }
            no++;
        
        }
        
            
        /*while(no<900)
        {   no++;
            //StringBuilder str_no=new StringBuilder(no);
            System.out.println(str_no.reverse());
            
            if(str_no.equals(str_no.reverse()) )
            {
                check=false;
                break;
            } 
        }
        if(check==false)
        {
            System.out.println(no);
        }*/
    }
    private static boolean pallin(int no)
    {
         int rem,temp=no,rev=0;
         while(temp>0)
            {
                 rem=temp%10;
                 temp=temp/10;
                 rev=rev*10+rem;

            }
            System.out.println(rev);
            
            if(rev==no)
            {
                
                System.out.println(rev);
                return true;
              
            }
            return false;
    }
    
}```

I think what is wrong is that you set your int rev=0.我认为错误的是您设置了 int rev=0。 When calculating rev=rev*10+rem;计算rev=rev*10+rem; you're getting a wrong answer.你得到了错误的答案。 Moreover, in the nested while loop with condition no>0;此外,在条件no>0;的嵌套 while 循环中no>0; no is not changing value.不改变价值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示 Time Limit Exceed erroe 的代码,有人可以解释一下,这段代码的问题在哪里 - The code showing Time Limit Exceed erroe, could some one explain, where is problem in this code 为什么长,不是int,否则限制时间超过 - why long, not int otherwise limit time exceed 我的最大 1、最大 2、最大 3 编号的代码通过了所有测试用例,但一个测试用例显示 MLE 错误有人知道如何通过吗? - My code for max 1, max 2, max3 number is passing all test cases but one test case is showing MLE error can any one know how to pass that? 代码没有在LeetCode#451上通过最终测试用例,是否可用于所有其他测试用例? - Code not passing final test case on LeetCode #451, working for all other test cases? 如何在等待一个测试用例执行完成时在TestNG测试套件中执行其他测试用例 - How to execute other test cases in a TestNG test suite while waiting for one test case execution to finish 为什么我的联合查找不交集集算法不能通过所有测试用例? - Why is my Union Find Disjoint Sets algo for this problem not passing all test cases? Hashmap 代码未通过所有测试用例 - Hashmap code not passing all the test cases 为什么我的字符数组的就地递归代码不适用于所有测试用例? 我的代码有什么问题 - why my code of inplace recursion of character array is not working for all the test cases? is there any problem with my code 为什么更改 while 循环中的条件顺序会影响通过测试用例的数量? - Why does changing the order of conditions in a while loop affect the number of passing test cases? 为什么在测试用例中测量代码覆盖率? - Why is code coverage measured in test cases?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM