class Solution(object): """ 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。 示例 3: 输入: 10 输出: false 解释: 从右向左读, 为 01 。因此它不是一个回文数。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/palindrome-number 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 """ """ @author : jiyanjiao @date :2020-4-3 """ # 基本解法 @staticmethod def isPalindrome(x): """ :type x: int :rtype: bool """ sx = str(x) rsx = sx[::-1] if sx == rsx: print("true") else: print("false") # 双向队列方法 @staticmethod def isPalindrome_p(x): """ :type x: int :rtype: bool """ lst = list(str(x)) while len(lst) > 1: if lst.pop(0) != lst.pop(): print("False") return Falseprint("True") return True # 双指针方法 @staticmethod def isPalindrome_z(x): """ :type x: int :rtype: bool """ lst = list(str(x)) L, R = 0, len(lst)-1 while L <= R: if lst[L] != lst[R]: print("false") return False L += 1 R -= 1 print("true") return True if __name__ == '__main__': s = Solution() # s.isPalindrome(10) # s.isPalindrome_p(12321) s.isPalindrome_z(12321)