博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Palindrome Number
阅读量:4150 次
发布时间:2019-05-25

本文共 980 字,大约阅读时间需要 3 分钟。

class Solution {//compare both the left most and the right most digit, //if not equal return falsepublic:	bool isPalindrome(int x) {		// Start typing your C/C++ solution below		// DO NOT write int main() function		if(x < 0) return false;		int dvsNow = 1;		while(x/dvsNow >= 10)			dvsNow *= 10;		while(x != 0)		{			int l = x/dvsNow;			int r = x%10;			if(l != r) return false;			x = (x%dvsNow)/10;//take off both the left most and the right most digit			dvsNow /= 100;		}		return true;	}};

second time

class Solution {public:    bool isPalindrome(int x) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if(x < 0) return false;                long long target = x;        //if(target < 0) target = -target;         long long reverse = 0;        long long curNum = target;        while(curNum != 0)        {            reverse = 10*reverse+curNum%10;            curNum /= 10;        }                return reverse == target;    }};

转载地址:http://fmxti.baihongyu.com/

你可能感兴趣的文章
实验4-1 逻辑量的编码和关系操作符
查看>>
实验5-2 for循环结构
查看>>
实验5-3 break语句和continue语句
查看>>
实验5-4 循环的嵌套
查看>>
实验5-5 循环的合并
查看>>
实验5-6 do-while循环结构
查看>>
实验5-7 程序调试入门
查看>>
实验5-8 综合练习
查看>>
第2章实验补充C语言中如何计算补码
查看>>
深入入门正则表达式(java) - 命名捕获
查看>>
使用bash解析xml
查看>>
android系统提供的常用命令行工具
查看>>
【Python基础1】变量和字符串定义
查看>>
【Python基础2】python字符串方法及格式设置
查看>>
【Python】random生成随机数
查看>>
【Python基础3】数字类型与常用运算
查看>>
Jenkins迁移jobs
查看>>
【Python基础4】for循环、while循环与if分支
查看>>
【Python基础5】列表和元组
查看>>
【Python基础6】格式化字符串
查看>>