티스토리 뷰

leetcode easy

9. Palindrome Number

msna 2019. 3. 1. 01:05

https://leetcode.com/problems/palindrome-number/


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:


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.

설명 : 왼쪽에서 오른쪽으로 읽을 때 -121. 오른쪽에서 왼쪽으로 읽을 때 121-.  그래서 회문이 아닙니다.


Example 3:

Input: 10

Output: false

Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

설명 : 오른쪽에서 왼쪽으로 읽으면 01. 그래서 회문이 아닙니다.


Follow up:

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

도전과제 : 숫자형을 문자형으로 바꾸지 않고 이 문제를 풀 수 있습니까?


문자형으로 변환을 하지 않고 푸는 것이 도전과제이므로 문자형으로 변환하면 쉽게 풀 수 있다는 것을 알 수 있다.


문자형으로 변환하여 풀기 :


숫자형 자체만으로도 푸는 것도 어렵지는 않다. 숫자를 반전시켜서 반전시키기 전의 값과 같으면 회문숫자이다.

숫자를 반전시키는 건 7.Reverse Integer 참조



'leetcode easy' 카테고리의 다른 글

7. Reverse Integer  (0) 2019.02.28
1. Two Sum  (0) 2019.02.20