LeetCode 564. Find the Closest Palindrome (构造)
题意:
给一个数字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 (构造)的更多相关文章
- leetcode 564. Find the Closest Palindrome
leetcode564题目地址 Given an integer n, find the closest integer (not including itself), which is a pali ...
- 【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:1008. 先序遍历构造二叉树
Leetcode:1008. 先序遍历构造二叉树 Leetcode:1008. 先序遍历构造二叉树 思路 既然给了一个遍历结果让我们建树,那就是要需要前序中序建树咯~ 题目给的树是一颗BST树,说明中 ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- java面试一日一题:在创建微服务时,是用RPC还是http
问题:请讲下在做微服务时,是使用RPC还是http 分析:该问题主要考察对RCP及http的理解,也关系到在进行微服务选型时的两大方向,dubbo和springCloud,都是RPC框架,但前者是RP ...
- Jenkins+docker 部署SpringCloud微服务
部署需要提前准备的环境:安装好Jenkins.docker.Maven.Jdk1.8.Git 说明:由于本例只说明如何部署,所以有关项目其他服务如nacos.mysql.redis.seata等默认已 ...
- Python和RPA网页自动化-发送邮件
以163邮箱为例,分别使用Python和RPA网页自动化发送邮件到指定邮箱 其中2个方法都需要用到163邮箱授权码,开启IMAP/SMTP服务即可得到授权码(POP3/SMTP服务不用开启) 1.py ...
- Jmeter函数助手31-changeCase
changeCase函数用于字符转换大小写. 字符串修改:填入需要转换的字符 更改案例模式UPPER(默认),LOWER,CAPITALIZE:不填默认UPPER,UPPER全部转换为大写,LOWER ...
- 【DataBase】MySQL 28 流程控制
一.分支结构 1.IF函数 语法: IF(表达式1, 表达式2, 表达式3) 类似三元运算符,表达式1返回True Or False True执行表达式2,False执行表达式3 IF实现多分枝结构 ...
- 【DataBase】MySQL 26 存储过程
一.概述 存储过程&函数,类似编程语言的方法 什么是方法? 完成特定功能的一组语句 方法的特点 1.可重用性 2.简化操作 二.存储过程[ Stored Procedures]: 一组预先编译 ...
- 【Vue】Re07 插槽Slot
一.插槽基本使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 【Redis】RCMD 04 List 列表
1.LPUSH 写入命令: LPUSH 键 值1 值2 值3 值4 ... 127.0.0.1:6379[12]> LPUSH LIST-1 1 2 3 4 5 (integer) 5 2. ...
- 说说中国高校理工科教育中的基础概念混乱问题——GPU是ASIC吗
在YouTube上看到这样一个视频: https://www.youtube.com/watch?v=7EXDp6c9n-Q&lc=Ugydwl8gppB5FWE8Y5V4AaABAg.9fc ...
- 解密prompt系列35. 标准化Prompt进行时! DSPy论文串烧和代码示例
一晃24年已经过了一半,我们来重新看下大模型应用中最脆弱的一环Prompt Engineering有了哪些新的解决方案.这一章我们先看看大火的DSPy框架,会先梳理DSPy相关的几篇核心论文了解下框架 ...