当前位置:网站首页>Two methods are used to solve the "maximum palindrome product" problem
Two methods are used to solve the "maximum palindrome product" problem
2022-04-23 03:05:00 【& Xiaobai &】
fourteen 、 Maximum palindrome product
14.1、 Question design requirements
Given an integer n , return Can be expressed as two n Of the product of bit integers Maximum palindrome integer . Because the answer can be very big , So return it to 1337 Remainder .
Example 1:
Input :n = 2
Output :987
explain :99 x 91 = 9009, 9009 % 1337 = 987
Example 2:
Input : n = 1
Output : 9
Tips :
1 <= n <= 8
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/largest-palindrome-product
14.2、 Their thinking
The first solution :
First calculate the maximum product , Then take out the first half of the maximum product and use the generation function to generate the corresponding palindrome number , Then compare the generated palindromes , If not found, subtract the first half of the number and regenerate a new palindrome number for comparison , Go on in turn ; If it is found, output it .
The second solution :
Because the scope of this question is fixed and relatively small , Just type the meter directly .
14.3、 Algorithm
The first algorithm :
class Solution {
public int largestPalindrome(int n) {
// If n by 1, only 9 Maximum
if (n == 1) {
return 9;
}
// Definite boundary
long upper = (long) Math.pow(10,n) - 1;
long lower = upper/10 + 1;
// The maximum product in the range
long maxNumber = upper * upper;
// Calculate the first half of the maximum number
long half = (long) (maxNumber/Math.pow(10,n));
// Whether to find
boolean found = false;
// Return value
long result = 0;
// Loop to find the corresponding number of palindromes
while (!found){
// Call build function
result = create(half);
// Search from high to low
for (long i = upper; i >= lower; i--) {
//i*i The value is less than result Value
if (i * i < result){
break;
}
//result Yes i Take remainder as 0
if (result % i == 0){
found = true;
break;
}
}
// Did not find , Then look for the next digit in the first half
half--;
}
// Returns the number of palindromes and returns to 1337 Remainder
return (int) (result % 1337);
}
// Generating function
public long create(long number){
// Splice the first half of the transmitted number with the first half of the number generated by flipping to generate a new palindrome number
String str = number + new StringBuilder().append(number).reverse().toString();
return Long.parseLong(str);
}
}
Reference video : Power button up Lord Guo
The second algorithm :
class Solution {
public int largestPalindrome(int n) {
int[] palindromes = {
9,987,123,597,677,1218,877,475};
return palindromes[n - 1];
}
}
版权声明
本文为[& Xiaobai &]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301476761.html
边栏推荐
- [learn junit5 from official documents] [II] [writingtests] [learning notes]
- Cherno_ Game engine series tutorial (5): 101~
- ASP.NET 6 中间件系列 - 执行顺序
- Xamarin效果第二十二篇之录音效果
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- ASP.NET和ASP.NETCore多环境配置对比
- 使用split来解决“最常见的单词”问题
- Deep q-network (dqn)
- 最通俗易懂的依赖注入与控制反转
- How to use C language to realize [guessing numbers game]
猜你喜欢

Encapsulation of ele table

Openfeign details show
![Niuke white moon race 6 [solution]](/img/c5/6c59378c3bb12efa60ab3a8cd2c943.png)
Niuke white moon race 6 [solution]

Laravel new route file

Detailed explanation of distributed things

Opencv fills the rectangle with a transparent color

L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
![How to use C language to realize [guessing numbers game]](/img/8c/052dcb0ce64ee1713bebb1340248e6.png)
How to use C language to realize [guessing numbers game]

LNMP MySQL allows remote access

微软是如何解决 PC 端程序多开问题的——内部实现
随机推荐
Openfeign details show
中后二叉建树
HLS / chisel practice CORDIC high performance computing complex square root
Chapter IV project cost management of information system project manager summary
使用DFS来解决“字典序排数”问题
Redis data server / database / cache (2022)
Traversée de l'arbre L2 - 006
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
微软是如何解决 PC 端程序多开问题的——内部实现
eventBus
宁德时代地位不保?
Typescript Learning Guide
Summary of software test interview questions
《信息系統項目管理師總結》第六章 項目人力資源管理
Creating wechat voucher process with PHP
JSON data text
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
[software testing] understand the basic knowledge of software testing
Introduction to ACM [TSP problem]
Judge whether there is a leap year in the given year