[Leetcode] 564. Find the Closest Palindrome 解题报告

Java实现 LeetCode 564 寻找最近的回文数(今天要GG在这道题了 头晕+题难(((φ(◎ロ◎;)φ)))) 564. 寻找最近的回文数 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身)。 “最近的”定义为两个整数差的绝对值最小。 示例 1: 输入: “123” 输出: “121” 注意: n 是由字符串表示的正整数,其长度不超过18。 如果有多个结果,返回最小的那个。 PS: 可以证明以下结论: 如果n 的前半部分是整数N,那么它的解一定是 以下三者之一: N-1 和 N-1的回文组成的数字。 N 和 N的回文组成的数字 N+1 和 N+1 的回文组成的数字 class Solution { public String mirroring(String s) { 立即下载

题目:

Given an integer n, find the closest integer (not including itself), which is a palindrome.

The 'closest' is defined as absolute difference minimized between two integers.

Example 1:

Input: "123"
Output: "121"

Note:

  1. The input n is a positive integer represented by string, whose length will not exceed 18.
  2. If there is a tie, return the smaller one as answer.

思路:

和n最接近的回文数根据位数划分,则只有3种可能:

1:比n多1位的数,例如对于99而言,距离它最近的回文数就是101;

2:比n少1位的数,例如对于101, 距离它最近的回文数就是99;

3:和n位数一样,并且其后一半是n的前一半的翻转的数。这样的数又可以分为3种:1)中间数和n相同的,例如123的最近回文数就是121;2)中间数比n大1,例如189的最近回文数就是191;3)中间数比n小1,例如920的最近回文数就是919。

所以只要把这5个数分别计算出来,然后比较哪一个和n最接近即可。

代码:

class Solution {
public:
    string nearestPalindromic(string n) {
        int l = n.size();
        set<long> candidates;
        // biggest, one more digit, 10...01
        candidates.insert(long(pow(10, l)) + 1);
        // smallest, one less digit, 9...9 or 0
        candidates.insert(long(pow(10, l - 1)) - 1);
        // the closest must be in middle digit +1, 0, -1, then flip left to right
        long prefix = stol(n.substr(0, (l + 1) / 2));
        for ( long i = -1; i <= 1; ++i ) {
            string p = to_string(prefix + i);
            string pp = p + string(p.rbegin() + (l & 1), p.rend());
            candidates.insert(stol(pp));
        }
        long num = stol(n), minDiff = LONG_MAX, diff, minVal;
        candidates.erase(num);                  // cannot be itself
        for ( long val : candidates ) {
            diff = abs(val - num);
            if ( diff < minDiff ) {
                minDiff = diff;
                minVal = val;
            }
        }
        return to_string(minVal);
    }
};
损失函数大汇总(六十一)(Signed Distance Map Loss附公式推导与代码) 摘要 本文介绍了Signed Distance Map (SDM) Loss,一种将形状先验直接融入分割任务的方法。SDM将每个像素表示为到目标边界的带符号距离(内部为负,外部为正),相比二值mask能更连续地表达形状几何信息。SDM Loss的核心思想是让网络学习这个隐式形状场,通过可微的Heaviside函数近似将SDM转换为分割结果。文中重点分析了一种product-based regression loss,它能同时关注距离数值和内外符号的正确性,对边界区域尤为敏感。SDM Loss通常与传统的分割 阅读详情

相关推荐

从3D相机数据到三维模型:Halcon实战深度图、亮度图与点云转换全流程

本文详细解析了如何利用Halcon将3D相机采集的深度图、亮度图转换为三维点云模型的完整流程。通过实战案例分享,涵盖数据格式处理、点云生成方法(SDK接口与纯Halcon实现)、三维模型可视化优化等关键技术,帮助工程师解决工业检测中的实际难题,提升三维重建精度与效率。

weixin_42525005的博客 394

564. Find the Closest Palindrome (Hard)

原题目:   Given an integer n, find the closest integer (not including itself), which is a palindrome.   The ‘closest’ is defined as absolute difference minimized between two integers.Example 1: Input: "

JJLight502 2013

ABB机器人与西门子PLC、工业相机联合实现无序抓取的完整调试方案

将此方案导入系统后,逐步执行调试,确保各环节信号正常触发,即可实现高效无序抓取!

CHUXUEZHE8210的博客 1992

LeetCode每日一题(2217. Find Palindrome With Fixed Length)

Given an integer array queries and a positive integer intLength, return an array answer where answer[i] is either the queries[i]th smallest positive palindrome of length intLength or -1 if no such palindrome exists. A palindrome is a number that reads the

wangjun861205的博客 281

保姆级教程:用CellOracle 0.10.13从单细胞数据构建基因调控网络(附完整代码)

本文提供了一份详细的CellOracle 0.10.13使用指南,帮助用户从单细胞数据构建基因调控网络。通过Python接口,即使是生物信息学新手也能快速掌握数据准备、KNN插补、GRN计算及可视化等关键步骤。文章包含完整代码和实用技巧,助力单细胞转录组数据分析。

weixin_42563415的博客 721

Halcon频域滤波实战:5分钟搞定傅里叶变换与功率谱分析(附完整代码)

本文提供Halcon频域滤波的实战指南,重点讲解如何利用傅里叶变换与功率谱分析快速解决图像处理难题。通过核心算子fft_image和power_real,演示了从空间域转换到频域、分析功率谱以定位周期性噪声,并构造滤波器进行精准去除的完整流程,附有可直接运行的代码示例。

docker8compose的博客 215

leetcode 564. 寻找最近的回文数

1 题目描述 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身)。 “最近的”定义为两个整数差的绝对值最小。 示例 1: 输入: “123” 输出: “121” 注意: n 是由字符串表示的正整数,其长度不超过18。 如果有多个结果,返回最小的那个。 2 解题思路 太痛苦了,要考虑的情况好多,做出来也没什么成就感。基本上提交后根据错误进行修改,一直改对的。再也不想做这类题了。 上图...

fxnfk 1578

RK3588 部署 YOLO26-Pose 人体姿态检测:Windows Docker 环境全流程

YOLO26-Pose 在 RK3588 上的部署流程是 PC 端 ONNX 导出 → RKNN 转换(INT8 量化)→ 板端推理。INT8 量化后 YOLO26s-Pose 约 35ms,YOLO26n-Pose 约 15ms,可以覆盖实时姿态检测的需求。后处理是 Python 实现的常见瓶颈——如果帧率不达标,先用 npy 保存模型输出在 PC 上验证解析逻辑的正确性,确认无误后再迁移到 C++ 实现。

m0_64661770的博客 542

算法练习之 564.寻找最近的回文数

题目 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身)。 “最近的”定义为两个整数差的绝对值最小。 示例 1: 输入: “123” 输出: “121” 注意: n 是由字符串表示的正整数,其长度不超过18。 思路分析 一个数字附近的回文数有很多个,想要寻找最近的一个回文数,我们不可能通过遍历其所有回文数来达到目的。所以这时候就需要给这个数的相近回文数一个范围,查找这个范围内的回文数,然...

ywloveb的博客 1753

web渗透学习手册

依据多年的渗透测试项目经验及个人理解,对漏洞分类

渗透之路,攻防之间皆学问 1万+

Windows 11 安装 Miniforge,配置国内源

本文详细介绍了在 Windows 11 系统中安装 Miniforge 的完整流程,包括下载、安装选项设置、配置国内镜像源(如清华大学源)以加速包管理,并启用 mamba 提升依赖解析与安装速度。同时涵盖 conda 与 mamba 的基本使用验证、PowerShell 初始化及环境提示符优化,帮助用户快速搭建高效、开源、免费的 Python 科学计算环境。

寒水馨 · 轻量级技术文档 · hanshuixin.org 4510

LeetCode 564. 寻找最近的回文数题解

564. 寻找最近的回文数题解 题目来源:564. 寻找最近的回文数 2022.03.02 每日一题 每日一题专栏地址:LeetCode 每日一题题解更新中❤️💕

小明不要写Bug的博客 386

手把手教你学simulink(31.17)--双向DCDC电源场景示例:基于simulink 的双向DCDC应用:车载充电器的pi控制

通过引入PI控制器,系统可以更精确地控制电压和电流,确保充电和放电过程的稳定性和高效性。

MHD0815的博客 205

LeetCode //C - 564. Find the Closest Palindrome

【代码】LeetCode //C - 564. Find the Closest Palindrome。

Made in Code 1733

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

564. Find the Closest Palindrome

564. Find the Closest Palindrome方法1:易错点 Given an integer n, find the closest integer (not including itself), which is a palindrome. The ‘closest’ is defined as absolute difference minimized between tw...

wilzxu的博客 214

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

leetcode 564. Find the Closest Palindrome(java)

题目: Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'closest' is defined as absolute difference minimized between two integers. example: Input

Ellenfeifei的博客 1023

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

leetcode 564. Find the Closest Palindrome

Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'closest' is defined as absolute difference minimized between two integers. Example 1: Input: "123" ...

cmy203的博客 152

【LeetCode】564. Find the Closest Palindrome——java实现

564. 寻找最近的回文数 题目描述: 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身)。 “最近的”定义为两个整数差的绝对值最小。 示例 1: 输入: “123” 输出: “121” 注意: n 是由字符串表示的正整数,其长度不超过18。 如果有多个结果,返回最小的那个。 题目难度: hard 题目思路: 1、要求一个数的回文,首先想到...

GZY_BUPT的博客 618

[LeetCode] 564. Find the Closest Palindrome 深入浅出讲解和代码示例

1、汇总概要 xx 2、题目 Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'closest' is defined as absolute difference minimized between two integers.

karen0310的专栏 1212

LeetCode #564 - Find the Closest Palindrome

题目描述: Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'closest' is defined as absolute difference minimized between two integers. Example 1: Input: ...

LawFile的博客 218

LeetCode 564: Find the Closest Palindrome(python)

用O(1)的算法,找到距离输入数字最近的回文数字。如果出现两个距离相同,则输出较小的回文数字。

marlonlyh的博客 1867

LeetCode 564. Find the Closest Palindrome(查找最近的回文数,C++)

题目 Given an integer n, find the closest integer (not including itself), which is a palindrome. The ‘closest’ is defined as absolute difference minimized between two integers. Example 1: Input: "123" O...

Efan_W的博客 288

LeetCode 564. Find the Closest Palindrome

 折腾了一天多,从完全自己想轮子全是bug,到借鉴别人的思路,再踩无数string的坑,算是有些收获吧。。可惜只超过了10%的人。。。。 class Solution { public: string Mirror(string n) { auto len = n.size(); string res = n.substr(0,len/2); ...

啥都不会的博客 249

【细节实现 回文串12】LeetCode 564. Find the Closest Palindrome

LeetCode 564. Find the Closest Palindrome 本博客转载自:http://www.cnblogs.com/grandyang/p/6915355.html 这道题给了我们一个数字,让我们找到其最近的回文数,而且说明了这个最近的回文数不能是其本身。比如如果给你个131,那么就需要返回121。而且返回的回文数可能位数还不同,比如当n为100的时候,我们就应该返...

Allenlzcoder的博客 403

【leetcode】564. Find the Closest Palindrome

题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的。要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1,2和3对称把3变成2,得到1221,看起来好像没问题了。可是如果n=1283,按照这个方法得到的结果是1221,而预期的结果应该是1331,所以需要考虑到进位和退位的问题。这就有点复杂了,简单...

weixin_33858249的博客 112
上一篇: [Leetcode] 563. Binary Tree Tilt 解题报告
下一篇: [Leetcode] 565. Array Nesting 解题报告
魔豆Magicbean
博客等级 码龄19年 146粉丝 742原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值