Monday, December 28, 2015

246. Strobogrammatic Number ---------E

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
A:

**************  用 Hashing ***************
class Solution {
public:
    bool isStrobogrammatic(string num) {
        unordered_map<char,char> M={{'1','1'}, {'0','0'}, {'8','8'},{'6','9'},{'9','6'}};
        string res="";
        for(char ch:num){
            if(M.find(ch) == M.end())
                return false;
            res= M[ch]+res;
        }
        return res == num;
    }
};

*************  忘记先检查  char  是否在map中 ***********



No comments:

Post a Comment