没有合适的资源?快使用搜索试试~ 我知道了~
LeetCode Palindrome Number解决方案
需积分: 0 0 下载量 101 浏览量
2024-01-23
23:05:29
上传
评论
收藏 11KB DOCX 举报
温馨提示
试读
2页
LeetCode Palindrome Number解决方案
资源推荐
资源详情
资源评论
Summary:
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?
Answer:
~~~
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
int count = 0;
int temp = x;
while (temp/10 >0) {
temp = temp / 10;
count++;
}
temp = x;
int result = 0;
for (int i = count; i >= 0; --i) {
result += (temp % 10) * pow(10,i);
temp = temp / 10;
}
if (result == x) return true;
else return false;
}
资源评论
句号(在IT行业摸爬滚打)
- 粉丝: 279
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功