leetcode Palindrome Number

负数被设定为不是回文数

 1 #include<iostream>

 2 

 3 using namespace std;

 4 

 5 int F(int t)

 6 {

 7     int f = 1;

 8     for (int i = 0; i < t; i++)

 9         f = f * 10;

10     return f;

11 }

12 

13 bool isPalindrome(int x) 

14 {

15     int m = x;

16     int L = 0;

17     while (m > 0)

18     {

19         m = m / 10;

20         L++;

21     }

22     int n = L / 2;

23     while (n>0)

24     {

25         int m = F(L - 1);

26         int c = x / m;

27         int t = x % 10;

28         if (t != c)

29         {

30             return false;

31         }

32         x = x % m;

33         x = x / 10;

34         L = L - 2;

35         n--;

36     }

37     return true;

38 }

39 

40 int main()

41 {

42     cout << isPalindrome(2321);

43 }

 

你可能感兴趣的:(LeetCode)