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.

Approach #1: Logic. [Java]

class Solution {
public String nearestPalindromic(String n) {
Long num = Long.parseLong(n);
Long big = findHigherPalindrome(num+1);
Long small = findLowerPalindrome(num-1);
return Math.abs(num - small) > Math.abs(big - num) ? String.valueOf(big) : String.valueOf(small);
} Long findHigherPalindrome(Long limit) {
String n = Long.toString(limit);
char[] s = n.toCharArray();
int m = s.length;
char[] t = Arrays.copyOf(s, m);
for (int i = 0; i < m / 2; ++i) {
t[m-i-1] = t[i];
}
for (int i = 0; i < m; ++i) {
if (s[i] < t[i]) return Long.parseLong(String.valueOf(t));
else if (s[i] > t[i]) {
for (int j = (m-1) / 2; j >= 0; --j) {
if (++t[j] > '9') t[j] = '0';
else break;
}
for (int k = 0; k < m / 2; ++k) {
t[m-1-k] = t[k];
}
return Long.parseLong(String.valueOf(t));
}
}
return Long.parseLong(String.valueOf(t));
} Long findLowerPalindrome(Long limit) {
String n = Long.toString(limit);
char[] s = n.toCharArray();
int m = s.length;
char[] t = Arrays.copyOf(s, m);
for (int i = 0; i < m / 2; ++i) {
t[m-1-i] = t[i];
}
for (int i = 0; i < m; ++i) {
if (s[i] > t[i]) return Long.parseLong(String.valueOf(t));
else if (s[i] < t[i]) {
for (int j = (m - 1) / 2; j >= 0; --j) {
if (--t[j] < '0') t[j] = '9';
else break;
}
if (t[0] == '0') {
char[] a = new char[m-1];
Arrays.fill(a, '9');
return Long.parseLong(String.valueOf(a));
}
for (int k = 0; k < m / 2; ++k) {
t[m-1-k] = t[k];
}
return Long.parseLong(String.valueOf(t));
}
}
return Long.parseLong(String.valueOf(t));
}
}

  

Analysis:

We first need to find the higher palindrome and lower palidrome respectively. and return the one who has the least different with the input number.

For the higher palindrome, the lower limit is number + 1 while for the lower palindrome, the hight limit is number - 1.

One global solution to find a palindrome is to copy first half part of the array to the last half part, we regards this as standard palindrome.

We need to detect this standard palindrome belongs to higher one or the lower one. And other solutions will be based on this standard one.

For the higher palindrome, if the standard one belongs to higher, we just simply return it. Or we need to change it.

For example, stirng n is 1343, and the standard palindrome is 1331. to get the higher one form the standard palidrome, we start from the first 3, which is (n.length - 1) / 2. Add the number by 1, 9--> 1431) if the added result is not higher than 9, the changing process is finished, otherwise, continuously changing the number of previous index by i. After the changing process, we re-palidrome the string. (1431 --> 1441)

For the lower palidrom, similar idea. But we need to notice that when we decrease a number a number, and if the first character of the string is '0', we need to resize the array of n.length - 1, and fill in with '9'. (for example, n is '1000', the standard palidrome is '1001' (higher one), the lower one '0000' --> '999'.)

Reference:

https://leetcode.com/problems/find-the-closest-palindrome/discuss/102390/Java-solution-with-full-explaination

564. Find the Closest Palindrome的更多相关文章

  1. leetcode 564. Find the Closest Palindrome

    leetcode564题目地址 Given an integer n, find the closest integer (not including itself), which is a pali ...

  2. 【leetcode】564. Find the Closest Palindrome

    题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...

  3. [LeetCode] Find the Closest Palindrome 寻找最近的回文串

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

  4. [Swift]LeetCode564. 寻找最近的回文数 | Find the Closest Palindrome

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

  5. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

  8. leetcode 学习心得 (3)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 517. Super Washing Machines You have n super washing ...

  9. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

随机推荐

  1. mybatis 的查询某个字段的特定位数(模糊查询)

    获取特定的几位:1.取url字段后三位字符 select SUBSTRING(url, -3) from link; 2.取url字段前三位字符 select SUBSTRING(url, 3) fr ...

  2. 【Web】Sublime Text 3 安装+注册+汉化

    Sublime Text 介绍 Sublime Text 是一个代码编辑器,也是HTML和散文先进的文本编辑器.Sublime Text是由程序员Jon Skinner于2008年1月份所开发出来,它 ...

  3. linux_磁盘挂载

    mount -o loop 磁盘的位置 想要挂载的位置 磁盘卸载 umont 挂载的磁盘的详细位置 注意:磁盘卸载时你当前所在的路径不要在磁盘挂载的路径,应该其他与磁盘挂载路径不相干的路径下即可

  4. Hadoop3集群搭建之——虚拟机安装

    现在做的项目是个大数据报表系统,刚开始的时候,负责做Java方面的接口(项目前端为独立的Java web 系统,后端也是Java web的系统,前后端系统通过接口传输数据),后来领导觉得大家需要多元化 ...

  5. 2018.11.02 洛谷P2661 信息传递(拓扑排序+搜索)

    传送门 按照题意模拟就行了. 先拓扑排序去掉不在环上面的点. 剩下的都是简单环了. 于是都dfsdfsdfs一遍求出最短的环就行. 代码: #include<bits/stdc++.h> ...

  6. Latex插图操作

    1.竖排插入两张图 \begin{figure}[h] //放在当前位置 \centering \subfigure[A given traffic flow set]{ \includegraphi ...

  7. C# 中的委托(Delegate)

    委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托(Delegate)特别用于实现事件和回调方法.所有的委托(Delegate)都派生自 System.D ...

  8. dj 中间件

    中间件的概念 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用不好会影响到 ...

  9. laravel 5.1 使用Eloquent ORM 操作实例

    Laravel 的 Eloquent ORM 提供了更优雅的ActiveRecord 实现来和数据库的互动. 每个数据库表对应一个模型文件. 数据库配置 .env文件(也可以直接修改config/da ...

  10. [待完善]mycat使用(一)

    生产上的mycat已经投入使用,这次的应用场景是数据写入和查询都非常大的一个需求,而且经常出现多表join的查询 1.应用上线没多久出现大量慢查询: 分片键的选择率非常高,但没有建索引,在其上加上索引 ...