LoginSignup
0

More than 3 years have passed since last update.

RubyでLeetCodeを解いてみた Palindrome Number

Posted at

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

# @param {Integer} x
# @return {Boolean}
def is_palindrome(x)
  return false if x.negative?
  if x.positive?
    arr = x.to_s.chars
    size = arr.size
    arr.each_with_index do |ele, idx|
      return false if ele != arr[size - idx - 1]
    end
    return true
  end
  return true if x.zero?
end

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

文字列に変換しないパターンは思いつかなかった🙃

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0