TypechoJoeTheme

IT技术分享

统计

[LeetCode 9] Palindrome Number [C]

2017-06-02
/
0 评论
/
568 阅读
/
正在检测是否收录...
06/02

1. Description

Determine whether an integer is a palindrome. Do this without extra space.

2. Runtime Distribution

3. Submission Details

4. Example

input -1 return false
input 12344321 return true

5. Code

package Digital;

public class LeetCode0009 {
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }

        int count = 1, num = x;
        while (x > 9) {
            count *= 10;
            x /= 10;
        }

        while (count > 1) {
            int high = num / count;
            int low = num % 10;
            if (high != low) {
                return false;
            }
            num %= count;
            num /= 10;
            count /= 100;
        }
        return true;
    }

    public static void main(String[] args) {
        LeetCode0009 leetcode = new LeetCode0009();
        System.out.println(leetcode.isPalindrome(120000121));
    }
}
#include<stdio.h>
#include <stdbool.h>

bool isPalindrome(int x) {

    if (x < 0)
    {
        return false;
    }

    int mDigits = 1;

    for (int num = x; (num /= 10) > 0; mDigits *= 10);

    for (; mDigits != 0 && x / mDigits == x % 10; x %= mDigits, mDigits /= 100, x /= 10);

    return x == 0;
}

int main() {
    int x = -1;
    printf("%d\n", isPalindrome(x));
    system("pause");
    return 0;
}
Digital
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/243/(转载时请注明本文出处及文章链接)