题意:

给一个数字n 求离n最近(且不等)的回文串 存在多个答案返回最小的

首先很容易想到

将数字分为两段,如 12345 -> 123/45,然后将后半段根据前面的进行镜像重置 123/45 -> 12321

那,如果数字刚好是回文串,就把前半段-1就好了

但是存在以下例外,就是当前半段 +1 或 -1 就会造成进位

10999

10901-10999 = -98

11011 - 10999 = -12

可以发现是因为9存在的进位,同样0也可能,所以对前半段进行 +1 和 -1 两种处理,然后选择差比较小的那个。

代码写的比较乱= =

class Solution {
public:
string nearestPalindromic(string n) {
typedef long long ll;
if (n == "10" || n == "11") return "9"; int l = n.size();
int half = (l + 1) / 2;
string left = n.substr(0, half);
string right = n.substr(half); // 情况1: 直接镜像翻转
string ans1 = (l & 1) ? left + rev_str(left).substr(1) : left + rev_str(left);
// 如果和原数字相同则不可用
ll diff1 = ans1 == n ? stoll(n) : abs(stoll(ans1) - stoll(n)); // 情况2: -1
string left_sub_1 = to_string(stoll(left) - 1);
string rleft_sub_1 = rev_str(left_sub_1);
string ans2;
if (left_sub_1.size() < half) {
ans2 = (l & 1)
? left_sub_1 + rleft_sub_1
: left_sub_1 + "9" + rleft_sub_1;
}
else {
ans2 = (l & 1)
? left_sub_1 + rleft_sub_1.substr(1)
: left_sub_1 + rleft_sub_1;
}
ll diff2 = abs(stoll(ans2) - stoll(n)); // 情况3: +1
string left_add_1 = to_string(stoll(left) + 1);
string rleft_add_1 = rev_str(left_add_1);
string ans3;
if (left_add_1.size() > half) {
ans3 = (l & 1)
? left_add_1 + rleft_add_1.substr(2)
: left_add_1 + rleft_add_1.substr(1);
}
else {
ans3 = (l & 1)
? left_add_1 + rleft_add_1.substr(1)
: left_add_1 + rleft_add_1;
}
ll diff3 = abs(stoll(ans3) - stoll(n)); if (diff2 <= diff1 && diff2 <= diff3) return ans2;
if (diff1 <= diff2 && diff1 <= diff3) return ans1;
return ans3;
} string rev_str(string a) {
string b(a);
reverse(b.begin(), b.end());
return b;
}
};

LeetCode 564. Find the Closest Palindrome (构造)的更多相关文章

  1. leetcode 564. Find the Closest Palindrome

    leetcode564题目地址 Given an integer n, find the closest integer (not including itself), which is a pali ...

  2. 【leetcode】564. Find the Closest Palindrome

    题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...

  3. 564. Find the Closest Palindrome

    Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...

  4. 乘风破浪:LeetCode真题_016_3Sum Closest

    乘风破浪:LeetCode真题_016_3Sum Closest 一.前言      这一次,问题又升级了,寻找的是三个数之和最靠近的某个数,这是非常让人难以思考的,需要把三个数相加之后和最后给的目标 ...

  5. Leetcode:1008. 先序遍历构造二叉树

    Leetcode:1008. 先序遍历构造二叉树 Leetcode:1008. 先序遍历构造二叉树 思路 既然给了一个遍历结果让我们建树,那就是要需要前序中序建树咯~ 题目给的树是一颗BST树,说明中 ...

  6. [LeetCode] Find the Closest Palindrome 寻找最近的回文串

    Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...

  7. [Swift]LeetCode564. 寻找最近的回文数 | Find the Closest Palindrome

    Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'clo ...

  8. Codeforces Round #185 (Div. 2) C. The Closest Pair 构造

    C. The Closest Pair Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/p ...

  9. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  10. 【一天一道LeetCode】#16. 3Sum Closest

    一天一道LeetCode系列 (一)题目: Given an array S of n integers, find three integers in S such that the sum is ...

随机推荐

  1. Appium Appium Python API 中文版

    1.contextscontexts(self): Returns the contexts within the current session. 返回当前会话中的上下文,使用后可以识别H5页面的控 ...

  2. Mac端MySQL安装教程

    Mac端: 1.安装 **        首先进入官网:**https://dev.mysql.com/downloads/mysql/ 根据自身情况是M系列芯片(ARM版)还是Intel系列芯片(x ...

  3. sftp文件上传下载方法

    随着信息化.数字化的发展,企业对数据安全及应用安全意识普遍加强,在数据文件传输过程中,一般建议使用sftp协议进行文件传输,sftp文件操作脚本如下: sftp操作主要有三种方式,分别是sftp客户端 ...

  4. 机器学习:详解是否要使用端到端的深度学习?(Whether to use end-to-end learning?)

    详解是否要使用端到端的深度学习? 假设正在搭建一个机器学习系统,要决定是否使用端对端方法,来看看端到端深度学习的一些优缺点,这样就可以根据一些准则,判断的应用程序是否有希望使用端到端方法. 这里是应用 ...

  5. adb对安卓app进行抓包(ip连接设备)

    adb对安卓app进行抓包(ip连接设备) 一,首先将安卓设备的开发者模式打开,提示允许adb调试 二,自己的笔记本要和安卓设备在同一个网段下(同连一个WiFi就可以了) 三,在笔记本上根据ip来连接 ...

  6. 【Redis】05 持久化

    持久化概述 Redis提供了不同的持久性选项: 1.RDB持久性按指定的时间间隔执行数据集的时间点快照. 2.AOF持久性会记录服务器接收的每个写入操作,这些操作将在服务器启动时再次播放,以重建原始数 ...

  7. 实用英语:英文中经常出现“ i.e.、e.g.、etc."缩写究竟啥意思?

    地址: https://mp.weixin.qq.com/s?__biz=MzIyMzU5NDE1Mw==&mid=2247487840&idx=1&sn=013992683c ...

  8. conda中安装GCC/G++

    参考: https://www.5axxw.com/questions/content/h0e4te ========================================== 一般在lin ...

  9. PyTorch显存机制分析

    参考: ======================================================= 在pytorch中有几个关于显存的关键词: 在pytorch中显存为缓存和变量分 ...

  10. Vue-购物车实战

    computed 计算属性 正则 css部分 [v-cloak] { display : none ; } table{ border : lpx solid #e9e9e9 ; border- co ...