leetcode: (9) Palindrome Number

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

方法一:首先求出x一共有多少位,然後從比最低位與最高位的值開始比較

低位的值可以通過 x(pow(10,i))%10的得到

高位的值首先要把第i位前面的去掉x- x/pow(10,count-i)*pow(10,count-i)

然後高位的值爲(x- x/pow(10,count-i)*pow(10,count-i))/pow(10,count-i-1)

bool isPalindrome(int x) {
	  if(x<0) return false;
      if(x/10==0) return 1;
	  int temp=x;
	  int count=0;
	  while(temp!=0)
	  {
		  count++;
		  temp/=10;
	  }
	  for(int i=0;i<count/2;)
	  {
		  temp=(x-(x/(int)pow(10.0,count-i))*(int)pow(10.0,count-i))/pow(10.0,count-1-i);
		  if(temp==x/(int)pow(10.0,i)%10) i++;
		  else return false;
	  }
	  return true;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章