leetcode: Palindrome Number

palindrome_binaryreversal:回文和二元逆转 palindrome_binaryreversal 回文和二元逆转 立即下载

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 the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

思路

简单想来,就是把每一位都算出来,然后比较。关于那个“without extra space"的意思,我理解为使用O(1)的空间吧(貌似也不能这么说,毕竟整数的位数也没有大到哪里去,撑死10位)。
class Solution {
public:
    bool isPalindrome(int x) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
		if (x<0) return false;
		int xb=x;
		int numDigits=0;
		int xd=1;
		while(xb!=0)
		{
			numDigits++;
			xb=xb/10;
			if(xb!=0) xd*=10;
		}
		xb=x;
		for(int i=0;i<numDigits/2;i++)
		{
			if (xb/xd!=xb%10)
				return false;
			xb=xb-(xd+1)*(xb%10);
			xd/=100;
			xb=xb/10;
		}
		if (xb<=9 && numDigits%2==1)
			return true;
		else if (xb==0 && numDigits%2==0)
			return true;
		else
			return false;
    }
};
旁边的同学看到了,想出来了一个更好的方法,就是如下 大笑,很简单看一眼就明白:
class Solution {
public:
    bool isPalindrome(int x) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
		if (x<0) return false;
		if (x<10) return true;
		int r=0,xb=x;
		while(r<xb)
		{
			r=r*10+xb%10;
			xb=xb/10;
		}
		if(r==xb || (r/10==xb && xb!=0)) return true;
		else return false;
    }
};
就是有一点,不知道为什么leetcode给的运行时间前面的是240ms,后者是248ms
【MATLAB-app】csdn上第一套关于matlab appdesigner界面设计 系列“视频课”来啦~~ 写作时间: 2020-12-10 标题: 01_为什么推荐你学习appdesigner? 目录: 1.什么是appdesigner? 2.appdesigner能干什么,有什么好处? 3.怎么学习?保证你一周内能够学会! 4.我的课程总目录 5.重磅来袭(如何获取学习资料代码+文档+视频) 正文: 1.什么是appdesigner? 2.appdesigner能干什么,有什么好处? 3.怎么学习?保证你一周内能够学会! 4.我的课程总目录 5.重磅来袭(如何获取学习资料代码+文档+视频) THE END 阅读详情

相关推荐

chmod: changing permissions of `/u01/app/oracle/product/11.2.0/dbhome_1/bin/extjobO‘: Operation not

oracle 11.2.0.4安装psu 29141056,日志末尾抛出报错 Patching component oracle.ctx, 11.2.0.4.0... Applying sub-patch '29141056' to OH '/u01/app/oracle/product/11.2.0/dbhome_1' Patching component oracle.rdbms, 11.2.0.4.0... Patching component oracle.rdbms.rman, 11.2.0.

m0_37625564的博客 1833

【CodeForces 1251B --- Binary Palindromes】

【CodeForces 1251B --- Binary Palindromes】 Description A palindrome is a string t which reads the same backward as forward (formally, t[i]=t[|t|+1−i] for all i∈[1,|t|]). Here |t| denotes the length of ...

来日浅谈的博客 1379

小米万兆路由器里的Docker安装可道云(Kodexplorer)私有网盘

小米2022年12月份发布了万兆路由器,里面可以使用Docker。 今天尝试在小米的万兆路由器里安装[可道云](http://kodcloud.com/)(Kodexplorer)私有网盘。

engchina的专栏 1万+

leetcode:Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["a

binary_search的专栏 1594

【回归预测】GRNN广义回归神经网络-MATLAB

广义回归神经网络(GRNN)是一种基于径向基函数(RBF)的神经网络模型,主要用于非线性回归任务。与传统的回归分析方法相比,GRNN具有更强的非线性映射能力和学习速度,能够快速逼近复杂的非线性关系。输入层、模式层、求和层和输出层。输入层接收输入数据,模式层通过径向基函数将输入映射到高维空间,求和层对模式层的输出进行加权求和,最后输出层生成预测结果。GRNN的核心思想是利用高斯核函数计算输入与训练样本之间的相似度,然后根据相似度加权平均得到预测值。

2303_77200324的博客 1091

Tomcat源码:Acceptor与Poller、PollerEvent

前文中我们介绍到NioEndpoint的start方法启动了Acceptor与Poller这两个异步线程来处理连接请求,本文我们就来接着介绍这两个组件。目录前言一、Acceptor1、构造方法2、run方法LimitLatchNioChannel二、Poller与PollerEvent2、pollerregisterruneventsprocessKey。

CRUD工程师 2065

【力扣Leetcode题解系列之0009—Palindrome Number 回文数:深入剖析与拓展】

摘要: 本文详细解析了力扣第9题「回文数」的两种解法:字符串转换法和数字翻转法。字符串法通过比较字符判断回文,时间复杂度O(log₁₀n),空间复杂度O(log₁₀n);数字翻转法通过数学运算反转数字,空间优化至O(1)。文章还提供了Python/Java/C/C++/Go多语言实现,并拓展了判断任意进制回文数的问题,建议处理负数、进制转换和字符比较。两种解法中翻转数字法更优,尤其适合限制空间的情况。

youngerwang的博客 718

FPGA SPI驱动ADC避坑指南:以ADC128S022为例,详解时序设计与代码调试

本文以ADC128S022为例,详细解析FPGA通过SPI驱动ADC的时序设计与代码调试中的常见问题。从时钟分频计算、SPI模式选择到状态机实现,提供避坑指南和实战技巧,帮助工程师解决数据采集系统中的时序匹配难题。特别针对FPGA与ADC通信中的微妙差异,给出精确的Verilog代码示例和ModelSim仿真验证方法。

weixin_42522374的博客 458

LeetCode: 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 Explanation...

weixin_43957391的博客 301

【FreeSwitch开发实践】外呼网关配置(拨打电话)

FreeSwitch开发一个重要应用是外呼,所谓外呼就拨打真实电话或手机号呼叫,配合语音机器人可以实现智能客服的功能。外呼配置主要是配置外呼服务器,外呼服务器主要指SIP网关,每个外呼服务器就是一个SIP节点,FreeSwitch服务也可以是一个SIP节点。本文主要介绍了FreeSwitch外呼相关配置,主要有外呼配置的模式,包括注册模式和非注册模式网关加载的查看方法拨号中常见的呼出配置和呼入配置如果觉得有些帮助或觉得文章还不错,请关注一下博主,你的关注是我持续写作的动力。............

xiao2macf的专栏 1万+

Agent应用实践之二十九 - 设计模式:人机协同

本章讲解了一种比较常见的设计模式:人机协同。通过一些场景举例,同时使用AgentScope本身自带的能力实现的人机协同模式,最后通过源码分析了AgentScope中如何实现。通过了解人机协同模式对于实际工程使用也是非常有用的。

代码让AI扣 305

LeetCode题解:Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. 思路: 题目要求只能用O(1)的空间,所以不能考虑把它转化为字符串然后reverse比较的方法。 在提示中也提到了,如果考虑reverse number的方法,可能造成溢出。 那么只能选择分离数字的方

Magi Su的技术回收站 9993

手把手教你学Simulink——基于Simulink的固态变压器(SST)多级协同控制

摘要:本文详细介绍了基于Simulink的固态变压器(SST)多级协同控制方法。SST作为新一代智能电网核心设备,通过AC-DC整流、DC-DC隔离和DC-AC逆变三级架构实现电能高效转换。针对多级耦合难题,提出能量-功率协同控制策略:纵向解耦电压环与功率前馈,横向联动级间功率指令。在Simulink中搭建10kV/400V系统模型,实现直流母线电压波动<2%的动态性能。特别设计了负载突变时的三级协同机制,通过虚拟惯性算法和前馈控制实现快速响应。文章包含完整的建模步骤、控制推导和参数设置,为电力电子工

MHD0815的博客 195

LeetCode 9: Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.本题是判断一个数是否是回文数。代码如下:bool isPalindrome(int x) { int max = x; int min = 0; while(max &gt;0){ min...

哈哈的个人专栏 3618

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode: Palindrome Number

题目: Determine whether an integer is a palindrome. Do this without extra space.思路分析: 回文判断,题目提示不能使用直接转字符串,不能翻转数字。 那只能一个一个取出数字进行比较了。C++参考代码:class Solution { public: bool isPalindrome(int x) {

给永远比拿愉快 1203

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

leetcode:Palindrome Number

一、 题目 试确定一个整数是否为回文数。并不使用额外的空间。 提示: 负整数可能是回文数吗?(例如 -1) 如果你想要将整数转换成字符串,那么你注意到不能使用额外的空间的限制。 可能你尝试翻转整数,但是,如果你已经解决这个问题“逆向整型”,你要知道,颠倒整数可能会溢出的情况。那么你会如何处理这样的情况呢? 要有解决这个问题的一种更通用的方法。 二、 分析 了解题目的意思后,其实问题

恰同学少年 970

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

leetcode 117: Palindrome Number

Palindrome NumberJan 4 '12 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinki

西施豆腐渣 1216

Leetcode9: Palindrome Number

一个回数的题目.......点进去的时候觉得很简单,数字转字符串,分分钟搞定。。。。 结果一看补充说明,懵逼了,不准使用额外空间。。注意整数溢出问题 不准使用额外空间,那就不能重新给string申请空间了。。。那怎么做呢。。。 代码如下 class Solution(object): def isPalindrome(self, x): """ :type x: int

bestallen的博客 642

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 converti...

dbu50120的博客 126

Leetcode 9:Palindrome Number

原题链接:Palindrome Number   乍一看,这题挺简单,直接将 int 转化为 string 处理,几行代码完事 class Solution { public: bool isPalindrome(int x) { string s = to_string(x); string s1 = s; std:...

wayne17的博客 270

Leetcode:Palindrome Number

题目出处:https://leetcode.com/problems/palindrome-number/ 题目:Determine whether an integer is a palindrome. Do this without extra space. 翻译:判断一个整型数是否是回文数 思路:将整型数转化为字符串,依次比较首尾 代码: public class Solution

yangyao_iphone的专栏 533

《leetCode》:palindrome Number

题目描述Determine whether an integer is a palindrome. Do this without extra space.题目大意:即检测一个数是否为回文数,不开辟额外的空间思路比较简单 第一步:统计x的长度。 第二步:比较x的最高位和最低位是否相等 第三步:如果第二步的结果相等,则将x的最高位和最低位的数字去掉变为新的x,重复第一步和第二步即可。

wojiushimogui的博客 769

leetcode:字符串之Valid Palindrome && Palindrome Number

leetcode:字符串之Valid Palindrome 题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, ”A man, a plan, a canal: Panama” is a p

what_lei的博客 372

leetcode: 9. Palindrome Number

第一次写的: 结果超时 bool isPalindrome(int x) { if(x<0){ return false; } if(x==0){ return true; } int mi=1; while((x/static_cast&...

wadfji的博客 172
上一篇: webpy的Hello World
下一篇: leetcode: Container With Most Water
binary_search
博客等级 码龄14年 6粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值