当前位置:网站首页>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
边栏推荐
- 利用正反遍历来解决“字符的最短距离”问题
- Publish to NPM?
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- JS learning notes
- Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
- C# WPF UI框架MahApps切换主题
- tf. keras. layers. Inputlayer function
- MAUI初体验:爽
- 使用split来解决“最常见的单词”问题
- Use of MySQL command line client and common commands
猜你喜欢
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
Blazor University (11)组件 — 替换子组件的属性
Passing object type parameters through openfeign
Realize QQ login with PHP
Dynamic sequence table + OJ
最通俗易懂的依赖注入与控制反转
C#中切片语法糖的使用
Openfeign timeout setting
Source Generator实战
C# 11 的这个新特性,我愿称之最强!
随机推荐
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
Blazor University (12)组件 — 组件生命周期
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
L2-006 树的遍历(中后序确定二叉树&层序遍历)
[format] simple output (2)
Introduction to ACM [inclusion exclusion theorem]
ASP.NET 6 中间件系列 - 条件中间件
Basic SQL (VIII) data update operation practice
C# 读写二进制文件
c#语法糖模式匹配【switch 表达式】
使用两种方法来解决“最大回文数乘积”问题
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
Basic workflow of CPU
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
How to deploy a website with only a server and no domain name?
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
Restart redis
最通俗易懂的依赖注入之生命周期
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题