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 ...
随机推荐
- 命令行创建KVM虚拟机
qemu命令创建虚拟机: qemu-img create -f qcow2 /home/ubuntu.img 20G qemu-system-x86_64 -m 2048 -enable-kvm ...
- [LabVIEW架构]ActorFramework(二)
前言 在上一个文章中,我们介绍了一下LabVIEW中AF的基本概念,本讲将以上一次的例子来讲解LabVIEW中的实现 正文 范例说明 假定两个人,一个作为老师,一个作为学生.学生每天早上给老师发送一封 ...
- golang相关问题
[转载][翻译]Go的50坑:新Golang开发者要注意的陷阱.技巧和常见错误[1] Golang作为一个略古怪而新的语言,有自己一套特色和哲学.从其他语言转来的开发者在刚接触到的时候往往 ...
- 解决Myeclipse编译不生成.class文件问题
1.Project --> clean... 如果该操作无效,请执行2. 2.Preferences -->Java -->Compliler -->Building --& ...
- 获取GUID的方法
uses QString; procedure TForm12.btn1Click(Sender: TObject); var g: TGUID; begin //方法1(Guid的Helper)-- ...
- elasticsearch更新license
Elasticsearch更新license: 初次安装Marvel,有30天的使用时间,当到期后,只保存7天的数据,所以需要注册申请一个license: 注册申请地址: https://regist ...
- WordPress 一键置顶文章(推荐用SM Sticky Clicky Star)
在 WordPress入门 之 发布新文章和管理文章 中,倡萌已经简单提到可以在文章编辑界面或者快速编辑界面设置置顶文章,但是如果你想在后台文章列表中添加一键置顶文章的功能,不妨试试 Quick St ...
- 前端读者 | 百度前端编码规范(CSS)
本文来自:百度FEX 1 前言 CSS作为网页样式的描述语言,在百度一直有着广泛的应用.本文档的目标是使CSS代码风格保持一致,容易被理解和被维护. 虽然本文档是针对CSS设计的,但是在使用各种CSS ...
- 使用css让文字两端对齐
text-align:justify; text-justify:distribute-all-lines; text-align-last:justify;可以让文字实现两端对齐
- 【剑指offer】面试题 57. 和为 S 的数字
面试题 57. 和为 S 的两个数字 题目一:和为 S 的两个数字 题目 输入一个递增排序的数组和一个数字 S,在数组中查找两个数,是的他们的和正好是 S,如果有多对数字的和等于 S, 输出两个数的乘 ...