Palindrome Number(回文)

 1 public class Solution{
 2     public boolean isPalindrome(int x) {
 3         //int count=1;
 4         int count1=0;
 5         int count2=0;
 6         int []bs=new int[100];
 7        if (x<0){//当输入的值为负的时候,就不是回文了
 8            System.out.println("false");
 9                return false;
10        }
11         String s=String.valueOf(x);//将输入的int型x转为字符串型
12         int t =s.length();//求出字符串型的x的长度
13         if(x>0&&x<10){//当输入的值只有一位数一定是回文
14             System.out.println("true");
15             return true;
16         }
17       //这一步其实不太懂,经过反复的代码验证可以得出,输入的值的位数是有算上符号的
18         /*if(x>0)
19             t=t;
20         else t=t-1;
21         //所以当输入的值为负的时候需要减一。再次回顾发现既然只要负数就不是回文,那么这段代码就是累赘了
22         //System.out.println(t);
23         x=Math.abs(x);*/
24         if(x!=0){
25             for(int i =0;i<t;i++){
26                 bs[i]=x%10;
27                 x=(x-bs[i])/10;
28                 //count++;   //x的长度
29                 //System.out.println(bs[i]);
30             }
31         }
32         if(t%2!=0){//当x为奇数的时候
33             for(int j=0;j<t/2;j++){
34                 if(bs[(t/2-j-1)]==bs[(t/2+j+1)])
35                 {
36                     count1++;
37                     //System.out.println(count1);
38                 }
39                 else{
40                     System.out.println("false");
41                     return false;
42                     //System.exit(-1);//强制退出
43                 }
44              }
45         }
46
47
48
49          if(t%2==0){
50             for(int j2=0;j2<t/2;j2++){
51                 if(bs[(t/2-j2-1)]==bs[(t/2+j2)]){
52                     count2++;
53
54                 }
55                 else{
56                     System.out.println("false");
57                     //System.exit(-1);
58                     return false;
59                 }
60             }
61          }
62              //System.out.println(count2);
63             if((count2==t/2)||(count1==t/2)){
64                 System.out.println("true");
65                 return true;
66             }
67             else{
68                 System.out.println("false");
69                 return false;
70             }
71
72
73         }
74
75
76
77     }
78
79 /*class a{
80     public static void main(String[] args){
81         // TODO Auto-generated method stub
82         Solution s = new Solution();
83         s.isPalindrome(1001);
84     }
85 }
86 这一段代码是为了验证添加的
87 */
时间: 2024-12-15 01:58:42

Palindrome Number(回文)的相关文章

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

leetcode 9 Palindrome Number 回文数

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

LeetCode Problem 9:Palindrome Number回文数

描述: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 a

9. Palindrome Number 回文 my second leetcode 20170807

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

Palindrome Number(回文串)

题目: Determine whether an integer is a palindrome. Do this without extra space. 分析: 该题目来源于leetcode.回文串是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.当然整数形式的回文串也是类似的.但负数不是回文串.两种思路: 按定义来,依次比较串的首尾,直到中间相隔1个或0个元素(取决于整数是奇数位数还是偶数位数).优点是当不是回文串时,可以很快发

Leetcode——3 Palindrome Number(回文数)

Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过,不过从基础开始尝试吧. Solution: public class Solution { public boolean isPalindrome(int x) { int n=1; int copyx=x; if(x<0)return false; if(x<10)return true; w

LeetCode 9 Palindrome Number 回文数字

题目:Determine whether an integer is a palindrome. Do this without extra space. 翻译:判断一个数字是否是回文数,不要额外空间. 解题思路:因为数字既然传过去了,就不会有越界的问题.每次只需要取最前面和最后面的那一位数字进行比较,相同则继续,不同则返回. 首先要获取数字的位数,假设数字是12344321,一共有8位. 其次是要每次取前后各一位来进行比较,用数字除以1后面7个0得到第一位,用数字对10取余数得到最后一位. 此

LeetCode (30) Palindrome Number (回文数字)

题目描述 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.

[LeetCode]9. Palindrome Number回文数

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