Problem
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 s) { //69, 88, 00, 11, 6969, 698869, 69869, 6908069, 886988 Mapmap = new HashMap<>(); map.put("6", "9"); map.put("9", "6"); map.put("0", "0"); map.put("1", "1"); map.put("8", "8"); int i = 0, j = s.length()-1; while (i <= j) { if (!map.containsKey(s.charAt(i))) return false; if (map.get(s.charAt(i++)) != s.charAt(j--)) return false; } return true; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/77259.html
摘要:題目解答題目解答先考慮最底層的兩種情況,當和當的時候,就是最中間的數為空還是存在唯一的一個數。然后我們在這個基礎上,用循環兩個數兩個數地一起向外擴張。擴張后的結果存在里,作為再服務于上一層的擴張,得到最終結果。 246.Strobogrammatic NumberI題目:A strobogrammatic number is a number that looks the same w...
摘要:比如,先判斷和是有映射的,然后和自己又是映射,所以是對稱數。這樣每次從中間插入兩個對稱的字符,之前插入的就被擠到兩邊去了。只插入一個字符時不能插入和插入字符和它的對應字符 Strobogrammatic Number I A strobogrammatic number is a number that looks the same when rotated 180 degrees ...
Problem A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Find all strobogrammatic numbers that are of length = n. Example: Input: n = 2Output...
Problem A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to count the total strobogrammatic numbers that exist in the range o...
摘要:題目鏈接這題和都可以做,一種思路就是從中間開始往兩邊延伸,每次有種可能性和,其中開頭處不能是。可以加或者用優化。 247. Strobogrammatic Number II 題目鏈接:https://leetcode.com/problems... 這題recursion和iteration都可以做,一種思路就是從中間開始往兩邊延伸,每次c[i-k], c[i+k]有5種可能性: (...
閱讀 4293·2021-09-24 09:47
閱讀 1184·2021-09-03 10:33
閱讀 2063·2019-08-30 11:13
閱讀 1029·2019-08-30 10:49
閱讀 1753·2019-08-29 16:13
閱讀 2046·2019-08-29 11:28
閱讀 3089·2019-08-26 13:31
閱讀 3631·2019-08-23 17:14