题意:

给一个数字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. Jmeter函数助手34-digest

    digest函数用于返回特定哈希算法的加密值. 算法摘要:填入算法,如MD2.MD5.SHA-1.SHA-224.SHA-256.SHA-384.SHA-512 String to be hashed ...

  2. DrawIO安装及基本使用教程

    1.DrawIO简介 DrawIO 是一款开源免费且功能强大的绘图工具,可以用于绘制流程图.组织结构图.网络图.UML图等各种类型的图表: DrawIO 支持多种文件格式,包括XML.PNG.SVG等 ...

  3. 【Vue】08 Webpack Part4 使用Vue

    新建一个目录创建Vue项目: cd .. mkdir webpack-vue cd webpack-vue 安装Vue cnpm install vue 保持之前的目录结构: 在Main.js中引用V ...

  4. Python的GDAL库绘制多波段、长时序遥感影像时间曲线图

      本文介绍基于Python中的gdal模块,对大量长时间序列的栅格遥感影像文件,绘制其每一个波段中.若干随机指定的像元的时间序列曲线图的方法.   在之前的文章中,我们就已经介绍过基于gdal模块, ...

  5. 【转载】pip install 使用多个.local环境

    原文地址: https://zhuanlan.zhihu.com/p/351468170 ================================ 我们都知道anaconda可以安装不同的py ...

  6. 如何在多台Linux系统主机上实现ssh免密访问——成公钥文件id_rsa.pub(数字签名RSA)

    假设共有三台Linux主机,为matser,slave1,slave2,现在要实现master主机可以ssh免密访问master主机自身以及slave1.slave2. 原理: 主机调用秘钥生成命令, ...

  7. stm32 F103C8T6 4x4矩阵键盘使用

    首先感谢 江科大 的stm32入门课程 受益匪浅.推荐有兴趣的朋友去看看. 先看看我用的矩阵键盘是啥样的(很常见的一种) 接线如图(其他型号根据自己需求接上GPIO口) 代码基于stm大善人的代码修改 ...

  8. 给我5分钟,保证教会你在vue3中动态加载远程组件

    前言 在一些特殊的场景中(比如低代码.减少小程序包体积.类似于APP的热更新),我们需要从服务端动态加载.vue文件,然后将动态加载的远程vue组件渲染到我们的项目中.今天这篇文章我将带你学会,在vu ...

  9. Django框架创建运行最小程序过程记录

    基于 python语言 Django web框架下 用pycharm创建,修改,运行 最简单程序.旨在过程 ========================================== 步骤一 ...

  10. 代码随想录Day20

    235. 二叉搜索树的最近公共祖先 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个 ...