Leetcode 每日刷题 --回文数判断

该代码实现了一个C语言函数,用于判断一个整数是否为回文数。当输入的x为负数时,函数直接返回false,因为负数不可能是回文。对于非负数,函数通过将x的每一位存储到数组中并比较首尾元素来检查是否为回文,如果在遍历过程中发现不相等的数字,则返回false,否则返回true。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

palindrome:Given an integer x, return true if x is a palindrome and false otherwise.

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

解题思路:

x 范围:-231 <= x <= 231 - 1,

负数:负数必然不是回文数,直接返回false

非负数:

1, 取出将x 每个位放入一个uint8_t array[32],

2, array 首尾元素依次向中心移动遍历,遇到不相等则为flase,

   首尾相遇则为true

答案C语言版本:

bool isPalindrome(int x){

    int tmp = x;

    uint32_t len = 0;

    uint8_t tmp_array[32] = {0};

    if (tmp < 0)

        return false;

    while (tmp) {

        tmp_array[len] = tmp % 10;

        tmp = tmp / 10;

        len++;

    }

    for (uint32_t i = 0; i < len; i++,len--)

        if (tmp_array[i] != tmp_array[len - 1])

            return false;

    return true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值