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 3389连接工具Rdesktop
简单使用 工作机换成战斗机了,改用ubuntu,原来的windows7上东西笔记多,还不想重装.用rdesktop来远程连接windows: sudo apt-get install rdesktop ...
- mysql之安装和配置(一)
环境 oracle linux7.3 数据库:MySQL-5.7.20 mysql的安装 先安装依赖的插件 yum install libaio 去官网下载mysql-5.7.20的tar.gz包: ...
- [How to] 使用Xib来创建view
1.简介 代码库 正如之前博客介绍的,xib可定义页面的某个部分,特别当此部分区域的view集中并且还有一些相互关联性(如隐藏等)是i特别适合使用xib来进行封装. 本文为[How to]使用自定义c ...
- tomcat远程调试参数备忘
tomcat远程调试,启动时添加参数: -server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,ser ...
- django的事务
在某些时候,你可能会在视图修改两张数据表.并且想让他们同时成功或者同时失败.这就是事务的原子性(atomicity).在django中应该怎么做呢? 详细可以参考官方文档:https://yiyibo ...
- resteasy json
https://www.cnblogs.com/toSeeMyDream/p/5763725.html
- 《java并发编程实战》读书笔记2--对象的共享,可见性,安全发布,线程封闭,不变性
这章的主要内容是:如何共享和发布对象,从而使它们能够安全地由多个线程同时访问. 内存的可见性 确保当一个线程修改了对象状态后,其他线程能够看到发生的状态变化. 上面的程序中NoVisibility可能 ...
- node修改全局环境路径 与 全局后出现sh:exe command not found
修改全局环境路径 当安装nodeJs时候需要修改全局环境的指向,先看看npm config get prefix 全局环境在哪里 然后执行更换命令,一个是主文件一个是缓存文件 npm config ...
- 画图工具除了echarts我使用highcharts
文件 :密码:xewx 官方文档教程地址:https://www.hcharts.cn/docs/basic-color 上手容易,api友好,尽管也有不尽人意的地方,但是它很适合我当前的项目,如果想 ...
- 3.spark streaming Job 架构和容错解析
一.Spark streaming Job 架构 SparkStreaming框架会自动启动Job并每隔BatchDuration时间会自动触发Job的调用. Spark Streaming的Job ...