• [LintCode] Mirror Numbers


    A mirror 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 mirror. The number is represented as a string.

    Example

    For example, the numbers "69", "88", and "818" are all mirror numbers.
    Given num = "69" return true
    Given num = "68" return false

    The allowed mapping is:

    6 -> 9 

    9 -> 6

    0 -> 0

    1 -> 1

    8 -> 8

    Use a hash map to store the above mapping and go through the input string. If a character has no mappings or 

    its mapped character is not the same with the character at its symmetric right side , return false.

     1 public class Solution {
     2     public boolean isStrobogrammatic(String num) {
     3         Map<Character, Character> map = new HashMap<Character, Character>();
     4         map.put('6', '9');
     5         map.put('9', '6');
     6         map.put('0', '0');
     7         map.put('1', '1');
     8         map.put('8', '8');
     9         int left = 0, right = num.length() - 1;
    10         while (left <= right) {
    11             if (!map.containsKey(num.charAt(left))) {
    12                 return false;
    13             }
    14             if (map.get(num.charAt(left)) != num.charAt(right)) {
    15                 return false;
    16             }
    17             left++;
    18             right--;
    19         }
    20         return true;
    21     }
    22 }

    Related Problems

    [LeetCode]Strobogrammatic Number II

    [LeetCode]Strobogrammatic Number III

  • 相关阅读:
    FJNUOJ Yehan’s hole(容斥求路径数 + 逆元)题解
    FJNUOJ the greed of Yehan(最长路 + 权值乘积转化)题解
    BZOJ 2956 模积和
    BZOJ 2299 向量
    codeforces 718c Sasha and Array
    BZOJ 3747 Kinoman
    BZOJ 2431 逆序对数列
    BZOJ 3289 Mato的文件管理
    BZOJ 3781 小B的询问
    BZOJ 2038 小Z的袜子(hose)
  • 原文地址:https://www.cnblogs.com/lz87/p/6999145.html
Copyright © 2020-2023  润新知