물음표 살인마의 개발블로그

알고리즘 문제/Leetcode

246. Strobogrammatic Number

BEstyle 2022. 12. 28. 16:40

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.

class Solution:
    def isStrobogrammatic(self, num: str) -> bool:
        lp=0
        rp=len(num)-1
        while lp<=rp:
            if num[lp] not in "01869":
                return False
            if num[lp] in "018":
                if num[lp]!=num[rp]:
                    return False
            if num[lp] == "6":
                if num[rp]!="9":
                    return False
            if num[lp] == "9":
                if num[rp]!="6":
                    return False
            lp+=1
            rp-=1
        return True

'알고리즘 문제 > Leetcode' 카테고리의 다른 글

441. Arranging Coins  (0) 2022.12.28
374. Guess Number Higher or Lower  (0) 2022.12.28
35. Search Insert Position  (0) 2022.12.28
704. Binary Search  (1) 2022.12.28
150. Evaluate Reverse Polish Notation  (0) 2022.12.28