Strobogrammatic Number

 

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.

 

 

public class Solution {
    public boolean isStrobogrammatic(String num) 
    {
        HashMap<Character, Character> map = new HashMap<Character, Character>();
        map.put('6','9');
        map.put('9','6');
        map.put('8','8');
        map.put('0','0');
        map.put('1','1');
        
        int left = 0;
        int right = num.length()-1;
        

        while(left<=right)
        {
            char c1 = num.charAt(left);
            char c2 = num.charAt(right);

            if(!map.containsKey(c1)||map.get(c1)!=c2) return false;
           
            left++;
            right--;
        }
        
        return true;
        
        
    }
}

 

posted @ 2016-01-06 06:58  Hygeia  阅读(158)  评论(0编辑  收藏  举报