当前位置:网站首页>C语言力扣第九题之回文数。两指针数组遍历法
C语言力扣第九题之回文数。两指针数组遍历法
2022-07-20 14:43:00 【管二狗绝不摆烂】
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
例如,121 是回文,而 123 不是。
示例 1:
输入:x = 121
输出:true
示例 2:
输入:x = -121
输出:false
解释:从左向右读, 为 - 121 。 从右向左读, 为 121 - 。因此它不是一个回文数。
示例 3:
输入:x = 10
输出:false
解释:从右向左读, 为 01 。因此它不是一个回文数。
来源:力扣(LeetCode)
链接:https ://leetcode.cn/problems/palindrome-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这个题和第六题第七题均类似,考虑三类:小于0、正常情况、溢出情况
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int test(int x)
{
int left = 0;
int right = 0;
int s[20] = { 0 };
if (x < 0)
return 0;
if (x == 0)
return 1;
if (x > 0)
{
while (x != 0)
{
s[right] = x % 10;
x = x / 10;
right++;
}
right = right - 1;
while (left <= right)
{
if (s[left] == s[right])
{
left++;
right--;
continue;
}
else if (s[left] != s[right])
return 0;
}
if (left == right + 2)
return 1;
}
}
int main()
{
int x = 121;
//printf("%d", x);
int y = 0;
y = test(x);
printf("%d", y);
return 0;
}
//leetcode实现
bool isPalindrome(int x)
{
int left = 0;
int right = 0;
int s[20] = { 0 };
if (x < 0)
return false;
if (x == 0)
return true;
if (x > 0)
{
while (x != 0)
{
s[right] = x % 10;
x = x / 10;
right++;
}
right = right - 1;
while (left <= right)
{
if (s[left] == s[right])
{
left++;
right--;
continue;
}
else if (s[left] != s[right])
return false;
}
if (left == right + 2)
return true;
}
return true;//最后一句一定要加 不加就报错 虽然没啥用
}
因为只有c++才有布尔类型,所以就先自己实现了一下,粘到上面切换了一下返回值。最后就是要注意力扣在最后一定要有个返回值,不然编译器就不会通过,虽然实在没啥用。
边栏推荐
猜你喜欢
A line break triggered thinking!
With the flow dividend receding, how can FMCG agents break through and grow with RPA?
Nacos 自定义扩展的 Data Id 配置
架构实战营模块七作业
【HarmonyOS】【ArkUI】鸿蒙 linear-gradient 来实现渐变色,怎么动态设置呢?尝试了一下,供大家参考
matlab基础语法总结
多线程进阶(下)
Compréhension approfondie de la haute concurrence
一个换行符引发的思考!
邀请函 | “人、财、税”数字化赋能,力促零售企业实现规模效益“倍增”
随机推荐
中台的理解
线性电路特性的研究与multisim仿真(附工程文件)
One of the tools of SAP ui5 system test: uiveri5
Principle analysis of SAP Spartacus reading cart
A line break triggered thinking!
MySQL implicit lock
MySQL隐式锁
如何使用 Node.js 访问 SAP HANA Cloud 数据库里的数据
高并发的深入理解
Skipped 60 frames! The application may be doing too much work on its main thread
【HMS core】【push kit】【FAQ】华为推送服务 手机未收到推送消息/消息延迟/息屏通知 问题合集
Binary number inversion (C language)
【Harmony OS】【FAQ】鸿蒙应用开发问题分享(字体/构造器)
Why does cloud speed live broadcast win the favor of enterprises?
[composition principle V system bus]
数字信号处理综合MATLAB设计 双音多频拨号系统
SAP UI5 System Test 的工具之一:uiveri5
MySQL页锁
Measurement of typical periodic electrical signals
Practice of data storage scheme in distributed system