摘要:比如,先判斷和是有映射的,然后和自己又是映射,所以是對稱數(shù)。這樣每次從中間插入兩個對稱的字符,之前插入的就被擠到兩邊去了。只插入一個字符時不能插入和插入字符和它的對應(yīng)字符
Strobogrammatic Number I
雙指針法 復(fù)雜度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.
時間 O(N) 空間 O(1)
思路翻轉(zhuǎn)后對稱的數(shù)就那么幾個,我們可以根據(jù)這個建立一個映射關(guān)系:8->8, 0->0, 1->1, 6->9, 9->6,然后從兩邊向中間檢查對應(yīng)位置的兩個字母是否有映射關(guān)系就行了。比如619,先判斷6和9是有映射的,然后1和自己又是映射,所以是對稱數(shù)。
注意while循環(huán)的條件是left<=right
代碼public class Solution { public boolean isStrobogrammatic(String num) { HashMapStrobogrammatic Number IImap = new HashMap (); 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; } }
中間插入法 復(fù)雜度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"].
時間 O(N) 空間 O(1)
思路找出所有的可能,必然是深度優(yōu)先搜索。但是每輪搜索如何建立臨時的字符串呢?因為數(shù)是“對稱”的,我們插入一個字母就知道對應(yīng)位置的另一個字母是什么,所以我們可以從中間插入來建立這個臨時的字符串。這樣每次從中間插入兩個“對稱”的字符,之前插入的就被擠到兩邊去了。這里有幾個邊界條件要考慮:
如果是第一個字符,即臨時字符串為空時進行插入時,不能插入"0",因為沒有0開頭的數(shù)字
如果n=1的話,第一個字符則可以是"0"
如果只剩下一個帶插入的字符,這時候不能插入"6"或"9",因為他們不能和自己產(chǎn)生映射,翻轉(zhuǎn)后就不是自己了
這樣,當(dāng)深度優(yōu)先搜索時遇到這些情況,則要相應(yīng)的跳過
注意為了實現(xiàn)從中間插入,我們用StringBuilder在length/2的位置insert就行了
代碼public class Solution { char[] table = {"0", "1", "8", "6", "9"}; Listres; public List findStrobogrammatic(int n) { res = new ArrayList (); 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和它的對應(yīng)字符 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); } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/64565.html
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...
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...
摘要:所以這題先建立一個對應(yīng)的,然后掃一遍字符串就可以了。復(fù)雜度分析第二題題目內(nèi)容解決思路一看關(guān)鍵詞,通常都是,深搜一遍,挖地三尺,雁過拔毛。復(fù)雜度分析第三題題目內(nèi)容解決思路復(fù)雜度分析 該系列共三道題,Company Tag只有一個Google,那就必須要做了。 第一題題目內(nèi)容 A strobogrammatic number is a number that looks the same ...
摘要:題目解答題目解答先考慮最底層的兩種情況,當(dāng)和當(dāng)?shù)臅r候,就是最中間的數(shù)為空還是存在唯一的一個數(shù)。然后我們在這個基礎(chǔ)上,用循環(huán)兩個數(shù)兩個數(shù)地一起向外擴張。擴張后的結(jié)果存在里,作為再服務(wù)于上一層的擴張,得到最終結(jié)果。 246.Strobogrammatic NumberI題目:A strobogrammatic number is a number that looks the same w...
閱讀 1209·2021-11-22 12:05
閱讀 1336·2021-09-29 09:35
閱讀 630·2019-08-30 15:55
閱讀 3122·2019-08-30 14:12
閱讀 954·2019-08-30 14:11
閱讀 2875·2019-08-30 13:10
閱讀 2400·2019-08-29 16:33
閱讀 3327·2019-08-29 11:02