元宝 LeetCode 564. Find the Closest Palindrome Java Implement

发布时间:2026/10/16 8:53:45
元宝    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”).

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询