当前位置:网站首页>[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
边栏推荐
- Network visualization: features of convolution kernel and CNN visualization (through the attention part of gradient visualization network)
- VAE generation model (with VAE implementation MNIST code)
- 过滤器、拦截器、监听器
- Convert py file to exe executable file
- Kingbasees Security Guide for Jincang database -- 4 data access protection
- CV2. Threshold(), CV2. Findcontours(), CV2. Findcontours image contour processing
- 仿真测试断开服务器公网连接
- 方舟生存进化自建服务器要多少成本?
- Analysis of static broadcast transmission process
- Experience sharing of automatic test for students with monthly salary of 28K
猜你喜欢

Servlet usage

servlet使用

Thoroughly understand the sharing function in wechat games

Virtual machine class loading mechanism

Security exception handling mechanism

Power consumption: leakage power

It's amazing. This article completely explains the service interface testing

Developing rc522 module based on c8t6 chip to realize breathing lamp

ServletContext、request、response

Learn regular expressions (regexp)
随机推荐
[performance optimization methodology series] III. core idea of performance optimization (2)
The unsatisfied analysis of setup and hold timing is the solution
Is there a bonus period for robot engineering
[mathematical modeling] Based on MATLAB seismic exploration Marmousi model [including Matlab source code, 1977]
21天,胖哥亲自带你玩转OAuth2
security异常处理机制
方舟生存进化自建服务器要多少成本?
Construction and use of FTP server and NFS server
"Three no's and five requirements" principle of enterprise Digitalization Construction
Detailed explanation of pl/sql parameters ("box model")
【无标题】
【day03】流程控制语句
Servlet usage
Some personal understandings of openpose
The simulation test disconnects the server from the public network
Kingbasees Security Guide for Jincang database -- 6.1 introduction to strong authentication
H265/HEVC名词解释-- CTU,CTB,CU,CB,TU,PU,TB,PB,LCU,Slice,Tile,Chroma,Luma,I帧,B帧,P帧
How does MySQL ensure high availability
pl/sql之各参数详解(“箱子模型“)
Learn regular expressions (regexp)