当前位置:网站首页>leetcode-9:回文数
leetcode-9:回文数
2022-07-05 05:46:00 【菊头蝙蝠】
题目
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
- 例如,121 是回文,而 123 不是。
示例 1:
输入:x = 121
输出:true
示例 2:
输入:x = -121
输出:false
解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入:x = 10
输出:false
解释:从右向左读, 为 01 。因此它不是一个回文数。
解题
方法一:双指针
class Solution {
public:
bool isPalindrome(int x) {
string s=to_string(x);
int left=0,right=s.size()-1;
while(left<right){
if(s[left]!=s[right]) return false;
left++;
right--;
}
return true;
}
};
边栏推荐
猜你喜欢
7. Processing the input of multidimensional features
【Jailhouse 文章】Jailhouse Hypervisor
Palindrome (csp-s-2021-palin) solution
Sword finger offer 05 Replace spaces
Introduction et expérience de wazuh open source host Security Solution
【Jailhouse 文章】Look Mum, no VM Exits
Sword finger offer 04 Search in two-dimensional array
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
智慧工地“水电能耗在线监测系统”
CF1637E Best Pair
随机推荐
Detailed explanation of expression (csp-j 2021 expr) topic
Hang wait lock vs spin lock (where both are used)
Daily question 1688 Number of matches in the competition
二十六、文件系统API(设备在应用间的共享;目录和文件API)
7. Processing the input of multidimensional features
浅谈JVM(面试常考)
One question per day 1765 The highest point in the map
使用Electron开发桌面应用
Configuration and startup of kubedm series-02-kubelet
Daily question 1984 Minimum difference in student scores
【Jailhouse 文章】Look Mum, no VM Exits
SAP method of modifying system table data
Dichotomy, discretization, etc
Developing desktop applications with electron
Individual game 12
【云原生】微服务之Feign自定义配置的记录
CF1637E Best Pair
884. Uncommon words in two sentences
Introduction and experience of wazuh open source host security solution
Collection: programming related websites and books