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"
Output: "121"
Note:
- The input n is a positive integer represented by string, whose length will not exceed 18.
- 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:
564. Find the Closest Palindrome的更多相关文章
- leetcode 564. Find the Closest Palindrome
leetcode564题目地址 Given an integer n, find the closest integer (not including itself), which is a pali ...
- 【leetcode】564. Find the Closest Palindrome
题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...
- [LeetCode] Find the Closest Palindrome 寻找最近的回文串
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- [Swift]LeetCode564. 寻找最近的回文数 | Find the Closest Palindrome
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- 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 ...
- leetcode 学习心得 (3)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 517. Super Washing Machines You have n super washing ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
随机推荐
- 2018.10.30 NOIP模拟 有环无向图(dijkstra+巧妙建图)
传送门 建图巧妙啊. 对于每个点的出边,我们将它们排序之后依次连边. 这样可以把O(m2)O(m^2)O(m2)的边数变成O(m)O(m)O(m)的了. 连的权值就是max(edgemax(edgem ...
- mac os 卸载android studio 从新安装遇到的一些问题
http://blog.csdn.net/elonspace/article/details/51800949 google中国论坛 http://www.android-studio.org lin ...
- java常用设计模式二:工厂模式
1.简单工厂模式(静态工厂方法模式) 抽象实例: public interface People { void talk(); } 具体实例: public class Doctor implemen ...
- python中的\n、\r与\b
python中使用print函数,有时候会使用end参数来控制字符输出效果,这时候\n.\r与\b就派上用场了. \n 代表换行,也就是从本行换到下一行 \r 代表回车,也就是回到本行最开始的位置,从 ...
- php,ajax上传文件,多文件上传
HTML <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- tp5,thinkphp5,隐藏index.php,隐藏入口文件
一.找到/public/.htaccess文件 Apache: <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews R ...
- 实现1sym转换成2个sym送给CVI(VGA数据)
CVI的时序如下 :de指示数据有效. 从下面的程序看,同步码的长度不会影响对有效数据的判断.同步码的作用更多的是用于计算行及一行的像素数目.方案一: 1 module vga_1sym_2_2sym ...
- npp 文本编辑器 开源
下载地址 https://notepad-plus-plus.org/download/v6.8.4.html
- sql_id VS hash_value
有没有发现,v$session,v$sql,v$sqlarea,v$sqltext,v$sql_shared_cursor等试图连接的时候经常会用到hash_value,sql_id,但是他们2个之间 ...
- zend studio 修改字体大小
第一步:进入设置窗口 windows -> preferences第二步:进入修改字体的选项卡. General -> Appearance -> Colors and ...