class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param message string字符串 * @param keyword string字符串 * @return int整型 */ int findKeyword(string message, string keyword) { // write code here int la = message.size(); int lb = keyword.size(); int i, j; for ( i = 0; i < la; ++i) { if (message[i] == keyword[0]) { // cout<<111<<endl; int cnt = i + 1; for ( j = 1; j < lb; ++j) { if (message[cnt] != keyword[j]) break; ++cnt; } // cout<<lb<<endl; if (j == lb ) return i; } } return -1; } };
一、题目考察的知识点
模拟
二、题目解答方法的文字分析
遍历一下即可,具体见代码
三、本题解析所用的编程语言
c++
方法二:库函数查找
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param message string字符串 * @param keyword string字符串 * @return int整型 */ int findKeyword(string message, string keyword) { // write code here int ans = message.find(keyword); if (ans == string::npos) return -1; else return ans; } };