美文网首页
246. Strobogrammatic Number (E)

246. Strobogrammatic Number (E)

作者: Ysgc | 来源:发表于2021-01-19 04:08 被阅读0次

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


我的答案:一开始忘了0

class Solution {
public:
    bool isStrobogrammatic(string num) {
        int left = 0;
        int right = num.size()-1;
        
        while (left <= right) { //check
            if (
                (num[left] == '6' and num[right] == '9') or
                (num[left] == '9' and num[right] == '6') or
                (num[left] == '8' and num[right] == '8') or
                (num[left] == '1' and num[right] == '1') or
                (num[left] == '0' and num[right] == '0')
            ) {
                ++left;
                --right;
            }
            else {
                return false;
            }
        }
        
        return true;
    }
};

Runtime: 4 ms, faster than 100.00% of C++ online submissions for Strobogrammatic Number.
Memory Usage: 6.4 MB, less than 30.62% of C++ online submissions for Strobogrammatic Number.

相关文章

网友评论

      本文标题:246. Strobogrammatic Number (E)

      本文链接:https://www.haomeiwen.com/subject/mltxwktx.html