当前位置:网站首页>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
边栏推荐
- Microservices (distributed architecture)
- 使用栈来解决”迷你语法分析器“的问题
- Redis data server / database / cache (2022)
- Summary of interface automation interview questions for software testing
- Numpy stack function
- Onenet connection process
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- SQL statement - DDL
- Creating wechat voucher process with PHP
- 对.NET未来的一点感悟
猜你喜欢

樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統

宁德时代地位不保?

Processes and threads

MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure

Small companies don't make formal offers

Blazor University (12)组件 — 组件生命周期

MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator

C# 11 的这个新特性,我愿称之最强!

L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)

全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
随机推荐
JS relearning
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (10)
TP5 inherits base and uses the variables in base
Introduction to ACM [TSP problem]
The space between the left and right of the movie ticket seats is empty and cannot be selected
MAUI初体验:爽
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Summary of software test interview questions
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
C# 11 对 ref 和 struct 的改进
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
Encapsulation of ele table
Introduction and use of openfeign component
Summary of interface automation interview questions for software testing
ASP.NET 6 中间件系列 - 自定义中间件类
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
The difference between encodeuri and encodeuricomponent
Realize QQ login with PHP
Load view Caton
Typescript Learning Guide