leetcode 903 超级回文数解答

时间仓促,代码可以优化,主要是思路:

class Solution {
    public int superpalindromesInRange(String L, String R) {
           long begin  =System.currentTimeMillis();
        long little = Long.valueOf(L);
        long bigger = Long.valueOf(R);
        int count = 0;
        int start = new java.math.BigDecimal(Math.sqrt(little)).setScale(0,java.math.BigDecimal.ROUND_HALF_DOWN).intValue();
        int end  = new java.math.BigDecimal(Math.sqrt(bigger)).setScale(0,java.math.BigDecimal.ROUND_HALF_DOWN).intValue();
            Map container = new TreeMap<>();
            List numberSet = new ArrayList<>();
            int nu = start;
            int realStart = 1;
            int realEnd = end;
            int squ =0;
            while(nu!=0){
                nu /=10;
                realStart*=10;
                squ++;
            }
            int endseq = 1;
            while(realEnd !=0){
                realEnd/=10;
                endseq++;

            }
            List numberSet2 = new ArrayList<>();
            for (long j =1 ; j < 10; j++){
                numberSet.add(j);
                long newVar = Long.valueOf(j+""+j);
                numberSet2.add(newVar);
                if (squ<3){
                    long result = j*j;
                    if (result > bigger){
                        break;
                    }
                    if (little<= result &&result < bigger){
                        long subnumber = result;
                        long subtemp = 0;

                        while( subnumber > 0 ){
                            subtemp=subtemp*10+(subnumber%10);
                            subnumber=subnumber/10;
                        }

                        if (result==subtemp){
                            count++;
                        }
                    }

                    result = newVar*newVar;

                    if (little<= result &&result < bigger){
                        long subnumber = result;
                        long subtemp = 0;

                        while( subnumber > 0 ){
                            subtemp=subtemp*10+(subnumber%10);
                            subnumber=subnumber/10;
                        }

                        if (result==subtemp){
                            count++;
                        }
                    }
                }
            }
            container.put(1,numberSet);
            container.put(2,numberSet2);
            realStart =10;
            for (int l = 3; l < endseq ; l++){
                realStart = realStart*10;
                List  preList = container.get(l-2);
                numberSet = new ArrayList<>();
                for (Long var : preList){
                    for(int i = 1 ; i <=9 ; i++){
                        long newVal = i*realStart+i+var*10;
                        numberSet.add(newVal);
                        if (l < squ){
                            continue;
                        }
                        long result = newVal*newVal;
                        if (result > bigger){
                            break;
                        }
                        if (little<= result &&result < bigger){
                            long subnumber = result;
                            long subtemp = 0;

                            while( subnumber > 0 ){
                                subtemp=subtemp*10+(subnumber%10);
                                subnumber=subnumber/10;
                            }

                            if (result==subtemp){
                                count++;
                            }
                        }

                    }
                    if (var%10==1){
                        for(int i = 1 ; i <=9 ; i++){
                            long newVal ;
                            if (var < 10){
                                newVal = i*realStart+i+var*10-1*(realStart/10);
                            }else{

                                newVal = i*realStart+i+var*10-10-1*(realStart/10);
                            }
                            numberSet.add(newVal);
                            long result = newVal*newVal;
                            if (result > bigger){
                                break;
                            }
                            if (little<= result &&result < bigger){
                                long subnumber = result;
                                long subtemp = 0;

                                while( subnumber > 0 ){
                                    subtemp=subtemp*10+(subnumber%10);
                                    subnumber=subnumber/10;
                                }

                                if (result==subtemp){
                                    count++;
                                }
                            }

                        }
                    }
                }
                container.put(l,numberSet);
            }
        System.out.println(System.currentTimeMillis()-begin);
        return count;
    }
}

你可能感兴趣的:(leetcode 903 超级回文数解答)