京东笔试 回文素数

只A了36%,没有找到原因。。
不是超时,是答案错误。。
求各位帮我看一下,,,费解呀

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int res = 0;
        for(int i = n;i<=m;i++){
            if(checkValid(i)) res++;
        }
        System.out.println(res);
        sc.close();
    }
    public static boolean checkValid (int num){
        String str = String.valueOf(num);
        for(int i = 0;i<str.length();i++){
            String temp = str.substring(0,i)+str.substring(i+1);
            int left = 0;
            while(temp.charAt(left)=='0'&&left<=temp.length()-1) left++;
            if(left>=temp.length()) continue;
            String curr = temp.substring(left);
            if(isPla(curr)&& strPrime(curr)) return true;
        }
        return false;

    }
    public static boolean strPrime(String str){
        int num = Integer.valueOf(str);
        return isPrime(num);
    }
    public static boolean isPrime (int num){
        if(num < 2) return false;
        if(num == 2) return true;
        int sqrt = (int) Math.sqrt(num);

        for(int i=2;i <= sqrt; i++){
            if(num%i==0) return false;
        }
        return true;

    }

    public static boolean isPla(String temp){
        int left = 0;
        int right = temp.length()-1;

        if(temp.length()==1) return true;
        while(left<right){
            char a = temp.charAt(left);
            char b = temp.charAt(right);
            if(a == b){
                left++;
                right--;
            }else{
                return false;
            }
        }
        return true;
    }

}
#笔试题目##京东#
全部评论
我也暴力,不过只有27😂
1 回复
分享
发布于 2020-08-06 22:04
我才18
1 回复
分享
发布于 2020-08-07 00:55
阿里巴巴
校招火热招聘中
官网直投
大概意思是: 一个数,去掉一位, 判断是否是 回文数&&素数。
点赞 回复
分享
发布于 2020-08-06 21:54
同样是A了36%
点赞 回复
分享
发布于 2020-08-06 22:12
我也是36,很怪异 ,我查了半小时还是没想起来漏了什么
点赞 回复
分享
发布于 2020-08-06 22:12
同36%
点赞 回复
分享
发布于 2020-08-06 22:16
+1
点赞 回复
分享
发布于 2020-08-06 22:18
我这儿有一个C++的代码,你要看吗,Java太长了
点赞 回复
分享
发布于 2020-08-06 22:29
1不是素数,判断加上后即可
点赞 回复
分享
发布于 2020-08-06 22:30
之前也是36,我把判断前置0的代码删掉,到了91
点赞 回复
分享
发布于 2020-08-06 22:41
其实可以打表😂
点赞 回复
分享
发布于 2020-08-06 22:56
10110,去掉第一位0110是,但数字应该是110不是
点赞 回复
分享
发布于 2020-08-06 23:23
同36  加了去第一位0之后没有变化 😂
点赞 回复
分享
发布于 2020-08-07 00:36
while(temp.charAt(left)=='0'&&left<=temp.length()-1) left++; 你这个语句不会越界吗?如果都是0的话
点赞 回复
分享
发布于 2020-08-07 09:11
我也36 奇怪得数字
点赞 回复
分享
发布于 2020-08-07 10:17
**血亏,我说怎么一直是18,我一直看的是有多少回文数,而不是回文素数,我日加个判断也许就过去了。。。。。。
点赞 回复
分享
发布于 2020-08-07 10:22
我想问一下,我写完代码后只测了自测数据通过了,然后点击了那个保存按钮,没有测全部数据,后台会帮我测全部数据吗?第一次用赛马那个平台,不会用,我一直按运行一直在跑自测数据,我还以为这个是平台自动帮你测的,看到你们个个AC多少多少,我感觉自己是不是白做了?😂😂😂
点赞 回复
分享
发布于 2020-08-07 10:56
如果n小于10,m大于10,就把n置为10,还有要解析前导0的情况
点赞 回复
分享
发布于 2020-08-07 14:56
我开始36,加了循环条件是i<sqrt&&i<num就a了
点赞 回复
分享
发布于 2020-08-07 15:06
小姐姐,我之前也是36,后来改到了91,原因是删除前置0的那里代码有了问题,你把这个地方改成这样就可以了while(left<=temp.length()-1 && temp.charAt(left)=='0') left++;
点赞 回复
分享
发布于 2020-08-08 23:19

相关推荐

1 7 评论
分享
牛客网
牛客企业服务