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

问题描述:

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;

}

你可能感兴趣的:(Leetcode,leetcode,算法,数据结构)