leetcode 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
给定一个字符串。输出离他最近的回文数字,本题中最近的意思是绝对值最小的。
回文应该很熟悉了把,就是以最中间的数字为对称点对称的数字。
思路:
1.理解一下可以发现,题目只有5个candidate数字。将最中间的数字+1,+0,-1然后构造回文数字,因为要尽可能里原来的数字近,只要将左边依次赋值给右边就好了。
这里可以构造出三个candidate。但是中间数字为0时,-1的时候会出现特殊情况;中间数字为9,+1的时候会出现特殊情况;这两种情况就是下面讨论的。
2.还有两种可能比较特别。如1001这种,就是中间数字为零的情况,上面的方法-1的情况构造出来就是99。但是答案明明是999。所以我们构造一个比原来位数小一位最大的回文,999作为candidate。 还有一种情况就是999的情况,答案应该是1001.上面的方法中+1的方法构造出来的是100001.显然不对。所以我们够一个比原来位数大一位最小的回文。这是另外两个candidates
其实思路只有一个,就是将中间数字+1,+0,-1的情况讨论,另外两个只是对数字越出极限的讨论整理。
注意:
1.题目中要求的回文是不能跟原来的数字一样的,也就是距离为零是不行的。
2.NOTE1中要求数字最长为18位,所以应该用long
3.距离一样取小的那个。
class Solution {
public:
//已知一半构造回文另一半的函数
string other(string p,int l){
string other="";
int len=p.length();
for(int i=len--(l&);i>=;i--)
other+=p[i];
return other;
}
string nearestPalindromic(string n) {
int l=n.length();
set<long> cand;
//构造比原来位数大一位的最小回文
cand.insert(long(pow(,l))+);
//构造比原来位数小一位的最大回文
cand.insert(long(pow(,l-))-);
long mid=stol(n.substr(,(l+)/));
//-1,+0,+1三种情况
for(int i=-;i<=;i++){
string p=to_string(mid+i);
string pp=p+other(p,l);
cand.insert(stol(pp));
} long num = stol(n), minDiff = LONG_MAX, diff, minVal;
//除去原来的数字
cand.erase(num);
for ( long val : cand ) {
diff = abs(val - num);
if ( diff < minDiff ) {
minDiff = diff;
minVal = val;
} else if ( diff == minDiff ) {
minVal = min(minVal, val);
}
}
return to_string(minVal);
}
};
ps:以前在32位机器上int,long的取值范围是一样的。现在64位机器上,int占4字节-2^31~2^31+1,long占8字节,long数据范围变为:-2^63~2^63-1。
leetcode 564. Find the Closest Palindrome的更多相关文章
- 【leetcode】564. Find the Closest Palindrome
题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...
- 564. Find the Closest Palindrome
Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...
- 乘风破浪:LeetCode真题_016_3Sum Closest
乘风破浪:LeetCode真题_016_3Sum Closest 一.前言 这一次,问题又升级了,寻找的是三个数之和最靠近的某个数,这是非常让人难以思考的,需要把三个数相加之后和最后给的目标 ...
- [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][Python]16: 3Sum Closest
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...
- 【一天一道LeetCode】#16. 3Sum Closest
一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...
- [LeetCode] Maximize Distance to Closest Person 离最近的人的最大距离
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...
- LeetCode(16)题解--3Sum Closest
https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...
随机推荐
- Linux 入门记录:七、fdisk 分区工具
一.fdisk分区工具 fdisk 是来自 IBM 的老牌分区工具,支持绝大多数操作系统,几乎所有的 Linux 发行版都装有 fdisk,包括在 Linux 的 resuce 模式下依然能够使用. ...
- 64_j1
JSCookMenu-2.0.4-13.fc26.noarch.rpm 13-Feb-2017 22:06 38098 Java-WebSocket-1.3.1-0.2.git58d1778.fc24 ...
- Development tools[重点]
Development tools yum groupinfo "Development tools" Loaded plugins: product-id, security, ...
- XMLDocument 方法中实现post发送消息
XMLDocument 方法中实现post发送消息
- php常见术语
什么是PHP? php是Hypertext Preprocessor的缩写,php是一种内嵌 HTML的脚本语言.PHP的独特语法混合了c,java和perl及PHP式的新语法.这门语言的的目标是让网 ...
- 封装ajax方法
function ajaxRequest(type, url, data, callback, loading, cache) { var ajaxConfig = { url: '', data: ...
- 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记10——三维天空的构建&三维粒子的实现&多游戏模型的载入
第23章 三维天空的构建 目前描述三维天空的技术主要包括三种类型,直接来介绍使用最广泛的模拟技术,详细的描述可以见作者的博文. 天空盒(Sky Box),即放到场景的是一个立方体.它是目前使用最广泛的 ...
- win10更换登陆背景和关闭锁屏
更换登陆背景图 用国外一个hacker写的工具,可在github上下载,或者下载我上传到百度网盘的备份.win10默认的登陆背景太难看了. 去掉锁屏图片 登陆界面其实本身就是一种锁屏,为什么还要单独搞 ...
- nodejs里的express自动刷新高级篇【转载】
搬运自[简书:http://www.jianshu.com/p/2f923c8782c8]亲测可用哦! 最近在使用express框架及mongodb,由于前端和后端代码修改后都需要实现自动刷新功能,刚 ...
- jQuery插件开发,jquery插件
关于jQuery插件的开发自己也做了少许研究,自己也写过多个插件,在自己的团队了也分享过一次关于插件的课.开始的时候整觉的很复杂的代码,现在再次看的时候就清晰了许多.这里我把我自己总结出来的东西分享出 ...