leetcode564题目地址

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:

  1. The input n is a positive integer represented by string, whose length will not exceed 18.
  2. 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的更多相关文章

  1. 【leetcode】564. Find the Closest Palindrome

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

  2. 564. Find the Closest Palindrome

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

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

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

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

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

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

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

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

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

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

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

  8. [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 ...

  9. LeetCode(16)题解--3Sum Closest

    https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...

随机推荐

  1. 写个dump_stack【转】

    转自:http://blog.chinaunix.net/uid-27714502-id-3434761.html 简单实现dump_stack 0.首先确保你能写个内核模块:打印"hell ...

  2. 設定 gpio 為 讀取用途,需注意的參數

    Schematic 解說 上面的 線路圖, R1 R2 只能有一個被接上, R3 R4 只能有一個被接上, 是使用 gpio 讀取 電壓 判斷為0 或是 1 這時的 gpio 設定,其中一個參數需設為 ...

  3. Win10打开照片提示“无效的注册表值”解决方法

    1.点开开始菜单,右键单击,选择“以管理员运行”[键盘win键+R]输入PowerShell. 2.输入Get-AppxPackage *photo* | Remove-AppxPackage后回车. ...

  4. 【bzoj2006】NOI2010超级钢琴

    补了下前置技能…… 题意就是求一段区间的权值和前k大的子序列的和. 把段扔进优先队列 每次拿出来之后按照所选择的j进行分裂 #include<bits/stdc++.h> #define ...

  5. C中级 MariaDB Connector/C API 编程教程

    引言 - 环境搭建 首先开始环境搭建. 主要在Window 10 + Visual Studio 2015 上构建使用 mariadb connector/c api 进行数据操作开发. 为什么选择在 ...

  6. 升级OS10.11系统后 Xcode6.4的变化少了个按钮 could not launch “Xcode” Xcode 插件安装

    升级OS10.11系统后 Xcode6.4的变化少了个按钮 could not launch “Xcode”  Xcode 插件安装 A:  升级10.11后Xcode 左上角模拟器选择菜单不在了   ...

  7. Sql中把datetime转换成字符串(CONVERT)(转)

    一.回顾一下CONVERT()的语法格式: CONVERT (<data_ type>[ length ], <expression> [, style]) 二.这里注重说明一 ...

  8. hit-testing机制介绍

    1.简介 寻找处理触摸事件的view的过程为hit-testing,找到的能够处理触摸事件的view叫做hit-test view. 2.机制介绍 假设下图为我们的手机屏幕,当我们假设点击了view ...

  9. 数据库简述(以MySQL为例)

    一.数据库中的概念 1.数据库是用户存放数据.访问数据.操作数据的存储仓库,用户的各种数据被有组织地存放在数据库中.可以随时被有权限的用户查询.统计.添加.删除和修改.可以说,数据库是长期存储在计算机 ...

  10. POJ 2912 Rochambeau(种类并查集+枚举)

    题目链接:http://poj.org/problem?id=2912 题目大意:n个人玩,玩石头剪刀布游戏,其中1人是裁判,剩下的n-1个人分为3组, 他们商量好了,相同组的人每次都出相同的手势,不 ...