国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 248. Strobogrammatic Number III

yzd / 3198人閱讀

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.

Solution
class Solution {
    int count = 0;
    public int strobogrammaticInRange(String low, String high) {
        Map map = 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

相關(guān)文章

  • 246. 247. 248. Strobogrammatic Number I II II

    摘要:題目解答題目解答先考慮最底層的兩種情況,當(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...

    Fundebug 評論0 收藏0
  • [Leetcode] Strobogrammatic Number 對稱數(shù)

    摘要:比如,先判斷和是有映射的,然后和自己又是映射,所以是對稱數(shù)。這樣每次從中間插入兩個對稱的字符,之前插入的就被擠到兩邊去了。只插入一個字符時不能插入和插入字符和它的對應(yīng)字符 Strobogrammatic Number I A strobogrammatic number is a number that looks the same when rotated 180 degrees ...

    wendux 評論0 收藏0
  • [LeetCode] 246. Strobogrammatic Number

    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...

    whatsns 評論0 收藏0
  • [LeetCode] 247. Strobogrammatic Number II

    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...

    GHOST_349178 評論0 收藏0
  • 247. Strobogrammatic Number II

    摘要:題目鏈接這題和都可以做,一種思路就是從中間開始往兩邊延伸,每次有種可能性和,其中開頭處不能是。可以加或者用優(yōu)化。 247. Strobogrammatic Number II 題目鏈接:https://leetcode.com/problems... 這題recursion和iteration都可以做,一種思路就是從中間開始往兩邊延伸,每次c[i-k], c[i+k]有5種可能性: (...

    cnTomato 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<