其他分享
首页 > 其他分享> > LeetCode9. 回文数Golang版

LeetCode9. 回文数Golang版

作者:互联网

LeetCode9. 回文数Golang版

1. 问题描述

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。

2. 思路

转换为字符串,使用双指针遍历

3. 代码

func isPalindrome(x int) bool {
    if x > math.MaxInt32 || x < math.MinInt32 {
        return false
    }

    if x < 0 {
        return false
    }

    if x / 10 == 0 {
        return true
    } 

    strX := strconv.Itoa(x)
    j := len(strX) - 1
    for i := 0; i < len(strX) / 2; i++ {
        if strX[i] != strX[j] {
            return false
        }
        j--
    }
    return true
}

标签:strX,false,LeetCode9,Golang,return,true,math,回文
来源: https://blog.csdn.net/flying_monkey_1/article/details/115255078