成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

246. 247. 248. Strobogrammatic Number I II II

Fundebug / 1320人閱讀

摘要:題目解答題目解答先考慮最底層的兩種情況,當(dāng)和當(dāng)?shù)臅r(shí)候,就是最中間的數(shù)為空還是存在唯一的一個(gè)數(shù)。然后我們?cè)谶@個(gè)基礎(chǔ)上,用循環(huán)兩個(gè)數(shù)兩個(gè)數(shù)地一起向外擴(kuò)張。擴(kuò)張后的結(jié)果存在里,作為再服務(wù)于上一層的擴(kuò)張,得到最終結(jié)果。

246.Strobogrammatic NumberI
題目:
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.

For example, the numbers "69", "88", and "818" are all strobogrammatic.

解答:

public class Solution {
    //1, 6, 8, 9, 0
    public boolean isStrobogrammatic(String num) {
        Map map = new HashMap<>();
        map.put(1, 1); map.put(6, 9); map.put(8, 8); map.put(9, 6); map.put(0, 0);
        
        int len = num.length();
        for (int i = 0; i < len; i++) {
            int c1 = num.charAt(i) - "0", c2 = num.charAt(len - 1 - i) - "0";
            if (!map.containsKey(c1) || !map.containsKey(c2) || c1 != map.get(c2)) {
                return false;
            }
        }
        return true;
    }
}

247.StroboGrammatic NumberII
題目:
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.

For example,
Given n = 2, return ["11","69","88","96"].

解答:
先考慮最底層的兩種情況,當(dāng)n == 0和當(dāng)n == 1的時(shí)候,就是最中間的數(shù)為空還是存在唯一的一個(gè)數(shù)。然后我們?cè)谶@個(gè)基礎(chǔ)上,用循環(huán)兩個(gè)數(shù)兩個(gè)數(shù)地一起向外擴(kuò)張。擴(kuò)張后的結(jié)果存在result里,作為base再服務(wù)于上一層的擴(kuò)張,得到最終結(jié)果。

public class Solution {
    public List helper(int n, int m) {
        if (n == 0) return new ArrayList(Arrays.asList(""));
        if (n == 1) return new ArrayList(Arrays.asList("0", "1", "8"));
        
        List list = helper(n - 2, m);
        List result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            
            if (n != m) result.add("0" + s + "0");
            result.add("1" + s + "1");
            result.add("8" + s + "8");
            result.add("6" + s + "9");
            result.add("9" + s + "6");
        }
        
        return result;
    }
    
    public List findStrobogrammatic(int n) {
        return helper(n, n);
    }
}

248.Strobogrammatic NumberIII
題目:
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.

For example,
Given low = "50", high = "100", return 3. Because 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.

解答:
有了上一題作基礎(chǔ),這里我們可以先求出所有長度滿足的數(shù),再通過與low,high的compare比較,選出最終的結(jié)果并count。

public class Solution {
    public List helper(int n, int max) {
        if (n == 0) return new ArrayList(Arrays.asList(""));
        if (n == 1) return new ArrayList(Arrays.asList("1", "8", "0"));
        
        List list = helper(n - 2, max);
        List result = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if (n != max) result.add("0" + s + "0");
            result.add("1" + s + "1");
            result.add("8" + s + "8");
            result.add("6" + s + "9");
            result.add("9" + s + "6");
        }
        return result;
    }
    
    public int strobogrammaticInRange(String low, String high) {
        int count = 0;
        List result = new ArrayList();
        for (int i = low.length(); i <= high.length(); i++) {
            result.addAll(helper(i, i));
        }
        for (String str : result) {
            if (str.length() == low.length() && str.compareTo(low) < 0 || str.length() == high.length() && str.compareTo(high) > 0) continue;
            count++;
        }
        return count;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66216.html

相關(guān)文章

  • 247. Strobogrammatic Number II

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

    cnTomato 評(píng)論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 評(píng)論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 評(píng)論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 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<