当前位置:网站首页>- daily a LeetCode 】 【 9. Palindrome
- daily a LeetCode 】 【 9. Palindrome
2022-08-02 02:46:00 【The moon chews into stars~】
Table of Contents
Original title:
Gives you an integer x , returns true if x is a palindrome; otherwise, returns false .
Palindromic numbers are integers that read the same in positive order (from left to right) and in reverse order (from right to left).
For example, 121 is a palindrome, but 123 is not.
Example 1:
Input: x = 121Output: true
Example 2:
Input: x = -121output: falseExplanation: Read from left to right, as -121 .Reading from right to left, it is 121- .Therefore it is not a palindrome.
Example 3:
Input: x = 10output: falseExplanation: Read from right to left as 01 .Therefore it is not a palindrome.
Solution ideas:
First understand the concept of palindrome correctly.We know that negative numbers and numbers with 0 at the end cannot be palindromic numbers, so these two are used as boundaries and are false.The other numbers are palindrome numbers.First of all, you can solve the problem like this: determine whether the first and last digit of the number are consistent. If they are different, it is not a palindrome, and two are compared each time, that is, a left and a right must be set, and the left andWhether the right is consistent or not, it returns false.
Compare the corresponding numbers in the integers by rounding and remainder operations.
For example:
1221 is the number.By calculating 1221 / 1000, get the first 1
By calculating 1221 % 10, you can get the last 1
Compare
Then take 22 out to continue the comparison
Code implementation:
class Solution {public boolean isPalindrome(int x) {//Boundary judgmentif(x<0||(x%10==0&&x!=0)){return false;}int ret=1;while(x/ret>=10){ret*=10;}while(x>0){int left=x/ret;int right=x%10;if(left!=right){return false;}x=(x%ret)/10;ret/=100;}return true;}}
Run result:
边栏推荐
- 【LeetCode】1374. 生成每种字符都是奇数个的字符串
- 线程的不同状态
- 第10章_索引优化与查询优化
- esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
- Chapter 7 Noise analysis
- analog IC layout-Environmental noise
- BI-SQL丨WHILE
- Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
- Oracle19c安装图文教程
猜你喜欢
ros多客户端请求服务
Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
一次SQL优化,数据库查询速度提升 60 倍
Remember a pit for gorm initialization
Chapter 7 Noise analysis
Flask入门学习教程
BI - SQL 丨 WHILE
使用docker安装mysql
NAS和私有云盘的区别?1篇文章说清楚
数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
随机推荐
svm.SVC应用实践1--乳腺癌检测
【LeetCode】20.有效的括号
GTK RGB图像绘制
【LeetCode】94.二叉树的中序遍历
C#测试项目中属性的用法
BI-SQL丨WHILE
51. 数字排列
2022.8.1-----leetcode.1374
【web】Understanding Cookie and Session Mechanism
ros多客户端请求服务
2022牛客多校四_G M
很有意思的经历,很有意思的项目--文件夹对比工具
IPFS部署及文件上传(golang)
字符串常用方法
CASE2023
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
Install mysql using docker
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
Flask 报错:WARNING This is a development server. Do not use it in a production deployment