当前位置:网站首页>Leetcode 9. palindromes
Leetcode 9. palindromes
2022-07-29 06:22:00 【Zhangchuming ZCM】
Power button | #9 Palindrome number
Title screenshot

Method 1 : Convert an integer to a string, invert the characters, and then compare
Learn from the idea of integer inversion , Slightly modify the code , Add a returned comparison value
class Solution:
def isPalindrome(self, x: int) -> bool:
INT_MIN, INT_MAX = -2**31, 2**31-1
if x<INT_MIN or x>INT_MAX:
return False
else:
y = int(str(abs(x))[::-1])
if y<INT_MIN or y > INT_MAX:
return False
else:
return x==y
Code optimization
Directly compare the two converted strings , There is no need to switch back and forth .
class Solution:
def isPalindrome(self, x: int) -> bool:
INT_MIN, INT_MAX = -2**31, 2**31-1
if x<INT_MIN or x>INT_MAX:
return False
else:
x = str(x)
return x == x[::-1]Code re optimization
class Solution:
def isPalindrome(self, x: int) -> bool:
INT_MIN, INT_MAX = -2**31, 2**31-1
s=str(x)
return True if (x>INT_MIN and x<INT_MAX) and (s == s[::-1]) else FalseMethod 2 : Mathematical method
Negative numbers must not be palindromes , Go straight back to False. Positive integer pair 10 After taking the module, it becomes the head of the new number . Comparing whether the old and new numbers are the same .
class Solution:
def isPalindrome(self, x: int) -> bool:
INT_MIN, INT_MAX = -2**31, 2**31-1
y = 0
temp = x
if x < 0:
return False
while temp != 0:
y = y*10 + temp%10
temp//=10
return False if (y>INT_MAX or x>INT_MAX or y<INT_MIN or x<INT_MIN ) else (x == y)Palindromes can also be considered because they are symmetrical , In fact, you don't need to compare after reversing all , But separate from the middle , before and after comparison .
I don't want to write any more here .
边栏推荐
- SimpleFOC调参2-速度、位置控制
- markdown与Typora
- LeetCode #1.两数之和
- From entry to soul: how to use tb6600 single chip microcomputer to control stepping motor with high precision (42/57)
- Ml self study notes 5
- 循环链表和双向链表
- Shell tool finalshell
- Zero basics FPGA (5): counter of sequential logic circuit design (with introduction to breathing lamp experiment and simple combinational logic design)
- 封装——super关键字
- 位运算学习笔记
猜你喜欢

【软件工程之美 - 专栏笔记】22 | 如何为项目做好技术选型?

2022 spring move - core technology FPGA post technical aspects (one side experience)

LeetCode #19.删除链表的倒数第N个结点

【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?

STM32 串口乱码

计算机大厂面试题

封装——super关键字

Open source based on STM32: MHD Bluetooth speaker (including source code +pcb)
![寒假集训总结 (1.23~1.28) [第一梯队]](/img/cf/2f86ecc23bfe6d96ad0429c785663a.png)
寒假集训总结 (1.23~1.28) [第一梯队]

FPGA based: moving target detection (schematic + source code + hardware selection, available)
随机推荐
FPGA based: moving target detection (schematic + source code + hardware selection, available)
三国演义章节内容
Eight sorts ----------- bubble sort
Ml6 self study notes
LeetCode #344.反转字符串
无符号右移
简洁代码实现pdf转word文档
八大排序-----------------堆排序
QT learning notes QT model/view
LeetCode #977.有序数组的平方
Rowkey设计
计算机网络面试题
LeetCode #9.回文数
【软件工程之美 - 专栏笔记】20 | 如何应对让人头疼的需求变更问题?
#7110 数字走向2 题解
markdown与Typora
Design and implementation of QT learning notes data management system
Power electronics: single inverter design (matlab program +ad schematic diagram)
抽象类以及接口
SimpleFOC调参1-力矩控制