新天。为有牺牲多壮志,敢教日月换

[Swift]LeetCode1078. Bigram 分词 | Occurrences After Bigram

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

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

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

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

Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second.

For each such occurrence, add "third" to the answer, and return the answer.

Example 1:

Input: text = "alice is a good girl she is a good student", first = "a", second = "good"
Output: ["girl","student"]

Example 2:

Input: text = "we will we will rock you", first = "we", second = "will"
Output: ["we","rock"] 

Note:

  1. 1 <= text.length <= 1000
  2. text consists of space separated words, where each word consists of lowercase English letters.
  3. 1 <= first.length, second.length <= 10
  4. first and second consist of lowercase English letters.

给出第一个词 first 和第二个词 second,考虑在某些文本 text 中可能以 "first second third" 形式出现的情况,其中 second 紧随 first 出现,third 紧随 second 出现。

对于每种这样的情况,将第三个词 "third" 添加到答案中,并返回答案。 

示例 1:

输入:text = "alice is a good girl she is a good student", first = "a", second = "good"
输出:["girl","student"]

示例 2:

输入:text = "we will we will rock you", first = "we", second = "will"
输出:["we","rock"] 

提示:

  1. 1 <= text.length <= 1000
  2. text 由一些用空格分隔的单词组成,每个单词都由小写英文字母组成
  3. 1 <= first.length, second.length <= 10
  4. first 和 second 由小写英文字母组成

4ms
复制代码
 1 class Solution {
 2     func findOcurrences(_ text: String, _ first: String, _ second: String) -> [String] {
 3       let subString = first + " " + second
 4       let arr = text.split(separator: " ")
 5       var result = [String]()
 6       for i in 0..<arr.count-2 {
 7         if arr[i] == first && arr[i+1] == second {
 8           result.append(String(arr[i+2]))
 9         }
10       }
11       return result
12     }
13 }
复制代码

Runtime: 8 ms

Memory Usage: 21.7 MB
复制代码
 1 import Foundation
 2 class Solution {
 3     func findOcurrences(_ text: String, _ first: String, _ second: String) -> [String] {
 4         if text.isEmpty {return []}
 5         var words:[String] = text.components(separatedBy: " ")
 6         var list:[String] = [String]()
 7         for i in 2..<words.count
 8         {
 9             if first == words[i-2] && second == words[i-1]
10             {
11                 list.append(words[i])
12             }
13         }
14         return list
15     }
16 }
复制代码

8ms

复制代码
 1 class Solution {
 2     func findOcurrences(_ text: String, _ first: String, _ second: String) -> [String] {
 3         var answer: [String] = []
 4         
 5         var words = Array(text.split(separator: " "))
 6         
 7         var i = 0
 8                 
 9         while (i + 2) < words.count {
10             if words[i] == first, words[i + 1] == second {
11                 answer.append(String(words[i + 2]))
12             }
13             
14             i += 1
15         }
16 
17         return answer
18     }
19 }
复制代码

 

posted @   为敢技术  阅读(551)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°