[LeetCode]判断回文数(Palindrome Number)

[Leetcode #9]Palindrome Number 判断一个整数是否是回文数 原题地址:https://leetcode.com/problems/palindrome-number/ 题目要求是:给定一个整数,判断是否是回文数。所谓回文就是正过来读和倒过来读都一样,例如12321是回文数,1221也是回文数。 另外,这个题目还有个很奇怪的要求:不能使用额外空间。个人理解这里的“额外空间”应该是指不能把整数转成成字符串,而不是说一个变量都不许用。。。(如果转成字符串再用 阅读详情

题目来源:Leetcode(www.leetcode.com)

Question:

Determine whether an integer is a palindrome. Do this without extra space.

Solution:
First, the problem statement did not specify if negative integers qualify as palindromes. Does negative integer such as -1 qualify as a palindrome? Finding out the full requirements of a problem before coding is what every programmer must do. For the purpose of discussion here, we define negative integers as non-palindromes.

The most intuitive approach is to first represent the integer as a string, since it is more convenient to manipulate. Although this certainly does work, it violates the restriction of not using extra space. (ie, you have to allocate ncharacters to store the reversed integer as string, where n is the maximum number of digits). I know, this sound like an unreasonable requirement (since it uses so little space), but don’t most interview problems have such requirements?

Another approach is to first reverse the number. If the number is the same as its reversed, then it must be a palindrome. You could reverse a number by doing the following:

int reverse(int num) {
  assert(num >= 0);   // for non-negative integers only.
  int rev = 0;
  while (num != 0) {
    rev = rev * 10 + num % 10;
    num /= 10;
  }
  return rev;
}

This seemed to work too, but did you consider the possibility that the reversed number might overflow? If it overflows, the behavior is language specific (For Java the number wraps around on overflow, but in C/C++ its behavior is undefined). Yuck.

Of course, we could avoid overflow by storing and returning a type that has larger size than int (ie, long long). However, do note that this is language specific, and the larger type might not always be available on all languages.

We could construct a better and more generic solution. One pointer is that, we must start comparing the digits somewhere. And you know there could only be two ways, either expand from the middle or compare from the two ends.

It turns out that comparing from the two ends is easier. First, compare the first and last digit. If they are not the same, it must not be a palindrome. If they are the same, chop off one digit from both ends and continue until you have no digits left, which you conclude that it must be a palindrome.

Now, getting and chopping the last digit is easy. However, getting and chopping the first digit in a generic way requires some thought. Please think your solution out before you peek on the solution below.

bool isPalindrome(int x) {
  if (x < 0) return false;
  int div = 1;
  while (x / div >= 10) {
    div *= 10;
  }        
  while (x != 0) {
    int l = x / div;
    int r = x % 10;
    if (l != r) return false;
    x = (x % div) / 10;
    div /= 100;
  }
  return true;
}

Alternative Solution:
A recursive solution (if extra stack space does not count as extra space), which is pretty neat too.

bool isPalindrome(int x, int &y) {
  if (x < 0) return false;
  if (x == 0) return true;
  if (isPalindrome(x/10, y) && (x%10 == y%10)) {
    y /= 10;
    return true;
  } else {
    return false;
  }
}
bool isPalindrome(int x) {
  return isPalindrome(x, x);
}

(LeetCode)Palindrome Number -- 判断回文数 阅读详情

相关推荐

Aruba CX交换机VSX配置实战:如何避免脑裂导致的网络中断(含详细命令)

本文提供Aruba CX交换机VSX配置的实战指南,重点讲解如何通过构建高可靠的ISL链路聚合与独立隔离的Keepalive链路,有效避免网络脑裂风险。文章包含详细的命令步骤,从基础配置到高级故障排查,帮助网络工程师构建坚如磐石的高可用核心网络,确保业务流量零中断。

weixin_29231725的博客 172

leetcode:9.判断回文数 --------三种方法

目录题目解法一:转为字符串逆置法解法二:高低位同时消除法解放三:后半部分逆置判断 题目 9.回文数 给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。 示例 1: 输入:x = 121 输出:true 示例 2: 输入:x = -121 输出:false 解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 示例 3: 输入:x

Zhou000815的博客 603

OpenCV透视变换实战:5分钟搞定歪斜证件照矫正(附完整代码)

本文详细介绍了如何使用OpenCV的透视变换技术快速矫正歪斜证件照,包含完整的Python代码实现。通过特征检测、坐标计算和矩阵变换三个核心步骤,5分钟内即可将手机拍摄的歪斜图像自动矫正为专业级证件照,大幅提升图像处理效率。

weixin_29094979的博客 482

判断回文数(三种解法)—— Leetcode(9)

判断回文数(三种解法)—— Leetcode(9) Leetcode链接:9. Palindrome NumberPalindrome Number,即判断是否为回文数,并且题目要求不能使用额外的空间。

qwerty200696的博客 1万+

基于ST32 DAC7311数模转换器驱动代码及电路设计完整版

基于ST32的DAC7311模数转换器的电路设计及驱动代码完整版

qq_39626643的博客 3720

VINS-Fusion实战:从零搭建无人机视觉SLAM系统(附ROS配置全流程)

本文详细介绍了如何从零开始为无人机搭建基于VINS-Fusion的视觉SLAM系统。内容涵盖硬件选型(重点推荐双目相机与IMU搭配)、ROS环境配置、传感器标定、核心参数调优以及实际飞行测试与问题排解全流程,旨在帮助开发者实现稳定、精确的无人机自主定位与导航。

wasm7browser的博客 214

leetcode-判断是否回文数字

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。

1073960086的博客 384

STM32L452与FPGA通信翻车?手把手教你关闭I2C时钟延展避坑(附OVR错误解决方案)

本文深入解析STM32L452与FPGA在I2C通信中因时钟延展功能导致的常见问题,提供关闭I2C时钟延展的详细配置方法(NOSTRETCH模式),并针对可能出现的OVR错误给出Combined模式解决方案。通过实战案例和波形分析,帮助工程师快速排查和解决嵌入式系统中的I2C兼容性问题。

e3f4g5的博客 216

[LeetCode]回文数(Palindrome Number)

题目描述 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 示例 1:输入: 121输出: true示例 2:输入: -121输出: false解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 示例 3:输入: 10输出: false解释: 从右向左读, ...

weixin_34112208的博客 158

博途S7-1500T 使用工艺对象驱动液压轴(含SimaHydTO库)

利用工艺对象控制液压轴位置含PLC控制和仿真程序文档资料下载地址如下:液压比例伺服阀的相关应用知识请参看下面的博客文章:液压位置闭环控制(比例伺服阀应用)_RXXW_Dor的博客-优快云博客。

RXXW_Dor的博客 2900

大数据领域存算分离:教育行业的数据应用探索

在当今数字化时代,教育行业积累了海量的数据,如学生的学习记录、考试成绩、在线学习行为等。如何高效地存储、管理和分析这些数据,以挖掘其潜在价值,为教育决策、教学改进和学生个性化发展提供支持,成为教育行业面临的重要挑战。大数据领域的存算分离技术为解决这一问题提供了新的思路和方法。本文旨在探索存算分离技术在教育行业的数据应用,分析其原理、优势和实际应用场景,为教育行业的数据管理和分析提供参考。本文共分为十个部分。第一部分介绍背景,包括目的和范围、预期读者、文档结构概述和术语表;

AGI×大数据,开启智能时代的认知跃迁;解码AGI,赋能数据驱动的智能革命。 813

leetcode回文数字

Palindrome Number: 当一个数字倒序和正序相同时,返回true,否则false 最简单的办法就是将数字转换成为string类型,然后对两个string进行比较,即位比较字符串是否相等 class Solution(object): def isPalindrome(self, x): “”" :type x: int :rtype: bool “”" a = str(x)[::1]...

jgx04的博客 384

Python 控制 Keysight N9010B/N9020/N9030 频谱仪

为什么要实现频谱仪的控制呢?起因是因为我司RF的同事们需要对通信模块进行GSM的带内杂散进行测试,由于该测试的频点涉及900多个,所以急需开发一款自动化测试工具。该工具需同时实现对无线通讯仪和频谱仪的控制,这篇文章主要介绍一下频谱仪的控制!做通信的应该对频谱仪都不陌生,不过我之前做的都是软件相关行业,所以我对频谱仪其实还是很陌生的;不过好在我的同时们都很热心,加上仪器的文档,一段时间下来,也算是熟悉了一些。

xgh1951的博客 1万+

LeetCode 9. Palindrome Number回文数判断

LeetCode 9. Palindrome Number回文数判断 Determine whether an integer is a palindrome. Do this without extra space.

My Blog 359

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【LeetCode】9. Palindrome Number 回文数判断

一、概述 给一个数,判断是否是回文数。 简单炸了,如果不要求时间复杂度的话。 二、分析 1、我的思路 int转string,reverse然后判断是否相等。 我真的好奇这些题目的难度梯度,太差劲了。 当然如果要求不用string还是有点麻烦的。 2、较好的思路 首先判断x,如果x是负数或者10的整数倍,那么直接返回false。 然后就是关键了: 判断前一半和后一半是否相等...

四轩茶屋 239

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

【LeetCode】9. Palindrome Number回文数判断

题目:           Determine whether an integer is a palindrome. Do this without extra space. 理解:          在不额外使用空间的情况下判断一个数是否为回文数,即各个位上的数字对称,如:12321。 分析:              由于不能使用额外的空间,因此不能使用将整数转换为

糖梦梦是女侠 2053

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

leetcode Palindrome Number 判断回文数

判断一个数是不是回文数,想不到什么高端做法,逐位分解然后再合成倒序的数,在判断和前数是不是相等,注意,负数都不是回文数。 class Solution { public: bool isPalindrome(int x) { if(x < 0) return false; int temp; int mark;

293

LeetCode Palindrome Number回文数判断

采用两种方法实现,其实时间复杂度都一样;第一种直接比较对位的数;#include&lt;math.h&gt; #include&lt;iostream&gt; using namespace std; bool isPalindrome(int x) { if (x &lt; 0) return false; int x_len = 1; while (x /int (pow(10,x...

u010259177的博客 271

Leetcode 9 Palindrome Number 判断一个数是否为回文数

题目描述:Determine whether an integer is a palindrome. Do this without extra space.判断一个数是否是回文数(正着读和反着读一样),负数没有回文数思路:1.取a=输入数。令b=0,是a的倒转形式每次b乘以10,以表示b向左移动一次,取a的最后一位数字加到b中,做完后a除以10,表示下一次可以取出a 的左面一位。假设a=121,...

yuweiming70的博客 388

leetcode 9. Palindrome Number(判断是不是回文数)

题目要求: 判断一个整数是不是回文数, 返回 true or false。 回文数的定义:在数学中 设n是一任意自然数。若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数。例如,若n=1234321,则称n为一回文数;但若n=1234567,则n不是回文数。(来源百度百科回文数) 示例: Example 1 Input : 121 Output : true Example 2 ...

爱喝舒化奶的博客 618

[Leetcode] Palindrome number 判断回文数

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting th...

weixin_30849591的博客 122

LeetCode 9. Palindrome Number 判断是否为回文数

Determine whether an integer is a palindrome. Do this without extra space. 题目解释:判断一个数是否为回文数,不能使用额外的空间 注意: 1.负数不能是回文数   2.如果转化为字符串进行处理就利用了额外的空间(If you are thinking of converting the integer to stri

gddxmmxf的博客 342

LeetCode第九题—— Palindrome Number(判断回文数)

题目描述 Determine whether an integer is a palindrome. An integerisapalindrome when itreads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: fal...

weixin_34040079的博客 207

【Leetcode】9. 判断回文数(Palindrome Number)

Leetcode - 9 Palindrome Number (Easy) Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. public boolean i...

厉兵秣码 260

【LeetCode 9. Palindrome Number】(回文数判断)

题目链接 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false...

feng_zhiyu的博客 437
上一篇: 一道关于signed和unsigned的微软面试题
下一篇: [LeetCode] Rotate List
Tim_WT
博客等级 码龄20年 2粉丝 30原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值