给定一个由N个形式为{L,R}的查询组成的数组Q [] [] ,每个查询的任务是从范围[L,R]中查找其总数为回文的数字总数。
例子:
Input: Q[][] = {{2, 10}, {10, 20}}
Output:
2
1
Explanation:
Query 1: The numbers from the range [2, 10], whose cube is a palindrome are 2, 7
Query 2: The only number from the range [10, 20], whose cube is a palindrome is 11
Input: Q[][] = {{1, 50}, {13, 15}}
Output:
4
0
Explanation:
Query 1: The numbers from the range [1, 50], whose cube is a palindrome are {1, 2, 7, 11}.
Query 2: No such number exists in the range [13, 15].
方法:解决问题的最简单方法是使用“包含-排除原理”和“前缀和数组”技术来解决此问题。请按照以下步骤解决给定的问题:
- 初始化数组arr [] ,以在第i个存储 索引,无论i的多维数据集是否为回文。
- 遍历数组arr []并且每隔第i次遍历一次 索引,检查i的多维数据集是否是回文。
- 如果发现为真,则设置arr [i] = 1 。
- 否则,设置arr [i] = 0 。
- 将数组arr []转换为前缀和数组。
- 遍历数组Q [] [] ,并通过计算arr [R] – arr [L-1]来计算范围为[L,R]的立方数为回文数的数字。
下面是上述方法的实现。
C++
// C++ program of the above approach
#include
using namespace std;
int arr[10005];
// Function to check if n is
// a pallindrome number or not
int isPalindrome(int n)
{
// Temporarily store n
int temp = n;
// Stores reverse of n
int res = 0;
// Iterate until temp reduces to 0
while (temp != 0) {
// Extract the last digit
int rem = temp % 10;
// Add to the start
res = res * 10 + rem;
// Remove the last digit
temp /= 10;
}
// If the number and its
// reverse are equal
if (res == n) {
return 1;
}
// Otherwise
else
return 0;
}
// Function to precompute and store
// the count of numbers whose cube
// is a palindrome number
void precompute()
{
// Iterate upto 10^4
for (int i = 1; i <= 10000; i++) {
// Check if i*i*i is a
// pallindrome or not
if (isPalindrome(i * i * i))
arr[i] = 1;
else
arr[i] = 0;
}
// Convert arr[] to prefix sum array
for (int i = 1; i <= 10000; i++) {
arr[i] = arr[i] + arr[i - 1];
}
}
// Driver Code
int main()
{
// Given queries
vector > Q = { { 2, 7 }, { 10, 25 } };
precompute();
for (auto it : Q) {
// Using inclusion-exclusion
// principle, count required numbers
cout << arr[it.second] - arr[it.first - 1] << "\n";
}
return 0;
}
Java
// Java program of the above approach
import java.io.*;
import java.util.*;
public class Pair {
private final int key;
private final int value;
public Pair(int aKey, int aValue)
{
key = aKey;
value = aValue;
}
public int key() { return key; }
public int value() { return value; }
}
class GFG {
static int[] arr = new int[10005];
// Function to check if n is
// a pallindrome number or not
static int isPalindrome(int n)
{
// Temporarily store n
int temp = n;
// Stores reverse of n
int res = 0;
// Iterate until temp reduces to 0
while (temp != 0)
{
// Extract the last digit
int rem = temp % 10;
// Add to the start
res = res * 10 + rem;
// Remove the last digit
temp /= 10;
}
// If the number and its
// reverse are equal
if (res == n) {
return 1;
}
// Otherwise
else
return 0;
}
// Function to precompute and store
// the count of numbers whose cube
// is a palindrome number
static void precompute()
{
// Iterate upto 10^4
for (int i = 1; i <= 10000; i++) {
// Check if i*i*i is a
// pallindrome or not
if (isPalindrome(i * i * i)!= 0)
arr[i] = 1;
else
arr[i] = 0;
}
// Convert arr[] to prefix sum array
for (int i = 1; i <= 10000; i++) {
arr[i] = arr[i] + arr[i - 1];
}
}
// Driver Code
public static void main(String[] args)
{
// Given queries
ArrayList Q = new ArrayList();
Pair pair = new Pair(2, 7);
Q.add(pair);
Pair pair2 = new Pair(10, 25);
Q.add(pair2);
precompute();
for (int i = 0; i < Q.size(); i++) {
// Using inclusion-exclusion
// principle, count required numbers
System.out.println(arr[Q.get(i).value()] - arr[Q.get(i).key()-1]);
}
}
}
// This code is contributed by Dharanendra L V
输出:
2
1
时间复杂度: O(N)
辅助空间: O(maxm),其中maxm表示查询中R的最大值