当前位置:网站首页>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 .
边栏推荐
- EF core :自引用的组织结构树
- Use es to realize fuzzy search and search recommendation of personal blog
- [untitled]
- The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
- Implement a avatar looping control
- [untitled]
- Be an artistic test / development programmer and slowly change yourself
- 线段树杂谈
- [mindspore] [mode] spontaneous_ The difference between mode and graph mode
- QT learning - using database singleton to complete login matching + registration function
猜你喜欢

Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm

Fast development board for Godson solid state drive startup (burning system to solid state) - partition

Wechat applet development learning 5 (custom components)
![[untitled]](/img/8a/c51328240e28459e98aedcf107c0a3.png)
[untitled]

Oracle is not null cannot filter null values

你还在使用System.currentTimeMillis()?来看看StopWatch吧

Dynamic programming-01 knapsack rolling array optimization

Advanced function of postman

Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库
![[LeetCode周赛复盘] 第 83 场双周赛20220723](/img/db/c264c94ca3307d4363d3cf7f5d770b.png)
[LeetCode周赛复盘] 第 83 场双周赛20220723
随机推荐
Use es to realize fuzzy search and search recommendation of personal blog
[hero planet July training leetcode problem solving daily] 24th line segment tree
Two numbers that appear only once in the array
[mindspore] [mode] spontaneous_ The difference between mode and graph mode
Redis memory analysis tool RMA usage
2022 Henan Mengxin League game 2: Henan University of technology K - Rice
Pit record: typeerror:'module'object is not callable
Can Baidu network disk yundetectservice.exe be disabled and closed
Click the "native practice" search box to expand the special effect so that you can realize it. How will you realize it?
Analyzing the principle of DNS resolution in kubernetes cluster
C language program environment and preprocessing
C language: deep analysis function stack frame
动态规划-01背包滚动数组优化
剖析kubernetes集群内部DNS解析原理
Pain and happiness -nio programming
torch.nn.SyncBatchNorm.convert_ sync_ Mindspore usage corresponding to batchnorm
Palm package manager of kubernetes learning offline installation of NFS client provider
采坑记录:TypeError: 'module' object is not callable
[LeetCode周赛复盘] 第 303 场周赛20220724
Simple operation K6