• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode936.戳印序列|StampingTheSequence

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9904208.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

You want to form a target string of lowercase letters.

At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters.

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.

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".

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.

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:

  1. 1 <= stamp.length <= target.length <= 1000
  2. 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. 1 <= stamp.length <= target.length <= 1000
  2. 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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode518.零钱兑换II|CoinChange2发布时间:2022-07-13
下一篇:
iOS swift语言生成条形码,可一次性生成多个!并带文字发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap