在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ You want to form a At the beginning, your sequence is 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 For example, if the initial sequence is "?????", and your stamp is 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. For example, if the sequence is "ababc", and the stamp is Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within Example 1: Input: stamp = [0,2]
([1,0,2] would also be accepted as an answer, as well as some other answers.)
Example 2: Input: stamp = [3,0,1]
Note:
你想要用小写字母组成一个目标字符串 开始的时候,序列由 在每个回合,你可以将印章放在序列上,并将序列中的每个字母替换为印章上的相应字母。你最多可以进行 举个例子,如果初始序列为 "?????",而你的印章 如果可以印出序列,那么返回一个数组,该数组由每个回合中被印下的最左边字母的索引组成。如果不能印出序列,就返回一个空数组。 例如,如果序列是 "ababc",印章是 另外,如果可以印出序列,那么需要保证可以在 示例 1: 输入:stamp = "abc", target = "ababc" 输出:[0,2] ([1,0,2] 以及其他一些可能的结果也将作为答案被接受) 示例 2: 输入:stamp = "aabcaca", target = "abca" 输出:[3,0,1] 提示:
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 }
|
请发表评论