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

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 246. Strobogrammatic Number

2022. 11. 29. 07:23

246. Strobogrammatic Number

Easy

Given a string num which represents an integer, return true if num is a strobogrammatic number.

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

 

Example 1:

Input: num = "69"
Output: true

Example 2:

Input: num = "88"
Output: true

Example 3:

Input: num = "962"
Output: false

 

Constraints:

  • 1 <= num.length <= 50
  • num consists of only digits.
  • num does not contain any leading zeros except for zero itself.

문제 풀이

  • 주어진 숫자 문자열을 180도 돌렸을때 변하지 않는 문자열인지 확인하는 문제이다.
  • 0, 1, 8은 180도 뒤집어도 같은 모양이며, 6을 뒤집으면 9가되고 9를 뒤집으면 6이된다.
  • 이 짝들을 미리 딕셔너리로 만든다.
  • 그리고 투 포인터를 사용해서 하나씩 양쪽에서 글자를 확인해 나간다. 
  • 글자가 딕셔너리 안에 없거나, 첫번째 포인터의 문자를 뒤집었을때 두번째 포인터의 문자랑 다르다면 False를 리턴한다.

소스 코드

class Solution:
    def isStrobogrammatic(self, num: str) -> bool:
        d = {'0':'0', '1':'1', '8':'8', '6':'9', '9':'6'}
        l, r = 0, len(num) - 1
        while l <= r:
            if num[l] not in d or d[num[l]] != num[r]:
                return False
            r -= 1
            l += 1
        return True
저작자표시 (새창열림)

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

[LeetCode] 380. Insert Delete GetRandom O(1)  (0) 2022.11.30
[LeetCode] 253. Meeting Rooms II  (0) 2022.11.30
[LeetCode] 2225. Find Players With Zero or One Losses  (0) 2022.11.29
[LeetCode] 627. Swap Salary  (0) 2022.11.23
[LeetCode] 1873. Calculate Special Bonus  (0) 2022.11.23
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 380. Insert Delete GetRandom O(1)
    • [LeetCode] 253. Meeting Rooms II
    • [LeetCode] 2225. Find Players With Zero or One Losses
    • [LeetCode] 627. Swap Salary
    saurus2
    saurus2
    Simple is Best

    티스토리툴바