★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-gvxkgjgp-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
You want to form a target
string of lowercase letters.git
At the beginning, your sequence is target.length
'?'
marks. You also have a stamp
of lowercase letters.github
On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp. You can make up to 10 * target.length
turns.數組
For example, if the initial sequence is "?????", and your stamp is "abc"
, then you may make "abc??", "?abc?", "??abc" in the first turn. (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)微信
If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn. If the sequence is not possible to stamp, return an empty array.app
For example, if the sequence is "ababc", and the stamp is "abc"
, then we could return the answer [0, 2]
, corresponding to the moves "?????" -> "abc??" -> "ababc".cors
Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within 10 * target.length
moves. Any answers specifying more than this number of moves will not be accepted.post
Example 1:this
Input: stamp = "abc", target = "ababc" Output: [0,2] ([1,0,2] would also be accepted as an answer, as well as some other answers.)
Example 2:spa
Input: stamp = "abca", target = "aabcaca" Output: [3,0,1]
Note:
1 <= stamp.length <= target.length <= 1000
stamp
and target
only contain lowercase letters.你想要用小寫字母組成一個目標字符串 target
。
開始的時候,序列由 target.length
個 '?'
記號組成。而你有一個小寫字母印章 stamp
。
在每一個回合,你能夠將印章放在序列上,並將序列中的每一個字母替換爲印章上的相應字母。你最多能夠進行 10 * target.length
個回合。
舉個例子,若是初始序列爲 "?????",而你的印章 stamp
是 "abc"
,那麼在第一回合,你能夠獲得 "abc??"、"?abc?"、"??abc"。(請注意,印章必須徹底包含在序列的邊界內才能蓋下去。)
若是能夠印出序列,那麼返回一個數組,該數組由每一個回合中被印下的最左邊字母的索引組成。若是不能印出序列,就返回一個空數組。
例如,若是序列是 "ababc",印章是 "abc"
,那麼咱們就能夠返回與操做 "?????" -> "abc??" -> "ababc" 相對應的答案 [0, 2]
;
另外,若是能夠印出序列,那麼須要保證能夠在 10 * target.length
個回合內完成。任何超過此數字的答案將不被接受。
示例 1:
輸入:stamp = "abc", target = "ababc" 輸出:[0,2] ([1,0,2] 以及其餘一些可能的結果也將做爲答案被接受)
示例 2:
輸入:stamp = "aabcaca", target = "abca" 輸出:[3,0,1]
提示:
1 <= stamp.length <= target.length <= 1000
stamp
和 target
只包含小寫字母。52ms
1 class Solution { 2 func movesToStamp(_ stamp: String, _ target: String) -> [Int] { 3 //轉換爲數組 4 var t:[Character] = target.toCharArray() 5 var s:[Character] = stamp.toCharArray() 6 7 var route:[Int] = [Int]() 8 let num:Int = t.count - s.count + 1 9 var done:[Bool] = [Bool](repeating:false,count:num) 10 while(true) 11 { 12 var up:Bool = false 13 for i in 0..<num 14 { 15 if done[i] {continue} 16 var cor = cors(s, t, i) 17 if cor == 1 18 { 19 route.insert(i, at: 0) 20 for j in 0..<s.count 21 { 22 t[j+i] = "." 23 } 24 up = true 25 done[i] = true 26 } 27 else if cor == 2 28 { 29 done[i] = true 30 } 31 } 32 if !up {break} 33 } 34 for i in 0..<t.count 35 { 36 if t[i] != "." 37 { 38 return [] 39 } 40 } 41 var ret:[Int] = [Int](repeating:0,count:route.count) 42 var p:Int = 0 43 for x in route 44 { 45 ret[p++] = x 46 } 47 return ret 48 } 49 50 func cors(_ s:[Character],_ t:[Character],_ f:Int) -> Int 51 { 52 var ex:Int = 0 53 for i in 0..<s.count 54 { 55 if t[i+f] == "." {continue} 56 ex = 1 57 if s[i] != t[i+f] {return 0} 58 } 59 if ex == 0 {return 2} 60 return 1 61 } 62 } 63 64 extension String { 65 //轉換爲字符數組 66 func toCharArray() -> [Character] 67 { 68 var chars:[Character] = [Character]() 69 for char in self.characters 70 { 71 chars.append(char) 72 } 73 return chars 74 } 75 } 76 /*擴展Int類,實現自增++運算符*/ 77 extension Int{ 78 //後綴++:先執行表達式後再自增 79 static postfix func ++(num:inout Int) -> Int { 80 //輸入輸出參數num 81 let temp = num 82 //num加1 83 num += 1 84 //返回加1前的數值 85 return temp 86 } 87 }