簡単 2件解決済み 正答率66.7%

Palindrome Number

Check whether a number reads the same backwards.

このチャレンジで学べること

  • Reverse a number digit by digit
  • Compare the reversed value with the original
  • Handle negative numbers and trailing zeros

問題

翻訳を表示

Decide whether the decimal digits of n read the same forwards and backwards. Negative numbers are never palindromes because of the minus sign.

Implement is_palindrome_number(n).

Input

n: The number to check.

Output

Return true for a palindromic number, otherwise false.

制約

-10¹⁸ ≤ n ≤ 10¹⁸

例

例 1
入力:
n = 12321

期待される出力:
expected = True
入力:
long long n = 12321;

期待される出力:
bool expected = true;
入力:
long n = 12321;

期待される出力:
bool expected = true;

解説

12321 reversed is 12321.

例 2
入力:
n = -121

期待される出力:
expected = False
入力:
long long n = -121;

期待される出力:
bool expected = false;
入力:
long n = -121;

期待される出力:
bool expected = false;

解説

The minus sign makes "-121" differ from "121-".

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

A negative number cannot be a palindrome because of the minus sign.

ヒント 2

Compare the number’s digits from both ends rather than converting unnecessarily.

ヒント 3

Single-digit numbers are always palindromes.

目安の計算量

O(log n) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 KKKaneki Ken Python 965 ms - 5/5 コードを見る
2 HJhettory Jurash Python 980 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に2件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。