当前位置:网站首页>- 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: trueExample 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:

边栏推荐
猜你喜欢

NAS和私有云盘的区别?1篇文章说清楚

Service discovery of kubernetes

局部敏感哈希:如何在常数时间内搜索Embedding最近邻

Analysis of the status quo of digital transformation of manufacturing enterprises

Safety (2)

因为WiFi原因navicat 无法连接数据库Mysql

51. 数字排列

2022 Henan Youth Training League Game (3)

树链剖分-

BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
随机推荐
树链剖分-
AWR analysis report questions for help: How can SQL be optimized from what aspects?
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
Flask入门学习教程
aws s3上传文件
搭建zabbix监控及邮件报警(超详细教学)
使用DBeaver进行mysql数据备份与恢复
ALCCIKERS Shane 20191114
简单的页面跳转活动
考完PMP学什么?前方软考等着你~
ros多客户端请求服务
NIO‘s Sword(牛客多校赛)
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
架构:分布式任务调度系统(SIA-Task)简介
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games
罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
【LeetCode】1374. 生成每种字符都是奇数个的字符串
51. 数字排列
记一次gorm事务及调试解决mysql死锁