领扣LintCode算法问题答案-1902. 寻找Google
目录
1902. 寻找Google描述样例 1:样例 2:
题解鸣谢
1902. 寻找Google
描述
给您一个字符串形式的C ++文件(每行是一个字符串),我们希望您在注释行中找到“ Google”。如果注释行中有“ Google”,则返回true,否则返回false。 C++有两种注释方式,一种是单行注释 //,代表着//后面的本行内容均为注释,另一种是多行注释,/和/ 这两者之间的部分均为注释。
保证“Google”字符串在一个string内,不会换行
样例 1:
输入: S = ["#include<bits/stdc++.h>","using namespace std;","//Google test","int main(){","return 0;","}"]
输出: true
样例 2:
输入: S = ["#include<bits/stdc++.h>","using namespace std;","int main(){","int Gogle = 0","return 0;","}"]
输出: false
说明: The google is not in commet line.
题解
public class Solution {
public boolean FindGoogle(List
<String> S
) {
boolean mulStart
= false;
for (String s
: S
) {
int startIndex
= s
.indexOf("//");
if (mulStart
|| startIndex
>= 0) {
if (startIndex
< 0) {
startIndex
= 0;
}
if (s
.indexOf("Google", startIndex
) >= 0) {
return true;
}
} else {
int lastMulStartIndex
= s
.lastIndexOf("/*");
int lastMulEndIndex
= s
.lastIndexOf("*/");
if (lastMulStartIndex
> lastMulEndIndex
) {
mulStart
= true;
} else {
mulStart
= false;
}
}
}
return false;
}
}
原题链接点这里
鸣谢
非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。 欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。