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


我的答案:一开始忘了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.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容