⚡Flashcard Drill➔
NeetCode #193LC-246EasyTwo Pointers
← Back to All Problems

#193 · #246 · Strobogrammatic Number(中心对称数)

📌 Problem Statement & Constraints

A strobogrammatic number is one that looks the same when rotated 180 degrees. The valid digit pairs are 0-0, 1-1, 6-9 and 8-8. Given a string num representing an integer, return whether it is strobogrammatic. Constraints: 1 <= num.length <= 50; num consists of digits only.

💡 Core Algorithmic Approaches

  1. Two pointers from the ends: each character must map to its rotation partner.
  2. The mapping is {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}; any other digit makes the number invalid.
  3. Compare num[i] with the rotation of num[j]; on a mismatch, return false.
  4. The middle character (for odd lengths) must map to itself, which the general comparison handles.

💻 Benchmark Python3 Implementation

class Solution:
    def isStrobogrammatic(self, num: str) -> bool:
        rot = {"0": "0", "1": "1", "6": "9", "8": "8", "9": "6"}
        i, j = 0, len(num) - 1
        while i <= j:
            if num[i] not in rot or num[j] not in rot:
                return False
            if rot[num[i]] != num[j]:   # rotation partner must match
                return False
            i += 1
            j -= 1
        return True

⚡ Complexity Deep Dive

⏱️ Time Complexity
O(n): each character is examined once.
💾 Space Complexity
O(1): the mapping table is fixed.

⚠️ Interview Pitfalls & Follow-ups

  • Assuming the rotation is just a reversal: 180-degree rotation reverses the order and maps each digit, so both steps are needed.
  • Forgetting that 2, 3, 4, 5, 7 are invalid: their presence makes the answer false.
  • Using i < j as the loop condition: for odd lengths the middle character must still be checked, so the condition is i <= j.
  • Checking num[i] == rot[num[j]]: equivalent here because the mapping is an involution, but the form rot[num[i]] == num[j] reads more naturally.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔

🧩 Related Two Pointers Problems

中文速览与面试要点

中心对称数

中心对称数是指一个数字在旋转了 180 度之后看起来依旧相同的数字(或者上下颠倒地看)。请写一个函数来判断该数字是否是中心对称数。数字旋转后的映射关系为:0 → 0、1 → 1、6 → 9、8 → 8、9 → 6。注意:含有其他数字(2、3、4、5、7)的数不是中心对称数。约束:1 ≤ num.length ≤ 50;num 仅由数字组成。

核心要点

  • 相向双指针 + 映射表(标准解) :用 l、r 分别指向首尾,检查 map[num[l]] == num[r] 。
  • 为什么用双指针而非整体反转 :旋转 180° 等价于「逆序 + 逐位映射」,双指针可以在 O(1) 空间内原地验证,无需构造新字符串。
  • 无效数字的处理 :若 num[l] 不在映射表中,直接返回 False。
  • 与 1056(易混淆数)的区别 :本题要求旋转后 等于 原数;1056 要求旋转后 不等于 原数。两者的判定条件恰好相反。

常见陷阱

  • 映射方向写反 :映射是 6 → 9 与 9 → 6 ,而 0/1/8 映射到自身。写反会导致 "69" 被误判。
  • 循环条件写成 l &lt; r :长度为奇数时中间那个字符也必须满足「映射到自身」(只能是 0、1、8),用 l &lt; r 会跳过检查。必须用 l &lt;= r 。
  • 忘记检查字符是否在映射表中 : "2" 应返回 False,若不检查映射表存在性会抛 KeyError。
  • 与 1056 混淆 :本题要求旋转后 相同 ,1056 要求 不同 。

参考实现

class Solution:
    def isStrobogrammatic(self, num: str) -> bool:
        # 旋转 180 度后的映射:0->0, 1->1, 6->9, 8->8, 9->6
        rotate = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'}
        l, r = 0, len(num) - 1
        while l <= r:
            if num[l] not in rotate or rotate[num[l]] != num[r]:
                return False
            l += 1
            r -= 1
        return True