zoukankan      html  css  js  c++  java
  • 9.Palindrome Number

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

    确定一个整数是否回文数,不能占用额外空间。

    (回文数形如121,12321,1221,5等)

    大神代码cbmbbz

    public boolean isPalindrome(int x) {
        if (x<0 || (x!=0 && x%10==0)) return false;
        int rev = 0;
        while (x>rev){//只比较后半部分
        	rev = rev*10 + x%10;//将x的值从各位开始往前移
        	x = x/10;
        }
        return (x==rev || x==rev/10);
    }

    我的代码
    复杂一些,依次比较第一位和最后一位,若相同则移除

    class Solution {
    public boolean isPalindrome(int x) {
    int start=0;
    int end=0;
    int length=String.valueOf(x).length()-1;

    if (x<0)
    return false;

    while(length>0) {
    start=(int) (x/(Math.pow(10, length)));
    end=x%10;
    if(start!=end)
    return false;
    x=(int) (x%(Math.pow(10, length)));
    length--;
    x=(int) (x/10);
    length--;
    }
    return true;
    }
    }

     
  • 相关阅读:
    房价
    Jsrender初体验
    GCD XOR UVA
    GCD
    Aladdin and the Flying Carpet LightOJ
    HDU6035 2017多校第一场1003 树形DP
    F
    C
    B
    An Easy Physics Problem HDU
  • 原文地址:https://www.cnblogs.com/mafang/p/8471239.html
Copyright © 2011-2022 走看看