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: "69" Output: true
Example 2:
Input: "88" Output: true
Example 3:
Input: "962" Output: false
class Solution { public boolean isStrobogrammatic(String num) { Map<Character, Character> mymap = new HashMap<>(); mymap.put('1', '1'); mymap.put('8', '8'); mymap.put('0', '0'); mymap.put('6', '9'); mymap.put('9', '6'); int left = 0; int right = num.length() - 1; while (left <= right) { char left_char = num.charAt(left); char right_char = num.charAt(right); if (!mymap.containsKey(left_char) || !mymap.containsKey(right_char)) { return false; } else if (mymap.get(left_char) != right_char) { return false; } left += 1; right -= 1; } return true; } }