文章

9. 回文数

问题9 回文数 ☆☆☆☆☆

https://leetcode.cn/problems/palindrome-number/

显然,如果入参是负数,那么就不是回文数。对于非负数,通过转换得到一个char数组的话,很容易判断是不是回文。但是题目里明确提示,有没有办法不做字符串转换?对于一个整数,从低位到高位遍历每一位数字是很容易的,那么如何在这样一个遍历过程中判断是否回文呢?我们在从低位到高位遍历的过程中,维护一个新的逆序的整数值,根据回文的特征,如果原整数是回文的,那么最终得到的新的逆序的整数和原整数应该相等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	public boolean isPalindrome(int x) {
		if (x < 0) {
			return false;
		}

		int y = x, reversed = 0;
		while (y != 0) {
			int remain = y % 10;
			reversed = reversed*10 + remain;
			y = y / 10;
		}

		return x == reversed;
	}

在 LeetCode 查看

本文由作者按照 CC BY 4.0 进行授权