当前位置:网站首页>leetcode 9. Palindromic Numbers
leetcode 9. Palindromic Numbers
2022-08-11 04:56:00 【_Liu Xiaoyu】
作者简介:C/C++ 、Golang 领域耕耘者,创作者
个人主页:作者主页
活动地址:CSDN21天学习挑战赛
题目来源: leetcode官网
如果感觉博主的文章还不错的话,还请关注 、点赞 、收藏🧡三连支持一下博主哦~~~
题目描述
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false .
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数.
例如,121 是回文,而 123 不是.
示例1:
输入:x = 121
输出:true
示例2:
输入:x = -121
输出:false
解释:从左向右读, 为 -121 . 从右向左读, 为 121- .因此它不是一个回文数.
示例 3:
输入:x = 10
输出:false
解释:从右向左读, 为 01 .因此它不是一个回文数.
🧡 算法分析
三种方法
Convert strings directly for comparison
library functions used,May not be recommended in interviews,The principle of the library function is also to reverse the order one character at a timeConverting reversed numbers is doing the comparison(推荐)
Compare the general characters of numbers
Optimization of the previous method,However, the time complexity is not optimized
代码实现
class Solution {
public:
bool isPalindrome(int x) {
// 字符串方法
// if(x < 0) return false;
// string t = to_string(x);
// return t == string(t.rbegin(), t.rend()); // 直接初始化t 的翻转字符串
// Transform digital methods
if(x < 0) return false;
int y = x;
long long re = 0;
while(x)
{
re = re * 10 + x % 10;
x /= 10;
}
return re == y;
}
};
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0 || x && x % 10 == 0) return false;
int s = 0;
while (s <= x)
{
s = s * 10 + x % 10;
if (s == x || s == x / 10) return true; // 分别处理整数长度是奇数或者偶数的情况
x /= 10;
}
return false;
}
};
执行结果:

时间复杂度分析
where the numbers are traversed once, 时间复杂度为O(n)
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
🧡 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
- 【小记】BatchSize的数值是设置的越大越好吗
- 2021 Network Planning Designer Afternoon Case Questions
- 02. Fold hidden text
- jwsManager服务接口实现类-jni实现
- C语言:实用调试技巧
- FPGA工程师面试试题集锦111~120
- K8s Review Notes 7--K8S Implementation of Redis Standalone and Redis-cluster
- The shortest path out of the maze
- C语句:数据存储
- 【Web3 系列开发教程——创建你的第一个 NFT(9)】如何在手机钱包里查看你的 NFT
猜你喜欢

ALSA音频架构

Jetson Orin platform 4-16 channel GMSL2/GSML1 camera acquisition kit recommended

2021年网络规划设计师下午案例题

Merkel Studio--OpenEuler Training Notes (1)

交换机和路由器技术-24-OSPF单区域配置

对象的创建以及显示转换

Layered Architecture & SOA Architecture

Paper Notes: Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks

ALSA音频架构 -- snd_pcm_open函数分析

Embedded Sharing Collection 33
随机推荐
如何阅读论文
交换机和路由器技术-32-命名ACL
FPGA工程师面试试题集锦121~130
2.2 user manual] [QNX Hypervisor 10.15 vdev timer8254
[Web3 series development tutorial - create your first NFT (9)] How to view your NFT in the mobile wallet
The sword refers to offer_abstract modeling capabilities
The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews
ALSA音频架构
剑指offer_抽象建模能力
To break the bottleneck of transactional work, the gentleman signs the electronic contract to release the "source power" of HR!
一起Talk编程语言吧
Self-research capability was recognized again, and Tencent Cloud Database was included in the Forrester Translytical report
zabbix构建企业级监控告警平台
增加PRODUCT_BOOT_JARS及类 提供jar包给应用
Dry goods: The principle and practice of server network card group technology
Australia cyberspace security system construction
C语句:数据存储
绿盾加密如何顺利切换成IP-Guard加密
【FPGA教程案例50】控制案例2——基于FPGA的PD控制器verilog实现
走出迷宫的最短路径