246. Strobogrammatic Number

246- Strobogrammatic Number
**Question
Editorial Solution

My Submissions

Total Accepted: 12358
Total Submissions: 33099
Difficulty: Easy

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.
For example, the numbers "69", "88", and "818" are all strobogrammatic.

Hide Company Tags
Google
Hide Tags
Hash Table Math
Hide Similar Problems
(M) Strobogrammatic Number II (H) Strobogrammatic Number III

import java.util.*;

public class Solution {
    public boolean isStrobogrammatic1(String num) {
       // ref: https://leetcode.com/discuss/50594/4-lines-in-java
       // idea: Just checking the pairs, going inwards from the ends.
       for (int i=0, j= num.length()-1; i<=j ; i++, j--) 
           if(!"00 11 88 969".contains(num.charAt(i) + "" + num.charAt(j)))
               return false;
        return true;
    }
    
    // https://discuss.leetcode.com/topic/21576/accepted-java-solution
    public boolean isStrobogrammatic(String num) {
        Map map = new HashMap();
        map.put('0', '0');
        map.put('1', '1');
        map.put('6', '9');
        map.put('8', '8');
        map.put('9', '6');
        
        int l = 0, r = num.length() - 1; 
        while (l <= r) {
            if (!map.containsKey(num.charAt(l))) return false;
            if (map.get(num.charAt(l)) != num.charAt(r)) return false;
            l++;
            r--;
        }
        return true;
       
    }
}

你可能感兴趣的:(246. Strobogrammatic Number)