作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/occurrences-after-bigram/

题目描述

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.

题目大意

找出在一个长字符串text中,符合"first second third"模式的third。其中first和second是输入的。

解题方法

字符串分割遍历

对字符串进行分割,然后循环遍历,每次判断三个单词,看前两个是不是first和second,如果满足的话那么把第三个放到结果中即可。

时间复杂度是O(N)。

Python代码如下:

class Solution(object):
def findOcurrences(self, text, first, second):
"""
:type text: str
:type first: str
:type second: str
:rtype: List[str]
"""
words = text.split()
res = []
L = len(words)
for i in range(L - 2):
f, s, t = words[i], words[i + 1], words[i + 2]
if f == first and s == second:
res.append(t)
return res

C++代码的难点是字符串操作。C++竟然没有split()函数,这也是大家吐槽太多的点。一个可以替换的方案是使用stringstream类,该类会模拟字符串读入操作,即实现字符串按空格分割。初始化三个字符串,分别是f,s,t,读入到第三个里,前两个保留的是前两次读入的结果。这样进行判断是比较优雅的操作。

C++代码如下:

class Solution {
public:
vector<string> findOcurrences(string text, string first, string second) {
stringstream ss(text);
vector<string> res;
string f, s, t;
while (ss >> t) {
if (f == first && s == second)
res.push_back(t);
f = s;
s = t;
}
return res;
}
};

参考资料:
https://leetcode.com/problems/occurrences-after-bigram/discuss/308385/C%2B%2B-stringstream

日期

2019 年 6 月 9 日 —— 简单的题没有难度,需要挑战有难度的才行

【LeetCode】5083. Occurrences After Bigram 解题报告(Python & C++)的更多相关文章

  1. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  6. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  8. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  9. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

随机推荐

  1. python爬虫之正则表达式(用在其他地方也可)

    1. 常用的匹配规则 ### 常用的匹配规则 # \w 匹配字母.数字及下划线 # \W 匹配不是字母.数字及下划线的字符 # \s 匹配任意空白字符,等价于[\t\n\t\f] # \S 匹配任意非 ...

  2. MybatisPlus使用Wrapper实现查询功能

    Wrapper---条件查询器 :使用它可以实现很多复杂的查询 几个案例 环境: 参照博客:MybatisPlus入门程序 1.条件查询 1.1 查询name不为空的用户,并且邮箱不为空的用户,年龄大 ...

  3. C#页面缓存设置

    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Sessioninfo(); } Session.R ...

  4. linux系统中安装JDK

    安装之前的准备工作 查看系统中之前安装好的JDK java –version rpm -qa | grep java 卸载JDK (以java-1.7.0-openjdk-1.7.0.45-2.4.3 ...

  5. 分布式事务(3)---强一致性分布式事务Atomikos实战

    分布式事务(1)-理论基础 分布式事务(2)---强一致性分布式事务解决方案 分布式事务(4)---最终一致性方案之TCC 前面介绍强一致性分布式解决方案,这里用Atomikos框架写一个实战的dem ...

  6. 日常Java 2021/10/12

    封装 在面向对象程式设计方法中,封装是指-种将抽象性函式接口的实现细节部分包装.隐藏起来的方法 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问 要访问该类的代码和数据,必 ...

  7. GO类型转换

    golang []byte转string golang中,字符切片[]byte转换成string最简单的方式是 package main import ( "fmt" _ &quo ...

  8. my38_MySQL事务知识点零记

    从innodb中查看事务信息 show engine innodb status\G; ------------ TRANSACTIONS------------Trx id counter 3153 ...

  9. OceanBase 2.x体验:推荐用DBeaver工具连接数据库

    Original MQ4096 [OceanBase技术闲谈](javascript:void(0) 2020-01-15 OceanBase 2.x体验:推荐用DBeaver工具连接数据库 Ocea ...

  10. 通过Jedis操作Redis

    package com.yh; import org.junit.After; import org.junit.Before; import org.junit.Test; import redis ...