(Palindrome Number) c#solution,compare last and first, constant space


  • 0
    Y
    public class Solution {
    public bool IsPalindrome(int x) {
          if (x < 0) return false;
            if (x < 10) return true;
    
            //the biggest int is 2147483647, the biggest digit is 9
            int i;
            for(i=9;i>=1;i--)
            {
                 if (x /(Math.Pow(10, i)) >= 1) break;
            }
            int k = i;
            int j = 0;
            while(k> j)
            {
                if (geteachdigit(x, k) != geteachdigit(x, j)) break;
                k--;
                j++;
                 }
            if (k <= j) return true;
    
            return false;
        }
        public  int geteachdigit(int x,int index)
    
        {
            int tmp = (int)Math.Floor(x / (Math.Pow(10, index)) % 10);
            return tmp ;
        }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.