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 ...
随机推荐
- centos6.5 yum安装postgresql9.3
rpm -ivh http://download.postgresql.org/pub/repos/yum/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-2.n ...
- Java学习总结1
1. 断点调试 a:定位(设置断点) b:启动调试 c:单步执行 观察变量(F5单步执行 F6单步跳过)d:修改2 static 静态 静态成员,为类的所有对象共享 在静态方法中,只能 ...
- VIM 与 系统剪切版
1, 查看 vim 是否支持 clipboard 功能 $ vim --version | grep clipboard 2, 如果有 +clipboard 则跳过这一步; 如果显示的是 -clipb ...
- Element类型
除了document,element类型也算是最常用的类型 Element节点有以下特征: nodeType 值为1 nodeName 元素的标签名 nodeValue 值为null parentNo ...
- 20155205 2016-2017-2《Java程序设计》课程总结
20155205 2016-2017-2<Java程序设计>课程总结 目录 一.每周作业链接汇总 二.实验报告链接汇总 三.博客中的经验与收获 - 自认为写得最好一篇博客是?为什么? - ...
- error C2143: syntax error : missing ';' before 'type'
许久之前,就想看看C和C++,看看这两种神奇的编程语言,但是一直被这样或者那样的琐事给耽搁了(狂喷自己的拖延症). 今天翻开自己的移动硬盘找到一本古老的书籍:<The C Programming ...
- spring 事务的传播特性
1.声明式事物中,一个类serviceA的方法test1()调用另一个类serviceB的方法test2() 要是serviceB的test2()事务配置在xml文件中为REQUIRED,又在此方法上 ...
- 1.7.6方法stop()与java.lang.threadDeath异常
调用stop方法时会抛出java.lang.ThreadDeath异常,但一般情况下这个异常不需要显示的捕捉 package com.cky.thread; /** * Created by edis ...
- (转)Memcached深度分析
转自:http://jwen.iteye.com/blog/1123991 memcached是高性能的分布式内存缓存服务器.一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态W ...
- linux 修改ip 地址
1./etc/sysconfig/network-scripts/ifcfg-网卡 如果是新网卡 自己写配置文档 ip a 即可查看网卡名字 (这是eno16777736) BOOTPROTO= dh ...