leedCode练题——9. Palindrome Number

1、题目

9. Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?


2、我的解法

# -*- coding: utf-8 -*-# @Time    : 2020/1/27 17:28# @Author  : SmartCat0929# @Email   : [email protected]# @Link    : https://github.com/SmartCat0929# @Site    : # @File    : 9. Palindrome Number.py

class Solution:    def isPalindrome(self, x: int) -> bool:        if x > 0:            r = 0            y = str(x)            n = len(y)            d = []            for i in (y):                d.append(i)            for j in range(n):                c = d.pop()                r = r + c                v = int(r)            if v == x:                return v            else:                return 0        elif x== 0:            return True        else:            return 0

print(Solution().isPalindrome(12321))

原文地址:https://www.cnblogs.com/Smart-Cat/p/12240725.html

时间: 2024-08-02 08:19:27

leedCode练题——9. Palindrome Number的相关文章

leedCode练题——12. Integer to Roman

1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve i

leedCode练题——14. Longest Common Prefix

1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight&qu

leedCode练题——21. Merge Two Sorted Lists(照搬大神做法)

1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4-&g

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

leetcode_9题——Palindrome Number (数学问题)

Palindrome Number Total Accepted: 57795 Total Submissions: 194286My Submissions Question Solution Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Hide Tags Math Have you met this question in a real i

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr

Palindrome Number leetcode java

题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

[LeetCode] Palindrome Number [13]

题目 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using