[leetcode] 9. Palindrome Number

网友投稿 605 2022-11-08

[leetcode] 9. Palindrome Number

[leetcode] 9. Palindrome Number

Description

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:

From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3: Input:

10

Output:

false

Explanation:

Reads 01 from right to left. Therefore it is not a palindrome.

Follow up: Coud you solve it without converting the integer to a string?

分析

题目的意思是:判断一个数是否是回文的,要求不能把数变为字符串。

把数变为字符串,但不符合题目要求;或者反转整个数都能求解,反转数可能溢出。这里直接求解,我们先比较最高位和最低位,然后相应的比较第二位和次高位,这样循环下去,思路很简答,这里需要求解base,base代表数量级,就是x的第二高的数量级,比如x=10001,base=1000。

代码

class Solution {public: bool isPalindrome(int x) { if(x<0){ return false; } vector v1; while(x){ v1.push_back(x%10); x=x/10; } int i=0; int j=v1.size()-1; while(i

参考文献

​​[编程题]palindrome-number​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于Jenkins+Maven+Gitea+Nexus搭建CICD环境的方式
下一篇:application installation failed android studio
相关文章

 发表评论

暂时没有评论,来抢沙发吧~