leetCode进阶算法题+解析(八十)

回文素数

题目:求出大于或等于 N 的最小回文素数。回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数。例如,2,3,5,7,11 以及 13 是素数。回顾一下,如果一个数从左往右读与从右往左读是一样的,那么这个数是回文数。例如,12321 是回文数。

示例 1:
输入:6
输出:7
示例 2:
输入:8
输出:11
示例 3:
输入:13
输出:101
提示:
1 <= N <= 10^8
答案肯定存在,且小于 2 * 10^8。

思路:这个题我暂时没get到考点在哪里。不知道暴力法会不会超时。我暂时的想法是从n开始往上找。先获取比n大的素数,然后判断是不是回文数,是直接返回。不是继续找下一个素数。我去实现下看看。
说出来可能比较诧异,这个题就是暴力法过的,贴代码:

class Solution {
    public int primePalindrome(int N) {
        while (true) {
            if (N == reverse(N) && isPrime(N))
                return N;
            N++;
            if (10000000 < N && N < 100000000)
                N = 100000000;
        }
    }

    public boolean isPrime(int N) {
        if (N < 2) return false;
        int R = (int) Math.sqrt(N);
        for (int d = 2; d <= R; ++d)
            if (N % d == 0) return false;
        return true;
    }

    public int reverse(int N) {
        int ans = 0;
        while (N > 0) {
            ans = 10 * ans + (N % 10);
            N /= 10;
        }
        return ans;
    }
}

注意上面的代码是我看题解得来的,我自己写的超时了,坑在了

if (10000000 < N && N < 100000000)
                N = 100000000;

这句,看了题解才发现据说八位数的没有质数,所以不要浪费性能。不加这行就超时。然後性能第一的大佬的做法是把所有回文素数找出来然後二分去找N的下一个。总而言之挺睿智的,贴上代码:

class Solution {
    int[] ans = {2,3,5,7,11,101,131,151,181,191,313,353,373,383,727,757,787,797,919,929,10301,10501,10601,11311,11411,12421,12721,12821,13331,13831,13931,14341,14741,15451,15551,16061,16361,16561,16661,17471,17971,18181,18481,19391,19891,19991,30103,30203,30403,30703,30803,31013,31513,32323,32423,33533,34543,34843,35053,35153,35353,35753,36263,36563,37273,37573,38083,38183,38783,39293,70207,70507,70607,71317,71917,72227,72727,73037,73237,73637,74047,74747,75557,76367,76667,77377,77477,77977,78487,78787,78887,79397,79697,79997,90709,91019,93139,93239,93739,94049,94349,94649,94849,94949,95959,96269,96469,96769,97379,97579,97879,98389,98689,1003001,1008001,1022201,1028201,1035301,1043401,1055501,1062601,1065601,1074701,1082801,1085801,1092901,1093901,1114111,1117111,1120211,1123211,1126211,1129211,1134311,1145411,1150511,1153511,1160611,1163611,1175711,1177711,1178711,1180811,1183811,1186811,1190911,1193911,1196911,1201021,1208021,1212121,1215121,1218121,1221221,1235321,1242421,1243421,1245421,1250521,1253521,1257521,1262621,1268621,1273721,1276721,1278721,1280821,1281821,1286821,1287821,1300031,1303031,1311131,1317131,1327231,1328231,1333331,1335331,1338331,1343431,1360631,1362631,1363631,1371731,1374731,1390931,1407041,1409041,1411141,1412141,1422241,1437341,1444441,1447441,1452541,1456541,1461641,1463641,1464641,1469641,1486841,1489841,1490941,1496941,1508051,1513151,1520251,1532351,1535351,1542451,1548451,1550551,1551551,1556551,1557551,1565651,1572751,1579751,1580851,1583851,1589851,1594951,1597951,1598951,1600061,1609061,1611161,1616161,1628261,1630361,1633361,1640461,1643461,1646461,1654561,1657561,1658561,1660661,1670761,1684861,1685861,1688861,1695961,1703071,1707071,1712171,1714171,1730371,1734371,1737371,1748471,1755571,1761671,1764671,1777771,1793971,1802081,1805081,1820281,1823281,1824281,1826281,1829281,1831381,1832381,1842481,1851581,1853581,1856581,1865681,1876781,1878781,1879781,1880881,1881881,1883881,1884881,1895981,1903091,1908091,1909091,1917191,1924291,1930391,1936391,1941491,1951591,1952591,1957591,1958591,1963691,1968691,1969691,1970791,1976791,1981891,1982891,1984891,1987891,1988891,1993991,1995991,1998991,3001003,3002003,3007003,3016103,3026203,3064603,3065603,3072703,3073703,3075703,3083803,3089803,3091903,3095903,3103013,3106013,3127213,3135313,3140413,3155513,3158513,3160613,3166613,3181813,3187813,3193913,3196913,3198913,3211123,3212123,3218123,3222223,3223223,3228223,3233323,3236323,3241423,3245423,3252523,3256523,3258523,3260623,3267623,3272723,3283823,3285823,3286823,3288823,3291923,3293923,3304033,3305033,3307033,3310133,3315133,3319133,3321233,3329233,3331333,3337333,3343433,3353533,3362633,3364633,3365633,3368633,3380833,3391933,3392933,3400043,3411143,3417143,3424243,3425243,3427243,3439343,3441443,3443443,3444443,3447443,3449443,3452543,3460643,3466643,3470743,3479743,3485843,3487843,3503053,3515153,3517153,3528253,3541453,3553553,3558553,3563653,3569653,3586853,3589853,3590953,3591953,3594953,3601063,3607063,3618163,3621263,3627263,3635363,3643463,3646463,3670763,3673763,3680863,3689863,3698963,3708073,3709073,3716173,3717173,3721273,3722273,3728273,3732373,3743473,3746473,3762673,3763673,3765673,3768673,3769673,3773773,3774773,3781873,3784873,3792973,3793973,3799973,3804083,3806083,3812183,3814183,3826283,3829283,3836383,3842483,3853583,3858583,3863683,3864683,3867683,3869683,3871783,3878783,3893983,3899983,3913193,3916193,3918193,3924293,3927293,3931393,3938393,3942493,3946493,3948493,3964693,3970793,3983893,3991993,3994993,3997993,3998993,7014107,7035307,7036307,7041407,7046407,7057507,7065607,7069607,7073707,7079707,7082807,7084807,7087807,7093907,7096907,7100017,7114117,7115117,7118117,7129217,7134317,7136317,7141417,7145417,7155517,7156517,7158517,7159517,7177717,7190917,7194917,7215127,7226227,7246427,7249427,7250527,7256527,7257527,7261627,7267627,7276727,7278727,7291927,7300037,7302037,7310137,7314137,7324237,7327237,7347437,7352537,7354537,7362637,7365637,7381837,7388837,7392937,7401047,7403047,7409047,7415147,7434347,7436347,7439347,7452547,7461647,7466647,7472747,7475747,7485847,7486847,7489847,7493947,7507057,7508057,7518157,7519157,7521257,7527257,7540457,7562657,7564657,7576757,7586857,7592957,7594957,7600067,7611167,7619167,7622267,7630367,7632367,7644467,7654567,7662667,7665667,7666667,7668667,7669667,7674767,7681867,7690967,7693967,7696967,7715177,7718177,7722277,7729277,7733377,7742477,7747477,7750577,7758577,7764677,7772777,7774777,7778777,7782877,7783877,7791977,7794977,7807087,7819187,7820287,7821287,7831387,7832387,7838387,7843487,7850587,7856587,7865687,7867687,7868687,7873787,7884887,7891987,7897987,7913197,7916197,7930397,7933397,7935397,7938397,7941497,7943497,7949497,7957597,7958597,7960697,7977797,7984897,7985897,7987897,7996997,9002009,9015109,9024209,9037309,9042409,9043409,9045409,9046409,9049409,9067609,9073709,9076709,9078709,9091909,9095909,9103019,9109019,9110119,9127219,9128219,9136319,9149419,9169619,9173719,9174719,9179719,9185819,9196919,9199919,9200029,9209029,9212129,9217129,9222229,9223229,9230329,9231329,9255529,9269629,9271729,9277729,9280829,9286829,9289829,9318139,9320239,9324239,9329239,9332339,9338339,9351539,9357539,9375739,9384839,9397939,9400049,9414149,9419149,9433349,9439349,9440449,9446449,9451549,9470749,9477749,9492949,9493949,9495949,9504059,9514159,9526259,9529259,9547459,9556559,9558559,9561659,9577759,9583859,9585859,9586859,9601069,9602069,9604069,9610169,9620269,9624269,9626269,9632369,9634369,9645469,9650569,9657569,9670769,9686869,9700079,9709079,9711179,9714179,9724279,9727279,9732379,9733379,9743479,9749479,9752579,9754579,9758579,9762679,9770779,9776779,9779779,9781879,9782879,9787879,9788879,9795979,9801089,9807089,9809089,9817189,9818189,9820289,9822289,9836389,9837389,9845489,9852589,9871789,9888889,9889889,9896989,9902099,9907099,9908099,9916199,9918199,9919199,9921299,9923299,9926299,9927299,9931399,9932399,9935399,9938399,9957599,9965699,9978799,9980899,9981899,9989899,100030001};
    public int primePalindrome(int N) {
        return binaryFind(N);
    }

    private int binaryFind(int n) {
        int l=0,r=ans.length-1;
        while(l= n){
                r = mid;
            }else{
                l = mid + 1;
            }
        }
        return ans[l];
    }

}

然後这就没啥好说的了,下一题吧:

重新排序得到2的幂

题目:给定正整数 N ,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零。如果我们可以通过上述方式得到 2 的幂,返回 true;否则,返回 false。

示例 1:
输入:1
输出:true
示例 2:
输入:10
输出:false
示例 3:
输入:16
输出:true
示例 4:
输入:24
输出:false
示例 5:
输入:46
输出:true
提示:
1 <= N <= 10^9

思路:我感觉这个题应该挺简单的。因为数据范围,所以其实2的幂也就那么多数字。N是10的九次方,也就是说N的最大值2的32次幂完全够用。我打算把2的32次幂的值都算出来。然後挨个数字和给定N的数字对比。不是直接比较,是存成数组,几个1,几个2,几个3这种。然後最终有一样的结果就是true。否则false。我去实现下试试。
第一版代码:

class Solution {
    public boolean reorderedPowerOf2(int n) {
        if(n == 1 || n == 2 || n == 4) return true;
        int[] d = new int[10];//0到9
        while (n>0){
            d[n%10]++;
            n /= 10;
        }
        int temp = 16;
        while(temp<1000000000){
            if(isOk(d,temp)) return true;
            temp *= 2;
        }
        return false;//走到这说明没合适的。
    }
    public boolean isOk(int[] d,int temp){
        int[] cur =  new int[10];
        while (temp>0){
            cur[temp%10]++;
            temp /= 10;
        }
        for(int i = 0;i<10;i++){
            if(d[i] != cur[i]) return false;
        }
        return true;
    }
}

思路没问题,性能也挺好,总而言之这个题这么想没错。当然了别的处理办法应该也有的。比如判断两个数是不是相等可以先排序再对比之类的。但是我觉得上面的处理已经很不错了, 我去看看性能第一的代码:

class Solution {
    public boolean reorderedPowerOf2(int N) {
        int[] num = new int[10];
        int min = 1;
        while(N>0){
            num[N%10]++;
            N=N/10;
            min*=10;
        }
        min/=10;
        
        int flag=1;
        while(flag0){
                temp[N%10]++;
                N=N/10;
            }
            for(int i=0;i<=9;i++){
                if(temp[i]!=num[i]){
                    res = true;
                    break;
                }
            }
            if(!res){
                return true;
            }
            
        }

        return false;
    }
}

一样一样的思路,我就不多说了,直接下一题吧。

优势洗牌

题目:给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述。返回 A 的任意排列,使其相对于 B 的优势最大化。

示例 1:
输入:A = [2,7,11,15], B = [1,10,4,11]
输出:[2,11,7,15]
示例 2:
输入:A = [12,24,8,32], B = [13,25,32,11]
输出:[24,32,8,12]
提示:
1 <= A.length = B.length <= 10000
0 <= A[i] <= 10^9
0 <= B[i] <= 10^9

思路:这个题我的想法就是贪心,每次尽量比B大且最小的值。这样总不会错。然後数据范围1w,估计二分查找大切仅大应该不会超时?这个题感觉应该不难,我去试试代码。
好吧,第一版代码超时了,卡在最后一个测试用例。。。也可能是我没二分查找的原因,先附上第一版代码:

class Solution {
    public int[] advantageCount(int[] A, int[] B) {
        Arrays.sort(A);
        int len = B.length;
        List list = new ArrayList();
        for(int i : A) list.add(i);
        int[] ans = new int[len];
        for(int i = 0;iB[i]) {
                    ans[i] = j;
                    list.remove(Integer.valueOf(j));
                    flag = false;
                    break;
                }
            }
            //说明这个元素没有更大的,那么把最小的放在这
            if(flag) {
                ans[i] = list.get(0);
                list.remove(0);
            }
        }
        return ans;
    }
}

我把list的遍历换成二分法应该就没问题了,我去试试:
果然第二版代码这个遍历改成二分就过了,直接贴代码:

class Solution {
    public int[] advantageCount(int[] A, int[] B) {
        Arrays.sort(A);
        int len = B.length;
        List list = new ArrayList();
        for(int i : A) list.add(i);
        int[] ans = new int[len];
        for(int i = 0;iB[i]) {
                ans[i] = list.get(0);
                list.remove(0);
            }else {
                while(r-l>1) {
                    int mid = (l+r)/2;
                    if(list.get(mid)>B[i]) {
                        r = mid;
                    }else {
                        l = mid;
                    }
                }
                ans[i] = list.get(r);
                list.remove(r);
            }
        }
        return ans;
    }
}

虽然性能不是特别好,但是起码ac了,然后二分的话就判断下两边的值。剩下就没什么好说的了。性能不好我觉得应该是细节处理上,思路应该是没问题的吧。。我去看看性能第一的代码:

class Solution {
     public int[] advantageCount(int[] A, int[] B) {
        Arrays.sort(A);
        int len = A.length;
        int[] others = new int[len];
        int[] res = new int[len];
        for (int i = 0; i < len;++i){
            others[i] = i;
        }
        qSort(B, 0, len - 1, others);
        int start = 0, right = len - 1;
        for (int value : A) {
            if (value > B[start]) {
                res[others[start++]] = value;
            } else {
                res[others[right--]] = value;
            }
        }
        return res;
    }

    private void qSort(int[] arr, int start, int end, int[] others) {
        if (start < end) {
            int left = start, right = end + 1, pivot = arr[start];
            while(left < right) {
                while(arr[++left] <= pivot && left < end);
                while(arr[--right] >= pivot && right > start);
                if (left >= right) {
                    break;
                }
                swap(arr, left, right);
                swap(others, left, right);
            }
            swap(arr, start, right);
            swap(others, start, right);
            qSort(arr, start, right - 1, others);
            qSort(arr, right + 1, end, others);
        }
    }

    private void swap(int[] num, int i, int j){
        int tmp = num[i];
        num[i] = num[j];
        num[j] = tmp;
    }
}

这代码量,emmmm...总而言之思路没错,只不过人家细节处理上更好。不多说了,下一题。

青蛙过河

题目:一只青蛙想要过河。 假定河流被等分为若干个单元格,并且在每一个单元格内都有可能放有一块石子(也有可能没有)。 青蛙可以跳上石子,但是不可以跳入水中。给你石子的位置列表 stones(用单元格序号 升序 表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一块石子上)。开始时, 青蛙默认已站在第一块石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格 1 跳至单元格 2 )。如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1、k 或 k + 1 个单位。 另请注意,青蛙只能向前方(终点的方向)跳跃。

示例 1:
输入:stones = [0,1,3,5,6,8,12,17]
输出:true
解释:青蛙可以成功过河,按照如下方案跳跃:跳 1 个单位到第 2 块石子, 然后跳 2 个单位到第 3 块石子, 接着 跳 2 个单位到第 4 块石子, 然后跳 3 个单位到第 6 块石子, 跳 4 个单位到第 7 块石子, 最后,跳 5 个单位到第 8 个石子(即最后一块石子)。
示例 2:
输入:stones = [0,1,2,3,4,8,9,11]
输出:false
解释:这是因为第 5 和第 6 个石子之间的间距太大,没有可选的方案供青蛙跳跃过去。
提示:
2 <= stones.length <= 2000
0 <= stones[i] <= 231 - 1
stones[0] == 0

思路:这个题是2021/4/29的每日一题。困难难度的,然後看完题目应该比较好理解:每次蹦跶最多三种选择:上一次蹦跶-1.上一次蹦跶步数,上一次蹦跶部署+1.而且很典型的动态规划。首先0只能跳到1,所以0,1是必须的。而再往下可以跳1步(k),也可以跳2步(k+1).这里因为k是1.不能k-1.所以继续往下可以跳到2,3。但是这个时候跳到2和3不是一样的。因为跳到2的话则2只能往3,4跳。3的话因为上一步1到3k是2,所以3可以往后跳1,2,3步。也就是3可以跳到4,5,6.然後我们由此知道跳到某一个点的k是很重要的东西,所以我们可以用dp记录到当前石头的k的可能性。然後就能知道当前石子往后走的可能行了,暂时的思路是这样,我去试试代码。
第一版代码(超时):

class Solution {
    public boolean canCross(int[] stones) {
        if(stones[1] != 1) return false;
        int max = stones[stones.length-1];
        Map> map = new HashMap>();
        for(int i : stones) map.put(i, new ArrayList());
        map.get(1).add(1);
        for(int j =1;j1 && map.get(i+k-1)!=null) map.get(i+k-1).add(k-1);
                if(map.get(i+k) != null) map.get(i+k).add(k);
                if(map.get(i+k+1) != null) map.get(i+k+1).add(k+1);
                if(i+k-1 == max || i+k == max || i+k+1 == max) return true;
            }
            map.remove(i);
        }
        //到这还没true说明没有能到的
        return false;
    }
}

依照上面的思路写出了第一版代码.但是超时了。其实本质上还是记录所有跳到当前的k。然后我们就可以根据k来算出下一步可以跳到的格子。这样一步一步往下顺,只要看有没有能跳到最后一个石子的可能就行了。但是问题是超时了。。咳咳,然后我上面用map超时,我觉得可以换一种数据结构试试。一个常规思路:当数据量不大的时候,数组是一个很好的选择。用空间换时间嘛。

class Solution {
    public boolean canCross(int[] stones) {
        int n = stones.length;
        boolean[][] dp = new boolean[n][n];
        dp[0][0] = true;
        for (int i = 1; i < n; ++i) {
            if (stones[i] - stones[i - 1] > i) {
                return false;
            }
        }
        for (int i = 1; i < n; ++i) {
            for (int j = i - 1; j >= 0; --j) {
                int k = stones[i] - stones[j];
                if (k > j + 1) {
                    break;
                }
                dp[i][k] = dp[j][k - 1] || dp[j][k] || dp[j][k + 1];
                if (i == n - 1 && dp[i][k]) {
                    return true;
                }
            }
        }
        return false;
    }
}

其实这个题目的k因为一次最多只能+1.所以其实步数最多也就是n步。然后用的二维布尔数组来表示当前可蹦跶的步数。同理如果能蹦跶到最后一块就直接true,如果能蹦跶的都蹦跶了还没true则返回false。总而言之思路就是这样。然后这个题就这样了。
本篇笔记就到这里,如果稍微帮到你了记得点个喜欢点个关注。也祝大家工作顺顺利利,生活健健康康~!

你可能感兴趣的:(leetCode进阶算法题+解析(八十))