public class Solution {
    public bool IsPalindrome(int x) {
        if (x < 0)
            {
                return false;
            }
            var str = x.ToString();
            var list = new List<string>();
            foreach (var c in str)
            {
                list.Add(c.ToString());
            }

            var count = list.Count;

            for (int i = 0; i < list.Count; i++)
            {
                var n1 = list[i];
                var n2 = list[list.Count - 1 - i];

                if (n1 != n2)
                {
                    return false;
                }

                if (i >= list.Count - 1 - i)
                {
                    break;
                }
            }
            return true;
    }
}

https://leetcode.com/problems/palindrome-number/#/description

 

补充一个python的实现,使用双指针:

 1 class Solution:
 2     def isPalindrome(self, x: int) -> bool:
 3         if x < 0:
 4             return False
 5         s = str(x)
 6         i,j = 0, len(s)-1
 7         while i < j:
 8             if s[i] != s[j]:
 9                 return False
10             i += 1
11             j -= 1
12         return True

 

posted on 2017-04-22 16:01  Sempron2800+  阅读(103)  评论(0编辑  收藏  举报