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 ...
随机推荐
- python输出格式对齐问题
采用.format打印输出时,可以定义输出字符串的输出宽度,在 ':' 后传入一个整数, 可以保证该域至少有这么多的宽度. 用于美化表格时很有用. >>> table = {'Goo ...
- 859. Buddy Strings
class Solution { public: bool buddyStrings(string A, string B) { int lenA=A.length(); int lenB=B.len ...
- ICMP协议、DNS、ARP协议、ping、DHCP协议
1.ICMP协议 1)ICMP协议,即:网络控制消息协议(Internet Control Message Protocol) 2)ICMP是网络层协议,因为ICMP报文是装在IP数据报中,作为它的数 ...
- SVN安装配置与使用
http://www.cnblogs.com/skyway/archive/2011/08/10/2133399.html http://www.cnblogs.com/lidabo/archive/ ...
- python,函数
numpy.tile():参考https://www.jianshu.com/p/4b74a367833c numpy.argsort:argsort()里面传入参数只有数组时,返回的是数组值从小到大 ...
- C#创建、设置和安装Windows服务
文章大部分内容转自:http://www.cnblogs.com/greatandforever/archive/2008/10/14/1310504.html:和:http://www.cnblog ...
- Keras预测股票
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Nov 18 21:22:29 201 ...
- HDU1864 最大报销额
Description 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上,单项物品的价值不得超过6 ...
- xslt 和一个demo
https://www.w3.org/1999/XSL/Transform Specifications The XSLT language has three versions which are ...
- Swift:用UICollectionView整一个瀑布流
本文的例子和Swift版本是基于Xcode7.2的.以后也许不知道什么时候会更新. 我们要干点啥 用新浪微博的Open API做后端来实现我们要提到的功能.把新浪微博的内容,图片和文字展示在colle ...