其他分享
首页 > 其他分享> > LeetCode 0009 Palindrome Number

LeetCode 0009 Palindrome Number

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

1、思路分析
反转一半数字,对比是否相等。
2、代码实现

package Q0099.Q009PalindromeNumber;

/*
   手动模拟Stack
*/
public class Solution {

    public boolean isPalindrome(int x) {
        if (x < 0 || (x % 10 == 0 && x != 0)) return false;
        int reverse = 0;
        while (x > reverse) {
            int pop = x % 10;
            x /= 10;
            reverse = reverse * 10 + pop;
        }
        return x == reverse || (reverse / 10) == x;
    }
}

time complexity: O(n)
space complexity: O(1)

标签:10,Palindrome,return,reverse,int,Number,pop,public,LeetCode
来源: https://www.cnblogs.com/junstat/p/15941433.html