Code Monkey home page Code Monkey logo

Comments (2)

PisecesPeng avatar PisecesPeng commented on September 26, 2024

解题思路

  • 直接将整数倒过来比较...

代码

public static boolean func(int num) {
    // 若整数为0, 则是回文数
    if (num == 0) return Boolean.TRUE;
    // 若整数为负数, 则不可能是回文数
    if (num < 0) return Boolean.FALSE;
    // 若整数最后一位为0, 则不可能是回文数
    if (num % 10 == 0) return Boolean.FALSE;

    int size = 0;
    int tmp = num;
    while (tmp > 0) {
        tmp /= 10;
        size++;
    }
    int[] nums = new int[size];  // 建立整数的数组

    int index = 0;
    while (num > 0) {  // 装载整数
        nums[index] = num % 10;
        num /= 10;
        index++;
    }

    for (int i = 0, j = nums.length - 1; i < nums.length; i++, j--) {
        if (i >= j) return Boolean.TRUE;
        if (nums[i] != nums[j]) return Boolean.FALSE;
    }

    return Boolean.FALSE;
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 26, 2024

LeetCode题解

解题思路

  • 解释已经在下面了

代码

public static boolean func(int num) {
    // 特殊情况:
    // 如上所述,当 x < 0 时,x 不是回文数。
    // 同样地,如果数字的最后一位是 0,为了使该数字为回文,
    // 则其第一位数字也应该是 0
    // 只有 0 满足这一属性
    if (x < 0 || (x % 10 == 0 && x != 0)) {
        return false;
    }

    int revertedNumber = 0;
    while (x > revertedNumber) {
        revertedNumber = revertedNumber * 10 + x % 10;
        x /= 10;
    }

    // 当数字长度为奇数时,我们可以通过 revertedNumber/10 去除处于中位的数字。
    // 例如,当输入为 12321 时,在 while 循环的末尾我们可以得到 x = 12,revertedNumber = 123,
    // 由于处于中位的数字不影响回文(它总是与自己相等),所以我们可以简单地将其去除。
    return x == revertedNumber || x == revertedNumber / 10;
}

from pisecespeng.record.me.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.