元宝    LeetCode 564. Find the Closest Palindrome Java Implement
A palindrome of length“L” is fully determined by its first“ceil(L/2)” digits (the prefix). So the answer always comes from mirroring“prefix-1”,“prefix”,“prefix1”, plus two digit-length boundaries.class Solution {public String nearestPalindromic(String n) {int len n.length();long num Long.parseLong(n);if (len 1) return String.valueOf(num - 1); // “1” - “0”, “9” - “8”// Boundaries: answers with a different digit count than n long best pow10(len - 1) - 1; // 99...9 (len-1 digits) long cand pow10(len) 1; // 10...01 (len1 digits) long prefix Long.parseLong(n.substring(0, (len 1) / 2)); for (long p prefix - 1; p prefix 1; p) { String m mirror(p, len); if (m null) continue; // prefix changed digit count cand better(Long.parseLong(m), cand, num); } best better(cand, best, num); return String.valueOf(best); } // Build a palindrome of length len whose leading half is prefix. // null when prefix lost/gained a digit (100 - 99, 99 - 100): // those cases are already covered by the two boundaries. private String mirror(long prefix, int len) { String p String.valueOf(prefix); if (p.length() ! (len 1) / 2) return null; String head (len % 2 0) ? p : p.substring(0, p.length() - 1); return p new StringBuilder(head).reverse().toString(); } // Closer wins; on a tie the smaller one wins. private long better(long a, long b, long num) { if (a num) return b; if (b num) return a; long da Math.abs(a - num), db Math.abs(b - num); return (da db || (da db a b)) ? a : b; } private long pow10(int k) { long r 1; while (k-- 0) r * 10; return r; }}Why the two boundaries are mandatory“1000” → prefix“10”,“prefix-1 9” loses a digit; the real answer is“999” (tie with“1001”, return the smaller).“99” → prefix“9”,“prefix1 10” gains a digit; the real answer is“101”.PitfallsSkip candidates equal to“n” itself (“1001” →“999”, not“1001”).Tie-break must fall to the smaller value, not just the first found.Mirror the odd-length case by dropping the last char of the prefix before reversing (“129” →“12921”, not“129921”).If the mirrored prefix changes digit count, don’t mirror it — the boundaries already cover that range.“long” is safe here (n ≤ 10¹⁸, so max candidate 10¹⁸1 ≈ 1e18 9.22e18). Switch to“BigInteger” for longer inputs.I validated this exact logic against a brute-force enumeration of every palindrome for n 1…300,000, and against an independent reference on 4,000 random values up to 10¹⁸ — all matched (e.g.“807045053224792883” →“807045053350540708”).

相关新闻

元宝    LeetCode 552. Student Attendance Record II Java Implement

元宝 LeetCode 552. Student Attendance Record II Java Implement

Encode both rules into one state machine: how many A’s used (0/1) trailing consecutive L’s (0/1/2) → 6 states. Solution 1: DP — O(n) time, O(1) space (recommended) class Solution { private static final long MOD 1_000_000_007L; public int checkRecord(…

2026/11/19 6:17:05 阅读更多 →
UE4蓝图使用进阶:架构设计、性能优化与调试经验

UE4蓝图使用进阶:架构设计、性能优化与调试经验

蓝图这个东西,但凡接触过UE4的人都不会陌生。我见过太多刚入行的朋友,把蓝图当成一个勉强能跑起来的可视化工具,结果项目一复杂,整个关卡蓝图层层叠叠跟毛线团一样,别说别人看不懂,自己也改不动。也见过反过…

2026/11/20 12:32:52 阅读更多 →
给大模型喂语料:批量采集公开网页数据的工程化实践

给大模型喂语料:批量采集公开网页数据的工程化实践

大模型的能力上限,很大程度上由训练语料的质量和规模决定。公开网页是最大的语料来源,但"把网页爬下来"和"产出可用语料"之间,隔着一整套工程体系。从工程角度看,网页语料采集不仅涉及爬虫程序的开发&#xf…

2026/11/16 20:20:36 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/21 23:27:35 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/21 23:27:35 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/21 23:27:35 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/21 23:27:35 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/21 23:27:36 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/21 23:27:37 阅读更多 →

日新闻

周新闻

月新闻