LeetCode:9. Palindromic Number(Medium)

原题链接:https://leetcode.com/problems/palindrome-number/description/

1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1)

2. 注意:负数不是回文!!因为前面有负号!注意整数溢出问题。

3. 思路:依然采用取余取整的方法

 1 package com.huiAlex;
 2 
 3 public class PalindromeNumber3 {
 4     public static void main(String[] args) {
 5         PalindromeNumber3 pn = new PalindromeNumber3();
 6         System.out.println(pn.isPalindrome(-12321));
 7         System.out.println(pn.isPalindrome(-2147483648));
 8         System.out.println(pn.isPalindrome(2147447412));
 9     }
10 
11     public boolean isPalindrome(int x) {
12         // 负数不是回文!!!
13         if (x < 0) {
14             return false;
15         }
16         int num = x;
17         System.out.println(num);
18         int result = 0;
19         while (num != 0) {
20             if (result > (Integer.MAX_VALUE - num % 10) / 10) {
21                 return false;
22             }
23             result = result * 10 + num % 10;
24             num = num / 10;
25         }
26         if(x>0){
27 
28         }else {
29             x = -x;
30         }
31         if (result == x) {
32             return true;
33         } else {
34             return false;
35         }
36     }
37 }

 

转载于:https://www.cnblogs.com/huiAlex/p/8036243.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值