[Leetcode] Strobogrammatic Number 對稱數

Strobogrammatic Number I

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).app

Write a function to determine if a number is strobogrammatic. The number is represented as a string.ide

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

雙指針法

複雜度

時間 O(N) 空間 O(1)指針

思路

翻轉後對稱的數就那麼幾個,咱們能夠根據這個創建一個映射關係:8->8, 0->0, 1->1, 6->9, 9->6,而後從兩邊向中間檢查對應位置的兩個字母是否有映射關係就好了。好比619,先判斷6和9是有映射的,而後1和本身又是映射,因此是對稱數。code

注意

  • while循環的條件是left<=right字符串

代碼

public class Solution {
    public boolean isStrobogrammatic(String num) {
        HashMap<Character, Character> map = new HashMap<Character, Character>();
        map.put('1','1');
        map.put('0','0');
        map.put('6','9');
        map.put('9','6');
        map.put('8','8');
        int left = 0, right = num.length() - 1;
        while(left <= right){
            // 若是字母不存在映射或映射不對,則返回假
            if(!map.containsKey(num.charAt(right)) || num.charAt(left) != map.get(num.charAt(right))){
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

Strobogrammatic Number II

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).get

Find all strobogrammatic numbers that are of length = n.string

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

中間插入法

複雜度

時間 O(N) 空間 O(1)io

思路

找出全部的可能,必然是深度優先搜索。可是每輪搜索如何創建臨時的字符串呢?由於數是「對稱」的,咱們插入一個字母就知道對應位置的另外一個字母是什麼,因此咱們能夠從中間插入來創建這個臨時的字符串。這樣每次從中間插入兩個「對稱」的字符,以前插入的就被擠到兩邊去了。這裏有幾個邊界條件要考慮:

  1. 若是是第一個字符,即臨時字符串爲空時進行插入時,不能插入'0',由於沒有0開頭的數字

  2. 若是n=1的話,第一個字符則能夠是'0'

  3. 若是隻剩下一個帶插入的字符,這時候不能插入'6'或'9',由於他們不能和本身產生映射,翻轉後就不是本身了

這樣,當深度優先搜索時遇到這些狀況,則要相應的跳過

注意

  • 爲了實現從中間插入,咱們用StringBuilder在length/2的位置insert就好了

代碼

public class Solution {
    
    char[] table = {'0', '1', '8', '6', '9'};
    List<String> res;
    
    public List<String> findStrobogrammatic(int n) {
        res = new ArrayList<String>();
        build(n, "");
        return res;
    }
    
    public void build(int n, String tmp){
        if(n == tmp.length()){
            res.add(tmp);
            return;
        }
        boolean last = n - tmp.length() == 1;
        for(int i = 0; i < table.length; i++){
            char c = table[i];
            // 第一個字符不能爲'0',但n=1除外。只插入一個字符時不能插入'6'和'9'
            if((n != 1 && tmp.length() == 0 && c == '0') || (last && (c == '6' || c == '9'))){
                continue;
            }
            StringBuilder newTmp = new StringBuilder(tmp);
            // 插入字符c和它的對應字符
            append(last, c, newTmp);
            build(n, newTmp.toString());
        }
    }
    
    public void append(boolean last, char c, StringBuilder sb){
        if(c == '6'){
            sb.insert(sb.length()/2, "69");
        } else if(c == '9'){
            sb.insert(sb.length()/2, "96");
        } else {
            sb.insert(sb.length()/2, last ? c : ""+c+c);
        }
    }
}
相關文章
相關標籤/搜索