JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

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

Write a function to determine if a number is strobogrammatic. The number is represented as a string.

Example 1:

Input: num = "69"
Output: true

Example 2:

Input: num = "88"
Output: true

Example 3:

Input: num = "962"
Output: false

Example 4:

Input: num = "1"
Output: true

Code

class Solution {
    public boolean isStrobogrammatic(String num) {
        HashMap<Character, Character> map = new HashMap<>();

        map.put('6', '9');
        map.put('9', '6');
        map.put('0', '0');
        map.put('8', '8');
        map.put('1', '1');

        int left = 0;
        int right = num.length() - 1;
        while(left <= right){
            if(!map.containsKey(num.charAt(left))){
                return false;
            }

            if(map.get(num.charAt(left)) != num.charAt(right)){
                return false;
            }

            left++;
            right--;
        }

        return true;
    }
}
class Solution:
    def isStrobogrammatic(self, num: str) -> bool:
        map = {
            "0": "0",
            "1": "1",
            "6": "9",
            "8": "8",
            "9": "6",
        }

        l = 0
        r = len(num) - 1

        while l <= r:
            if num[l] not in map.keys() or map[num[l]] != num[r]:
                return False

            l += 1
            r -= 1

        return True