当前位置:网站首页>Leetcode-9. Palindrome Numbber
Leetcode-9. Palindrome Numbber
2022-06-11 07:07:00 【Eistert】
subject
Palindrome Number
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
For example, 121 is a palindrome while 123 is not.
Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -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: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-231 <= x <= 231 - 1
Follow up: Could you solve it without converting the integer to a string?
solution
Method 1 : String inversion
public class PalindromeNumber9 {
public static void main(String[] args) {
int a = 121;
boolean palindrome = Solution.isPalindrome(a);
System.out.println(palindrome);
}
/** * Reverse string * * @author aiwenwen * @date 11:08 2022/6/10 **/
static class Solution {
static public boolean isPalindrome(int x) {
String revBefore = String.valueOf(x);
StringBuffer strUtil = new StringBuffer();
strUtil.append(revBefore);
String revAfter = strUtil.reverse().toString();
if (revBefore.equals(revAfter)) {
return true;
}
return false;
}
}
}
Method 2 : Reverse half number
/** * Reverse half number * * @author aiwenwen * @date 11:08 2022/6/10 **/
static class Solution2 {
static public boolean isPalindrome(int x) {
/** * A special case : * As mentioned above , When x<0 when ,x Not palindrome . * similarly , If the last digit is 0, To make the number palindrome , * The first digit should also be 0 * Only 0 Satisfy this attribute **/
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
/** * * When the number length is odd , We can go through revertedNumber / 10 Remove numbers in the middle . * for example , When the input is 12321 when , stay while At the end of the loop we can get x = 12, revertedNumber = 123, * Because the number in the middle does not affect palindrome ( It is always equal to itself ), So we can simply remove it . **/
boolean b = x == revertedNumber;
boolean b1 = x == revertedNumber / 10;
return b || b1;
}
}
Complexity analysis
Time complexity :O(logn), For each iteration , We'll divide the input by 10, So the time complexity is zero O(logn).
Spatial complexity :O(1). We just need constant space to hold a number of variables .
Reprint
author :LeetCode-Solution
link :https://leetcode.cn/problems/palindrome-number/solution/hui-wen-shu-by-leetcode-solution/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- Janus feature草稿
- Object. Specific implementation and difference between create() and new
- Practice: how to reasonably design functions to solve practical problems in software development (I)
- saltstack部署lnmp
- 服务器调参实录
- 【Matlab图像融合】粒子群优化自适应多光谱图像融合【含源码 004期】
- Starting from scratch (V) realize bullet positioning and animation
- Drawing with qpainter
- First day of database
- [并发进阶]——线程池总结
猜你喜欢

News web page display

Open source cartoon server mango

Saltstack deployment LNMP

WPF data binding (IV)
![[Xunwei dry goods] opencv test of Godson 2k1000 development board](/img/94/312bb1f0d5e8d49506f659ad23cd3a.jpg)
[Xunwei dry goods] opencv test of Godson 2k1000 development board

Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation

Drawing with qpainter

Heartless sword Chinese English bilingual poem 001 Love

Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically

【LeetCode】-- 17.电话号码的字母组合
随机推荐
Luogu p1091 chorus formation (longest ascending subsequence)
一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试
Listen to the left width of the browser to calculate the distance
Flutter 内外边距
[并发进阶]——线程池总结
微信小程序开发(原生和uniapp)DOM标签对比介绍
Summary of string processing skills III
The difference between TCP and UDP
Drawing with qpainter
554. brick wall
1734. arrangement after decoding XOR
Object. Specific implementation and difference between create() and new
Latex various arrows / arrows with text labels / variable length arrows
Starting from scratch (V) realize bullet positioning and animation
【迅为干货】龙芯2k1000开发板opencv 测试
WPF data binding (IV)
Open source cartoon server mango
1266_ Implementation analysis of FreeRTOS scheduler startup code
[advanced concurrency] - thread pool summary
教育专家王中泽老师:家庭教育重在自己成长