当前位置:网站首页>The number of palindromes in question 9 of C language deduction. Two pointer array traversal method
The number of palindromes in question 9 of C language deduction. Two pointer array traversal method
2022-07-25 00:15:00 【Take care of two dogs and never let them go bad】
Give you an integer x , If x Is a palindrome integer , return true ; otherwise , return false .
Palindrome number refers to positive order ( From left to right ) Reverse order ( From right to left ) Read all the same integers .
for example ,121 It's palindrome. , and 123 No .
Example 1:
Input :x = 121
Output :true
Example 2:
Input :x = -121
Output :false
explain : Read left to right , by - 121 . Read right to left , by 121 - . So it's not a palindrome number .
Example 3:
Input :x = 10
Output :false
explain : Read right to left , by 01 . So it's not a palindrome number .
source : Power button (LeetCode)
link :https ://leetcode.cn/problems/palindrome-number
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
This question is similar to question 6 and question 7 , Consider three categories : Less than 0、 Normal condition 、 Spillage
#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 Realization
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;// The last sentence must add Report an error without adding It doesn't work
}Because only c++ Boolean type only , So I realized it myself first , Stick to it and switch the return value . Finally, we must pay attention to the final, and there must be a return value , Otherwise, the compiler will not pass , Although it's really useless .
边栏推荐
- Remember the problem of using redisson to step on the pit once
- 做一个文艺的测试/开发程序员,慢慢改变自己......
- Beisen prospectus: the advantages of the track are prominent, and integration + medium and large customers are plus points
- NXP i.mx6q development board software and hardware are all open source, and the schematic diagram of the core board is provided
- Wine wechat initialization 96% stuck
- UART
- First experience of flask
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- 软考 --- 程序设计语言基础(下)
- Modify the existing annotator name in the word document
猜你喜欢

2022 最 NB 的 JVM 基础到调优笔记, 吃透阿里 P6 小 case

Can Baidu network disk yundetectservice.exe be disabled and closed
![[英雄星球七月集训LeetCode解题日报] 第24日 线段树](/img/ae/1f3288a99cb07fcbb1836357e0229a.png)
[英雄星球七月集训LeetCode解题日报] 第24日 线段树

痛并快乐的-NIO编程

阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题

Quartus: install cyclone 10 LP device library for quartus version 17.1

Wechat applet development learning 5 (custom components)
![[acwing weekly rematch] 61st weekly 20220723](/img/8b/df2c8d516db1e7e5f2d50bcf62b2b1.png)
[acwing weekly rematch] 61st weekly 20220723

4. Immersion test

C语言学习之分支与循环语句
随机推荐
[untitled]
NFT chain game system development metauniverse gamefi construction
QT project - security monitoring system (function realization of each interface)
codeforces round #797 ABCDEFG
Flash send email
如果实现与在线CAD图中的线段实时求交点
Coding builds an image, inherits the self built basic image, and reports an error unauthorized: invalid credential Please confirm that you have entered the correct user name and password.
Netease game Flink SQL platform practice
你还在使用System.currentTimeMillis()?来看看StopWatch吧
痛并快乐的-NIO编程
Click the "native practice" search box to expand the special effect so that you can realize it. How will you realize it?
Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库
[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU
R language plot visualization: plot to visualize the residual analysis diagram of the regression model, the scatter diagram of the predicted value and residual corresponding to the training set and th
Internal network mapping port to external network
Live broadcast preview | online seminar on open source security governance models and tools
Lambda&Stream
Wechat applet development learning 5 (custom components)
Redis memory analysis tool RMA usage
Efficiency increased by 98%! AI weapon behind operation and maintenance inspection of high altitude photovoltaic power station