2023年12月26日 星期二

12/26 每日C 回文

Easy
Topics
Companies
Hint

Given an integer x, return true if x is a 

, and false otherwise.

 

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

 

Constraints:

  • -231 <= x <= 231 - 1

 

Follow up: Could you solve it without converting the integer to a string? 




bool isPalindrome(int x) {

    if(x<0||x%10==0&&x!=0) return false;
   
    long int rev =0 ,temp=x;
    while(x !=0)
    {
        rev = rev*10 + x%10 ;
        x /=10;

    }
    //printf("rev=%d x=%d",rev,temp);
    //return (rev == x || x==rev/10);
    return rev ==temp;

}

標籤: ,

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁