# No.009 Palindrome Number

### 9. Palindrome Number

• Total Accepted: 136330
• Total Submissions: 418995
• Difficulty: Easy

Determine whether an integer is a palindrome. Do this without extra space.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

思路：

本题解题很简单，首先判断负数和0，然后我们计算得到最大基数，如果x为一个两位数，则基数base=100，三位数则为1000等，然后我们每次分别取这个数的最高位和最低位进行比较，如果不相等，则直接返回false，相等则去掉最左边和最右边的数后进行下一轮比较。代码如下：

``` 1 public boolean isPalindrome(int x) {
2     if(x < 0){
3         return false ;
4     }
5     if(x == 0 ){
6         return true ;
7     }
8
9     int base = 1 ;
10     while(x/base >= 10){
11         base *= 10 ;
12     }
13
14     while(x != 0){
15         int leftDigit = x/base ;
16         int rightDigit = x%10 ;
17         if(leftDigit != rightDigit){
18             return false ;
19         }
20         x = x%base/10 ;
21         base /= 100 ;
22     }
23     return true ;
24 }```

138 篇文章49 人订阅

0 条评论

## 相关文章

3496

### Socket与Http方式解析发送xml消息封装中间件jar包

最近项目代码中太多重复的编写Document,不同的接口需要不同的模板，于是重写提取公共部分打成jar包，方便各个系统统一使用~

1473

1151

1514

### Java_数据交换_JAXB_用法入门

JAXB（Java Architecture for XML Binding) 是一个业界的标准，是一项可以根据XML Schema产生Java类的技术。该过程...

1043

### 聊聊storm tuple的序列化

storm-2.0.0/storm-client/src/jvm/org/apache/storm/executor/ExecutorTransfer.java

794

### Code forces 719A Vitya in the Countryside

A. Vitya in the Countryside time limit per test:1 second memory limit per test:2...

3466

### Educational Codeforces Round 44 (Rated for Div. 2)A. Chess Placing

You are given a chessboard of size 1 × n. It is guaranteed that n is even. The c...

1172

3074

### 深入分析Spring MVC中RequestBody与ResponseBody

在SpringMVC中，可以使用@RequestBody和@ResponseBody两个注解，分别完成请求报文到对象和对象到响应报文的转换。在Sprin...

2341