Leetcode 0246: Strobogrammatic Number

编程入门 行业动态 更新时间:2024-10-11 21:25:14

<a href=https://www.elefans.com/category/jswz/34/1769930.html style=Leetcode 0246: Strobogrammatic Number"/>

Leetcode 0246: 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: 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

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

class Solution {public boolean isStrobogrammatic(String num) {Map<Character, Character> map = new HashMap<> (Map.of('0', '0', '1', '1', '6', '9', '8', '8', '9', '6'));for(int l = 0, r = num.length()-1; l <= r; l++,r--){char lc = num.charAt(l);char rc = num.charAt(r);if(!map.containsKey(lc) || map.get(lc) != rc){return false;}}return true;}
}

更多推荐

Leetcode 0246: Strobogrammatic Number

本文发布于:2024-02-24 14:28:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1695612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Leetcode   Number   Strobogrammatic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!