首页 > 试题广场 >

回文数字

[编程题]回文数字
  • 热度指数:38706 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
在不使用额外的内存空间的条件下判断一个整数是否是回文。
回文指逆序和正序完全相同。


数据范围:
进阶: 空间复杂度 ,时间复杂度

提示:
负整数可以是回文吗?(比如-1)
如果你在考虑将数字转化为字符串的话,请注意一下不能使用额外空间的限制
你可以将整数翻转。但是,如果你做过题目“反转数字”,你会知道将整数翻转可能会出现溢出的情况,你怎么处理这个问题?
示例1

输入

121

输出

true
示例2

输入

122

输出

false
public boolean isPalindrome (int x) {
    // write code here
    int num=0;
    while(x>num){
        num=num*10+x%10;
        x/=10;
        if(num==x || num==x/10){
            return true;
        }
    }
    return x==0;
}

发表于 2023-06-11 15:59:27 回复(0)
 public boolean isPalindrome (int x) {
        // 反向相加余数
        if(x < 0) {
            return false;
        }
        int rs = x;
        int temp = 0;
        while(x != 0 ) {
            temp = (temp * 10) + x % 10;
            x /= 10;
        }
        return temp == rs?true:false;
    }
发表于 2023-02-21 21:50:50 回复(0)
import java.util.*;


public class Solution {
    /**
     * 
     * @param x int整型 
     * @return bool布尔型
     */
    public boolean isPalindrome (int x) {
        //1 2 3
        //1 2 3 4
        String str=String.valueOf(x);

        for(int i=0;i<str.length()/2;i++){
            char a=str.charAt(i);
            char b=str.charAt(str.length()-i-1);
            if(a!=b){
                return false;
            }
        }

        return true;
    }
}

发表于 2022-10-29 23:43:46 回复(0)
才超越这么点人
import java.util.*;


public class Solution {
    /**
     *
     * @param x int整型
     * @return bool布尔型
     */
    public boolean isPalindrome (int x) {
        // write code here
        int tag = 1;
        if (x < 0) {
            tag = -1;
            x = -x;
        }
        int a = x, b = 0;
        while (a != 0) {
            b = b * 10 + a % 10;
            a = a / 10;
        }
        return x == b * tag ? true : false;
    }
}

发表于 2022-09-04 15:28:52 回复(0)
不如来个简单的思路
import java.util.*;


public class Solution {
    /**
     * 
     * @param x int整型 
     * @return bool布尔型
     */
    public boolean isPalindrome (int x) {
        // write code here
        String r=  String.valueOf(x);
        StringBuilder sb = new StringBuilder();
        sb.append(r.toCharArray());
        return sb.reverse().toString().equals(r);
    }
}

发表于 2022-02-08 17:38:49 回复(0)
直接将最后一位单独拿出来比就不会溢出了
import java.util.*;

public class Solution {
    
    public boolean isPalindrome (int x) {
        if (x < 0) return false;
        int y = 0;
        int z = x;
        while (!(z / 10 == 0)) {
            y = (z % 10 + y) * 10;
            z = z / 10;
        }
        if (x / 10 * 10 == y) return true;
        return false;
    }
}


发表于 2022-01-29 00:43:36 回复(1)
不是说不能用额外的空间嘛?为什么感觉好像所有的答案都定义了一个变量来存储中间值。
我感觉个人的解法就符合题目的意思?用函数的方式来代替“额外的空间”
import java.util.*;


public class Solution {
    /**
     * 
     * @param x int整型 
     * @return bool布尔型
     */
    public boolean isPalindrome (int x) {
        if(x<0) return false;
        if(x<10) return true;
        if(x%10==0) return false;
        return x==reverse(x,0);
    }
    
    public int reverse(int x,int pre){
        if(x<10) return x+pre*10;
        return reverse(x/10,x%10+pre*10);
    }
}


发表于 2022-01-17 23:26:03 回复(0)
一种比较笨的方法
import java.util.*;


public class Solution {
    /**
     * 
     * @param x int整型 
     * @return bool布尔型
     */
    public boolean isPalindrome (int x) {
        // write code here
        if(x < 0 ) return false;
        //计算最高位次
        int high = 1;
        long temp = 10;
        while(x/temp != 0 ){
            high ++;
            temp*=10;
        }
        //i每次加2 因为每次消除两位
        for(int i = 1 ; i <= high/2;i+=2){
            int left = (int)(x%Math.pow(10,1));
            int right = (int)(x/Math.pow(10,high-i));
            if( left!=right )
                return false;
            //最高位
            int max = (int)(Math.pow(10,high-i));
            //消除最高位
            x = x % max;
            //消除最低位
            x = x/10;
            
        }
        return true;
    }
}


发表于 2021-12-28 21:20:45 回复(0)
/*
     * 有人一样吗
     */
public boolean isPalindrome (int x) {         // write code here         if (x<0)return false;         String s=String.valueOf(x);         StringBuffer s1=new StringBuffer(s);         s1.reverse();         String s2 = s1.toString();         if (s2.equals(s)) return true;         return false;     }

发表于 2021-12-20 14:23:26 回复(0)
利用StringBuffer中提供的append方法和reverse()方法
public synchronized StringBuffer append(int i){   toStringCache = null;  super.append(i);  return this;  }

public boolean isPalindrome (int x) {
   StringBuffer sb = new StringBuffer().append(x);
  String bs = sb.reverse().toString();
  if (bs.equals(String.valueOf(x))) {
     return true;
  } else {
    return false;
  }
}

发表于 2021-12-14 21:56:18 回复(0)
import java.util.*;


public class Solution {
    /**
     * 
     * @param x int整型 
     * @return bool布尔型
     */
    public boolean isPalindrome (int x) {
        // write code here
        //利用指针进行查找
       String str= String.valueOf(x);
        int begin=0;
        int end=str.length()-1;
        while(begin<=end){
            //如果相等,遍历下一个
            if(str.charAt(begin)==str.charAt(end)){
                begin++;
                end--;
            }else{
                return false;
            }
        }
        return true;
    }
}

发表于 2021-10-29 17:35:23 回复(0)
public boolean isPalindrome (int x) {
   return String.valueOf(x).equals(new StringBuilder(String.valueOf(x)).reverse().toString());
}

发表于 2021-09-06 21:21:09 回复(0)
    public boolean isPalindrome (int x) {
        // write code here
        if(x < 0)    return false;
        
        if(x == helper(x)){
            return true;
        }
        
        return false;
    }
    
    public int helper(int x){
        long res = 0;
        
        while(x != 0){
            res = res*10+x%10;
            if(res >= Integer.MAX_VALUE)    return 0;
            x /= 10;
        }
        
        return (int)res;
    }
发表于 2021-08-22 21:19:57 回复(0)
import java.util.*;


public class Solution {
    /**
     * 
     * @param x int整型 
     * @return bool布尔型
     */
    public boolean isPalindrome (int x) {
        if(x<0) return false;
        int len=0;
        int tmp = x;
                //计算一共是几位数
        while(tmp!=0){
            tmp = tmp/10;
            len++;
        }
                //通过运算符模拟双指针比较数字
        for(int i=0; i<len; i++) {
            int left = x/(int)Math.pow(10,len-i-1)%10;
            int right = x%(int)Math.pow(10,i+1)/(int)Math.pow(10,i);
            if(left!=right) return false;
        }
        return true;
    
    }
}

发表于 2021-07-28 19:19:29 回复(0)
import java.util.*;
public class Solution {

    public boolean isPalindrome (int x) {
        
if (x > 0) {
			boolean flag = false;
			StringBuffer nx = new StringBuffer(String.valueOf(x));
			StringBuffer nnx = new StringBuffer(String.valueOf(x));
			nx.reverse();
			int s1 = Integer.parseInt(nx.toString());
			int s2 = Integer.parseInt(nnx.toString());
			return s1 == s2 ? true : false;
		}else if( x ==0){
    return true;
} else {
			return false;
		}
    }
}

请问一下,我这个方法有一个用例没过,该怎么解决?2147483647 输入这个数字失败了

发表于 2021-07-20 20:33:13 回复(0)

问题信息

难度:
36条回答 20781浏览

热门推荐

通过挑战的用户