leetcode
leetcode 2701 ~ 2750
k 镜像数字的和

k 镜像数字的和

难度:

标签:

题目描述

A k-mirror number is a positive integer without leading zeros that reads the same both forward and backward in base-10 as well as in base-k.

  • For example, 9 is a 2-mirror number. The representation of 9 in base-10 and base-2 are 9 and 1001 respectively, which read the same both forward and backward.
  • On the contrary, 4 is not a 2-mirror number. The representation of 4 in base-2 is 100, which does not read the same both forward and backward.

Given the base k and the number n, return the sum of the n smallest k-mirror numbers.

 

Example 1:

Input: k = 2, n = 5
Output: 25
Explanation:
The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
  base-10    base-2
    1          1
    3          11
    5          101
    7          111
    9          1001
Their sum = 1 + 3 + 5 + 7 + 9 = 25. 

Example 2:

Input: k = 3, n = 7
Output: 499
Explanation:
The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
  base-10    base-3
    1          1
    2          2
    4          11
    8          22
    121        11111
    151        12121
    212        21212
Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.

Example 3:

Input: k = 7, n = 17
Output: 20379000
Explanation: The 17 smallest 7-mirror numbers are:
1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596

 

Constraints:

  • 2 <= k <= 9
  • 1 <= n <= 30

代码结果

运行时间: 740 ms, 内存: 139.6 MB


/*
题目思路:
1. 使用流操作来生成数字序列并筛选k镜像数字。
2. 判断一个数字是否是十进制和k进制下的镜像数字。
3. 使用流操作将前n个k镜像数字的和计算出来。
*/

import java.util.stream.IntStream;

public class KMirrorNumbersStream {
    // 判断是否是回文数字
    public static boolean isPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    // 转换成k进制并判断是否是镜像数字
    public static boolean isKMirror(int num, int k) {
        String baseK = Integer.toString(num, k);
        return isPalindrome(baseK);
    }

    public static long sumKMirrorNumbers(int k, int n) {
        return IntStream.iterate(1, i -> i + 1)
                .filter(num -> isPalindrome(String.valueOf(num)) && isKMirror(num, k))
                .limit(n)
                .asLongStream()
                .sum();
    }

    public static void main(String[] args) {
        int k = 2, n = 5;
        System.out.println(sumKMirrorNumbers(k, n)); // 输出 25
    }
}

解释

方法:

该题解的核心是生成并检验k镜像数字。首先,使用一个递归函数`gen_ans`生成长度为`i`的所有可能的k进制数,这些数的特点是从两边向中间对称。然后,逐个检验这些数是否也是十进制下的镜像数字。如果是,累加它们的十进制值,并计数。当找到足够的k镜像数字时,返回它们的和。

时间复杂度:

O(k^i * d),其中i是最大的数字长度,d是数字的位数

空间复杂度:

O(k^i),其中i是最大的数字长度

代码细节讲解

🦆
在递归函数`gen_ans`中,如何确保生成的k进制数确实是对称的?具体的对称构造逻辑是什么?
在`gen_ans`递归函数中,确保生成的k进制数是对称的通过以下步骤:首先,如果请求生成长度为0的对称数,返回一个空字符串列表。如果长度为1,则返回所有单个数字的字符串,这些自然是对称的。对于更长的长度,函数首先遍历所有可能的数字作为起始和结束位(保证对称),然后递归地在这对数字之间插入长度为`length-2`的对称数。这样,每个生成的数都是通过在较短的对称数两侧添加相同数字构造的,从而确保整个数字的对称性。
🦆
为什么在检验十进制镜像数字时,需要特别判断生成的k进制数的首位不为'0'?
在k进制数中,首位数字为'0'意味着该数实际上在数学意义上不是一个有效的数值(例如,'012'在十进制中应该表示为'12')。这种情况在k进制表示中也同样适用,因为首位的'0'会使得数值的表示和实际值不符。在转换这种数到十进制时,会丢失前导零,导致可能的误解和数值错误。因此,检验时需要排除首位为'0'的k进制数,确保所有数在十进制和k进制中都是有效且准确的。
🦆
题解中提到了递归深度为101,这个数字是如何确定的?是否有可能存在更优的深度选择?
递归深度101在代码中并不是基于严格的数学证明,而是一个实际的设定范围,可能是为了确保足够的搜索深度来生成所需数量的镜像数字。这个值可能比实际需要的值大,确保覆盖足够的情况。实际上,更优的深度选择应该基于n的值(即所需的镜像数字的数量)和k的值。如果n较小或k较大,可能不需要如此深的递归。在实际应用中,可以根据具体问题需要调整递归深度,通过实验确定最佳值以优化性能。

相关问题