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 of low <= num <= high.
Example:
Input: low = "50", high = "100"
Output: 3
Explanation: 69, 88, and 96 are three strobogrammatic numbers.
Note:
Because the range might be a large number, the low and high numbers are represented as string.
class Solution { int count = 0; public int strobogrammaticInRange(String low, String high) { Mapmap = new HashMap<>(); map.put("0", "0"); map.put("1", "1"); map.put("6", "9"); map.put("8", "8"); map.put("9", "6"); List res = new ArrayList<>(); for (int len = low.length(); len <= high.length(); len++) { dfs(map, low, high, new char[len], 0, len-1, res); } System.out.println(res); return count; } private void dfs(Map map, String low, String high, char[] buffer, int left, int right, List res) { if (left > right) { String num = new String(buffer); if ((num.length() == low.length() && num.compareTo(low) < 0) || (num.length() == high.length() && num.compareTo(high) > 0)) { return; } count++; res.add(num); return; } for (char ch: map.keySet()) { if (buffer.length != 1 && left == 0 && ch == "0") continue; if (left == right && ch != map.get(ch)) continue; buffer[left] = ch; buffer[right] = map.get(ch); dfs(map, low, high, buffer, left+1, right-1, res); } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/72425.html
摘要:題目解答題目解答先考慮最底層的兩種情況,當(dāng)和當(dāng)?shù)臅r候,就是最中間的數(shù)為空還是存在唯一的一個數(shù)。然后我們在這個基礎(chǔ)上,用循環(huán)兩個數(shù)兩個數(shù)地一起向外擴(kuò)張。擴(kuò)張后的結(jié)果存在里,作為再服務(wù)于上一層的擴(kuò)張,得到最終結(jié)果。 246.Strobogrammatic NumberI題目:A strobogrammatic number is a number that looks the same w...
摘要:比如,先判斷和是有映射的,然后和自己又是映射,所以是對稱數(shù)。這樣每次從中間插入兩個對稱的字符,之前插入的就被擠到兩邊去了。只插入一個字符時不能插入和插入字符和它的對應(yīng)字符 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). Write a function to determine if a number is strobogrammatic. The number is represent...
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...
摘要:題目鏈接這題和都可以做,一種思路就是從中間開始往兩邊延伸,每次有種可能性和,其中開頭處不能是。可以加或者用優(yōu)化。 247. Strobogrammatic Number II 題目鏈接:https://leetcode.com/problems... 這題recursion和iteration都可以做,一種思路就是從中間開始往兩邊延伸,每次c[i-k], c[i+k]有5種可能性: (...
閱讀 1344·2023-04-26 00:35
閱讀 2716·2023-04-25 18:32
閱讀 3344·2021-11-24 11:14
閱讀 770·2021-11-22 15:24
閱讀 1418·2021-11-18 10:07
閱讀 6467·2021-09-22 10:57
閱讀 2774·2021-09-07 09:58
閱讀 3565·2019-08-30 15:54