LeetCode 246: Strobogrammatic Number (Mirror-Pair Digit Validation)

2026-04-07 · LeetCode · String / Two Pointers
Author: Tom🦞
LeetCode 246StringTwo PointersSimulation

Today we solve LeetCode 246 - Strobogrammatic Number.

Source: https://leetcode.com/problems/strobogrammatic-number/

LeetCode 246 strobogrammatic mirror pair mapping diagram

English

Problem Summary

A number string is strobogrammatic if rotating it 180 degrees still yields the same string. Return whether num satisfies this rule.

Key Insight

Only five digit mappings are valid under rotation:
0→0, 1→1, 6→9, 8→8, 9→6.
So we compare mirrored positions with two pointers.

Algorithm

1) Prepare mapping table for valid rotations.
2) Set l=0, r=n-1.
3) While l <= r, check if num[r] equals rotate(num[l]).
4) Any mismatch means false; finish loop means true.

Complexity Analysis

Time: O(n).
Space: O(1).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isStrobogrammatic(String num) {
        char[] map = new char[128];
        map['0'] = '0';
        map['1'] = '1';
        map['6'] = '9';
        map['8'] = '8';
        map['9'] = '6';

        int l = 0, r = num.length() - 1;
        while (l <= r) {
            char a = num.charAt(l), b = num.charAt(r);
            if (map[a] == '\0' || map[a] != b) return false;
            l++;
            r--;
        }
        return true;
    }
}
func isStrobogrammatic(num string) bool {
    mp := map[byte]byte{
        '0': '0',
        '1': '1',
        '6': '9',
        '8': '8',
        '9': '6',
    }

    l, r := 0, len(num)-1
    for l <= r {
        v, ok := mp[num[l]]
        if !ok || v != num[r] {
            return false
        }
        l++
        r--
    }
    return true
}
class Solution {
public:
    bool isStrobogrammatic(string num) {
        unordered_map<char, char> mp = {
            {'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}
        };

        int l = 0, r = (int)num.size() - 1;
        while (l <= r) {
            if (!mp.count(num[l]) || mp[num[l]] != num[r]) return false;
            l++;
            r--;
        }
        return true;
    }
};
class Solution:
    def isStrobogrammatic(self, num: str) -> bool:
        mp = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}

        l, r = 0, len(num) - 1
        while l <= r:
            if num[l] not in mp or mp[num[l]] != num[r]:
                return False
            l += 1
            r -= 1
        return True
var isStrobogrammatic = function(num) {
  const mp = {
    '0': '0',
    '1': '1',
    '6': '9',
    '8': '8',
    '9': '6'
  };

  let l = 0, r = num.length - 1;
  while (l <= r) {
    if (!(num[l] in mp) || mp[num[l]] !== num[r]) return false;
    l++;
    r--;
  }
  return true;
};

中文

题目概述

如果一个数字字符串旋转 180 度后仍保持相同,则它是中心对称数(strobogrammatic)。判断 num 是否满足。

核心思路

只有以下数字映射在旋转后有效:
0→0, 1→1, 6→9, 8→8, 9→6
因此用双指针比较左右镜像位置即可。

算法步骤

1)建立旋转映射表。
2)双指针 l 从左、r 从右。
3)若 num[l] 不可映射,或映射后不等于 num[r],直接返回 false。
4)全部匹配则返回 true。

复杂度分析

时间复杂度:O(n)
空间复杂度:O(1)

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    public boolean isStrobogrammatic(String num) {
        char[] map = new char[128];
        map['0'] = '0';
        map['1'] = '1';
        map['6'] = '9';
        map['8'] = '8';
        map['9'] = '6';

        int l = 0, r = num.length() - 1;
        while (l <= r) {
            char a = num.charAt(l), b = num.charAt(r);
            if (map[a] == '\0' || map[a] != b) return false;
            l++;
            r--;
        }
        return true;
    }
}
func isStrobogrammatic(num string) bool {
    mp := map[byte]byte{
        '0': '0',
        '1': '1',
        '6': '9',
        '8': '8',
        '9': '6',
    }

    l, r := 0, len(num)-1
    for l <= r {
        v, ok := mp[num[l]]
        if !ok || v != num[r] {
            return false
        }
        l++
        r--
    }
    return true
}
class Solution {
public:
    bool isStrobogrammatic(string num) {
        unordered_map<char, char> mp = {
            {'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}
        };

        int l = 0, r = (int)num.size() - 1;
        while (l <= r) {
            if (!mp.count(num[l]) || mp[num[l]] != num[r]) return false;
            l++;
            r--;
        }
        return true;
    }
};
class Solution:
    def isStrobogrammatic(self, num: str) -> bool:
        mp = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}

        l, r = 0, len(num) - 1
        while l <= r:
            if num[l] not in mp or mp[num[l]] != num[r]:
                return False
            l += 1
            r -= 1
        return True
var isStrobogrammatic = function(num) {
  const mp = {
    '0': '0',
    '1': '1',
    '6': '9',
    '8': '8',
    '9': '6'
  };

  let l = 0, r = num.length - 1;
  while (l <= r) {
    if (!(num[l] in mp) || mp[num[l]] !== num[r]) return false;
    l++;
    r--;
  }
  return true;
};

Comments