saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 알고리즘
  • 파이썬
  • DFS
  • c++
  • 온라인저지
  • 릿코드
  • 리트코드
  • 백준
  • LeetCode
  • 알고리즘문제해결
  • 개발자
  • 개발자 취업준비
  • BFS
  • 문제해결능력
  • 딕셔너리
  • 취업준비
  • Python
  • 딥러닝
  • 취준
  • two pointer

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[leedCode] 7. Reverse Integer 파이썬 Medium

2021. 11. 2. 04:37

문제 해석:

숫자 한개가 32비트 signed 정수로 주어진다.
이 숫자를 뒤집어야한다. 하지만 정답을 리턴할때 그 뒤집은 숫자가 32 비트 숫자를 넘을 경우 0을 리턴한다.

문제 해설:

1. 입력 받은 숫자를 양수와 음수였을때 처리를 다르게 해야한다.
2. 파이썬에서 숫자를 뒤집는 방법은 문자로 바꾼후에 쉽게 처리할 수 있다. ( 10의 몫과 나머지로 계산하는 무식한 방법도 있다. )
3. 양수일 경우 그냥 문자로 뒤집은 후, 정수로 리턴한다.
4. 음수일 경우 문자로 변환, 앞의 -를 때고 뒤집는다. 리턴할때 다시 붙이고 정수로 변환한다.

7. Reverse Integer

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

 

Example 1:

Input: x = 123 Output: 321

Example 2:

Input: x = -123 Output: -321

Example 3:

Input: x = 120 Output: 21

Example 4:

Input: x = 0 Output: 0

 

Constraints:

  • -231 <= x <= 231 - 1

 

소스코드:

import sys
class Solution:
    def reverse(self, x: int) -> int:
    	# 입력 받은 숫자를 문자열로 변환
        strx = str(x)
        # 숫자가 0 이상일경우 아래 슬라이스로 뒤집는다. 
        if x >= 0:
            res = strx[::-1]
        else:
        # 음수일 경우 앞의 마이너스 기호를 때고 뒤집는다.
            temp = strx[1:]
            temp2 = temp[::-1]
            res = '-' + temp2
        # 출력할 답이 정수 32비트를 넘을경우 0을 반환한다.
        if int(res) >= 2**31-1 or int(res) <= -2**31:
            return 0
        else:
        # 아니면 정수로 치환하여 반환한다.
            return int(res)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 3. Longest Substring Without Repeating Characters 파이썬 Medium  (0) 2021.11.02
[LeedCode] 1041. Robot Bounded In Circle 파이썬 Medium  (0) 2021.11.02
[LeedCode] 2. Add Two Numbers 파이썬 Medium  (0) 2021.11.02
[LeetCode] 200. Number of Islands 파이썬 Medium  (0) 2021.10.31
[LeedCode] 56. Merge Intervals 파이썬 Medium  (0) 2021.10.31
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 3. Longest Substring Without Repeating Characters 파이썬 Medium
    • [LeedCode] 1041. Robot Bounded In Circle 파이썬 Medium
    • [LeedCode] 2. Add Two Numbers 파이썬 Medium
    • [LeetCode] 200. Number of Islands 파이썬 Medium
    saurus2
    saurus2
    Simple is Best

    티스토리툴바