当前位置:网站首页>- 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:
边栏推荐
猜你喜欢
随机推荐
Flask 报错:WARNING This is a development server. Do not use it in a production deployment
2022年NPDP考完多久出成绩?怎么查询?
Chopper webshell feature analysis
AI目标分割能力,无需绿幕即可实现快速视频抠图
Flask入门学习教程
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
yaml
Chapter 7 Noise analysis
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
记一次gorm事务及调试解决mysql死锁
AI target segmentation capability for fast video cutout without green screen
淘宝详情.
Unable to log in to the Westward Journey
菜刀webshell特征分析
字符串常用方法
The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
AcWing 1053. 修复DNA 题解(状态机DP、AC自动机)
CASE2023
递归检查配置项是否更变并替换