[LeetCode] 9. Palindrome Number (Easy)

9. Palindrome Number (c++)

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -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: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

題意:判斷回文數(shù)字。不使用回文串方法。

思路:新建,賦值,判斷....

class Solution
{
public:
  bool isPalindrome(int x)
  {
    int a = 0, b = x;
    while (b>0)
    {
      a = a * 10 + b % 10;
      b = b / 10;
    }
    cout << a << b << endl;
    return a == x ? 1 : 0;
  }
};
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容