zoukankan      html  css  js  c++  java
  • [LC] 246. Strobogrammatic Number

    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:  "69"
    Output: true
    

    Example 2:

    Input:  "88"
    Output: true

    Example 3:

    Input:  "962"
    Output: false

    class Solution {
        public boolean isStrobogrammatic(String num) {
            Map<Character, Character> mymap = new HashMap<>();
            mymap.put('1', '1');
            mymap.put('8', '8');
            mymap.put('0', '0');
            mymap.put('6', '9');
            mymap.put('9', '6');
            int left = 0;
            int right = num.length() - 1;
            while (left <= right) {
                char left_char = num.charAt(left);
                char right_char = num.charAt(right);
                if (!mymap.containsKey(left_char) || !mymap.containsKey(right_char)) {
                    return false;
                } else if (mymap.get(left_char) != right_char) {
                    return false;
                }
                left += 1;
                right -= 1;
            }
            return true;
        }
    }
  • 相关阅读:
    AVFrame与Mat
    conda警告
    MS COCO数据集格式
    ubuntu卡在工作区切换界面
    C++编程便捷口
    Anaconda相关问题
    处理memory output
    ajax 上传form表单
    元类 metaclass
    小菜一碟
  • 原文地址:https://www.cnblogs.com/xuanlu/p/11840494.html
Copyright © 2011-2022 走看看