当前位置:网站首页>使用两种方法来解决“最大回文数乘积”问题
使用两种方法来解决“最大回文数乘积”问题
2022-04-23 03:02:00 【&小小白&】
十四、最大回文数乘积
14.1、题设要求
给定一个整数 n ,返回 可表示为两个 n 位整数乘积的 最大回文整数 。因为答案可能非常大,所以返回它对 1337 取余 。
示例 1:
输入:n = 2
输出:987
解释:99 x 91 = 9009, 9009 % 1337 = 987
示例 2:
输入: n = 1
输出: 9
提示:
1 <= n <= 8
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/largest-palindrome-product
14.2、解题思路
第一种解法:
先计算最大乘积,再将最大乘积的前半部分数字拿出并使用生成函数生成相应的回文数,然后将生成的回文数进行比较,若没有找到则将数字前半部分数字减一并重新生成新的回文数进行比较,依次循环下去;若找到则输出即可。
第二种解法:
因为此题的范围是固定且比较小的,直接打表即可。
14.3、算法
第一种算法:
class Solution {
public int largestPalindrome(int n) {
//如果n为1,则只有9最大
if (n == 1) {
return 9;
}
//定界限
long upper = (long) Math.pow(10,n) - 1;
long lower = upper/10 + 1;
//范围中的最大乘积
long maxNumber = upper * upper;
//计算最大数字的前半部分
long half = (long) (maxNumber/Math.pow(10,n));
//是否找到
boolean found = false;
//返回值
long result = 0;
//循环找对应的回文数
while (!found){
//调用生成函数
result = create(half);
//从高往低进行查找
for (long i = upper; i >= lower; i--) {
//i*i的值小于result的值
if (i * i < result){
break;
}
//result对i取余为0
if (result % i == 0){
found = true;
break;
}
}
//没找到,接着找前半部分下一位数字
half--;
}
//返回回文数并对1337取余
return (int) (result % 1337);
}
//生成函数
public long create(long number){
//将传来的数字前半部分与翻转生成的前半部分的数字拼接生成新的回文数
String str = number + new StringBuilder().append(number).reverse().toString();
return Long.parseLong(str);
}
}
参考视频:力扣up主郭郭
第二种算法:
class Solution {
public int largestPalindrome(int n) {
int[] palindromes = {
9,987,123,597,677,1218,877,475};
return palindromes[n - 1];
}
}
版权声明
本文为[&小小白&]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_52916408/article/details/124209836
边栏推荐
- tf. keras. layers. Timedistributed function
- ASP.NET 6 中间件系列 - 条件中间件
- 一套关于 内存对齐 的C#面试题,做错的人很多!
- Navicat failed to connect to Oracle Database: cannot load OCI DLL, 87: instant client package is
- [software testing] understand the basic knowledge of software testing
- [ncnn] - the meaning of - 23300 in param
- JS using the parameters of art template
- Encapsulation of ele table
- The usage of case when and select case when is very easy to use
- Onenet connection process
猜你喜欢

Passing object type parameters through openfeign

Error installing Mongo service 'mongodb server' on win10 failed to start
![Niuke white moon race 5 [problem solving mathematics field]](/img/be/ca059bd1c84eaaaefa3266f9119a6b.png)
Niuke white moon race 5 [problem solving mathematics field]

C#中切片语法糖的使用
![[format] simple output (2)](/img/24/64739f5e6bbd54bfa9fb78b8c53c94.png)
[format] simple output (2)

Detailed explanation of distributed things

腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!

Array and collection types passed by openfeign parameters

Dynamic sequence table + OJ

Summary of interface automation interview questions for software testing
随机推荐
Processes and threads
Error installing Mongo service 'mongodb server' on win10 failed to start
Blazor University (12)组件 — 组件生命周期
Regular object type conversion tool - Common DOM class
AOT和单文件发布对程序性能的影响
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
Microservices (distributed architecture)
Navicat premium import SQL file
Depth deterministic strategy gradient (ddpg)
Summary of interface automation interview questions for software testing
ASP.NET和ASP.NETCore多环境配置对比
The difference between encodeuri and encodeuricomponent
C# 11 的这个新特性,我愿称之最强!
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
Xamarin效果第二十二篇之录音效果
[learn junit5 from official documents] [II] [writingtests] [learning notes]
Wepy learning record
Passing object type parameters through openfeign
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
Service avalanche effect