【LeetCode算法-9】Palindrome Number
2023-09-14 09:00:41 时间
LeetCode第9题
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?
这题正好用到上一题的方法
代码:
class Solution {
public boolean isPalindrome(int x) {
if(x<0){
return false;
}else{
int y = 0;
int z = x;
while(x/10!=0){
y*=10;
y+=x%10;
x/=10;
}
y=y*10+x;
if(y==z){
return true;
}else{
return false;
}
}
}
}
负数都不成功,所以直接报false
相关文章
- leetcode 之Linked List Cycle(24)
- Java实现 LeetCode 810 黑板异或游戏 (分析)
- Java实现 LeetCode 780 到达终点(逻辑题)
- Java实现 LeetCode 699 掉落的方块(线段树?)
- Java实现 LeetCode 587 安装栅栏(图算法转换成数学问题)
- Java实现 LeetCode 587 安装栅栏(图算法转换成数学问题)
- Java实现 LeetCode 495 提莫攻击
- Java实现 LeetCode 60 第k个排列
- 【LeetCode算法-58/66】Length of Last Word/Plus One
- 【LeetCode算法-53】Maximum Subarray
- 【LeetCode算法-27】Remove Element
- 【LeetCode算法-21】Merge Two Sorted Lists
- 【LeetCode算法-20】Valid Parentheses
- 【栈&队列】LeetCode 394. 字符串编码【中等】
- LeetCode(27): 移除元素
- LeetCode(17):电话号码的字母组合
- leetcode 167. 两数之和 II - 输入有序数组----双指针篇六,二分篇二
- leetcode算法第5题
- leetcode算法第7题
- 【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】
- [LeetCode] 647. 回文子串 ☆☆☆(最长子串、动态规划、中心扩展算法)
- leetcode算法之动态规划总结(11种DP类型,70道全部搞懂)——总结非常全面
- 【Leetcode刷题Python】生词本单词整理
- 算法笔记:前缀和+哈希表(leetcode题解例)
- LeetCode刷题训练 && LeetCode算法和数据结构学习(一)