当前位置:网站首页>[record of question brushing] 9. Number of palindromes
[record of question brushing] 9. Number of palindromes
2022-07-28 04:19:00 【InfoQ】
One 、 Title Description
Input :x = 121
Output :true
Input :x = -121
Output :false
explain : Read left to right , by -121 . Read right to left , by 121- . So it's not a palindrome number .
Input :x = 10
Output :false
explain : Read right to left , by 01 . So it's not a palindrome number .
Two 、 Thought analysis
1. Turn the string
2. Reverse the number
2.1 Reverse the whole number
2.2 Invert half the array
for example , Input 1221, We can put the numbers “1221” The second half of the “21” Reversal is “12”, And compare it with the first half “12” Compare , Because they are the same , We know the numbers 1221 It's palindrome. .3、 ... and 、 Code implementation
class Solution {
public boolean isPalindrome(int x) {
// Negative numbers and 10 Multiple of Go straight back to
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNum = 0;
while (x > revertedNum) {
revertedNum = revertedNum * 10 + x % 10;
x /= 10;
}
// Palindrome number There are two cases of length parity
return x == revertedNum || x == revertedNum / 10;
}
}
Complexity analysis
Running results

summary
边栏推荐
- CV2. Threshold(), CV2. Findcontours(), CV2. Findcontours image contour processing
- Fourier series
- idea启动项目mvn命令终端用不了法将“mvn”项识别为 cmdlet
- Security exception handling mechanism
- numeric_ Limits the range and related attributes of each data type learned
- @Requiredargsconstructor annotation
- Machine learning 07: Bayesian learning
- Filters, interceptors, listeners
- How much does it cost to build a self built server for ark survival evolution?
- Ffmpeg common instructions
猜你喜欢
![[blood vessel detection] Based on MATLAB mom method, combined with Hessian and curve fitting, blood vessel diameter measurement [including Matlab source code, 1970]](/img/72/10bd8a292c3ee3445907795ebf2ae8.png)
[blood vessel detection] Based on MATLAB mom method, combined with Hessian and curve fitting, blood vessel diameter measurement [including Matlab source code, 1970]

Filters, interceptors, listeners

Summary of static blog building tools

STC timer is abnormal (how to modify the initial value, the timing time is 100ms)

Cookies and session

idea2022更改本地仓库,配置阿里云中央仓库

Citrix virtual desktop tcp/udp transmission protocol switching

idea启动项目mvn命令终端用不了法将“mvn”项识别为 cmdlet
![[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]](/img/5e/dfe029490183ee74687606941ce98e.jpg)
[kinematics] simulation of orbital angular momentum based on MATLAB [including Matlab source code 1971]

MySQL是怎么保证高可用的
随机推荐
Construction and use of FTP server and NFS server
RT-Thread改变打印串口(在BSP的基础上添加其他功能)
jdbc使用
Combination of Oracle and Premier League statistics and presentation
Idea2022 change the local warehouse and configure Alibaba cloud central warehouse
The State Administration of market supervision exposes typical cases of food safety
Security exception handling mechanism
Developing rc522 module based on c8t6 chip to realize breathing lamp
What technical capabilities should a qualified software testing engineer have?
Kingbasees security guide of Jincang database -- 6.2. Configuration files related to authentication
Go grpc: a solution of connection reset by peer
【伸手党福利】微信中h5网页调起扫一扫最简单的方法
Network visualization: features of convolution kernel and CNN visualization (through the attention part of gradient visualization network)
[day03] process control statement
Un7.27: how to successfully build a ruoyi framework project in idea?
Notes on writing questions in sword finger offer
Ffmpeg common instructions
一名合格的软件测试工程师,应该具备哪些技术能力?
C # cross thread refresh foreground UI
[Niuke] find 1+2+3+... +n