leetcode腾讯精选009

本文介绍了一种判断整数是否为回文数的方法,通过将整数转换为字符串并比较其首尾字符来实现。代码使用C++编写,详细展示了如何通过数学计算避免字符串转换,提高效率。

题目:https://leetcode-cn.com/problems/palindrome-number/
代码:
class Solution {
public:
bool isPalindrome(int x) {

		bool res = true;
		if (x < 0)
		{
			return false;
		}
		
		int dig = log10(x);
		int index = 0;
		int rev;

		while (index < dig)
		{
			int a = pow(10, index + 1);
			int single_dig = (x % a)/pow(10,index);
			int t = pow(10, dig - index);
			int high_dig = (x / t)%10;
			if (single_dig != high_dig)
			{
				res = false;
				break;
			}
			index++;
			
		}

		return res;

	}

};

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值