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

資訊專欄INFORMATION COLUMN

[LeetCode] 247. Strobogrammatic Number II

GHOST_349178 / 1421人閱讀

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 = 2
Output: ["11","69","88","96"]

Solution
class Solution {
    public List findStrobogrammatic(int n) {
        List res = new ArrayList<>();
        if (n == 0) return res;
        if (n == 1) return Arrays.asList("0", "1", "8");
        Map map = new HashMap();
        map.put("0", "0");
        map.put("1", "1");
        map.put("6", "9");
        map.put("8", "8");
        map.put("9", "6");
        dfs(map, new char[n], 0, n, res);
        return res;
    }
    private void dfs(Map map, char[] buffer, int index, int n, List res) {
        // when index just passed the mid point, (n+1)/2 works for both odd and even
        if (index == (n+1)/2) {
            res.add(String.valueOf(buffer));
            return;
        }
        for (char ch: map.keySet()) {
            if (index == 0 && n > 1 && ch == "0") continue;
            if (index == n/2 && (ch == "6" || ch == "9")) continue;
            buffer[index] = ch;
            buffer[n-1-index] = map.get(ch);
            dfs(map, buffer, index+1, n, res);
        }
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72426.html

相關文章

  • 247. Strobogrammatic Number II

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

    cnTomato 評論0 收藏0
  • 246. 247. 248. Strobogrammatic Number I II II

    摘要:題目解答題目解答先考慮最底層的兩種情況,當和當的時候,就是最中間的數為空還是存在唯一的一個數。然后我們在這個基礎上,用循環兩個數兩個數地一起向外擴張。擴張后的結果存在里,作為再服務于上一層的擴張,得到最終結果。 246.Strobogrammatic NumberI題目:A strobogrammatic number is a number that looks the same w...

    Fundebug 評論0 收藏0
  • [Leetcode] Strobogrammatic Number 對稱數

    摘要:比如,先判斷和是有映射的,然后和自己又是映射,所以是對稱數。這樣每次從中間插入兩個對稱的字符,之前插入的就被擠到兩邊去了。只插入一個字符時不能插入和插入字符和它的對應字符 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] 248. Strobogrammatic Number III

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

    yzd 評論0 收藏0

發表評論

0條評論

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