当前位置:网站首页>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;
}
};
边栏推荐
- Web APIs DOM node
- Sword finger offer 58 - ii Rotate string left
- 剑指 Offer 04. 二维数组中的查找
- Sword finger offer 53 - ii Missing numbers from 0 to n-1
- Solution to the palindrome string (Luogu p5041 haoi2009)
- [practical skills] how to do a good job in technical training?
- 2017 USP Try-outs C. Coprimes
- Csp-j-2020-excellent split multiple solutions
- On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
- Introduction et expérience de wazuh open source host Security Solution
猜你喜欢
【Jailhouse 文章】Jailhouse Hypervisor
API related to TCP connection
R language [import and export of dataset]
The connection and solution between the shortest Hamilton path and the traveling salesman problem
【Jailhouse 文章】Look Mum, no VM Exits
剑指 Offer 05. 替换空格
Talking about JVM (frequent interview)
shared_ Repeated release heap object of PTR hidden danger
Solution to the palindrome string (Luogu p5041 haoi2009)
Brief introduction to tcp/ip protocol stack
随机推荐
Daily question - longest substring without repeated characters
API related to TCP connection
shared_ Repeated release heap object of PTR hidden danger
Wazuh開源主機安全解决方案的簡介與使用體驗
[practical skills] how to do a good job in technical training?
Bit mask of bit operation
A misunderstanding about the console window
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Daily question 1984 Minimum difference in student scores
2020ccpc Qinhuangdao J - Kingdom's power
Wazuh开源主机安全解决方案的简介与使用体验
常见的最优化方法
Time of process
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
剑指 Offer 53 - I. 在排序数组中查找数字 I
Mysql database (I)
剑指 Offer 53 - II. 0~n-1中缺失的数字
剑指 Offer 58 - II. 左旋转字符串
Sword finger offer 53 - I. find the number I in the sorted array