Kotlin 算法2 Palindrome Number

题目:确定一个整数是否是回文。整数是回文,当它向后读和向前读时。
例子:
Example 1:
Input: 121
Output: true

Example 2:
Input: -121
Output: false
思路:取两边值比较。
代码:

fun isPalindrome(x: Int): Boolean {
    if (x < 0)
        return false
    if (x < 10)
        return true
    var highLength = 1
    var lowLength = 10
    while (x / highLength >= 10) {
        highLength *= 10
    }

    while (x / highLength % 10 == x % lowLength / (lowLength / 10)) {
        if (highLength < lowLength) return true
        highLength /= 10
        lowLength *= 10
        if (highLength < lowLength) return true
    }
    return false
}

你可能感兴趣的:(Kotlin 算法2 Palindrome Number)