https://leetcode.com/problems/implement-strstr/

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

  • 字符串简单题。一定要写的没BUG。两种解法,一种用string::find函数,另一种比较字符串。
  • 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR。
Line 27: control reaches end of non-void function [-Werror=return-type]

  • strstr - C++ Reference

    •   http://www.cplusplus.com/reference/cstring/strstr/?kw=strstr
  • 注意haystack.size() - needle.size() + 1需要显示转换为int,否则值为-1时unsigned long会自动转变

 1 //
 2 //  main.cpp
 3 //  LeetCode
 4 //
 5 //  Created by Hao on 2017/3/16.
 6 //  Copyright © 2017年 Hao. All rights reserved.
 7 //
 8
 9 #include <iostream>
10 #include <cstring>
11 #include <vector>
12 #include <cmath>
13 using namespace std;
14
15 class Solution {
16 public:
17     // with string::find function
18     int strStr(string haystack, string needle) {
19         return (haystack.find(needle) != string::npos) ? haystack.find(needle) : -1;
20     }
21
22     // compare string
23     int strStr1(string haystack, string needle) {
24         // Corner case
25         if (needle.empty()) return 0; // for both empty string should return 0
26         if (haystack.empty()) return -1;
27
28         bool flag;
29         int n = haystack.size() - needle.size() + 1; // pay attention to + 1
30
31         // better use < instead of <=
32         for (int i = 0; i < n; i ++) {
33             flag = true;
34
35             for (int j = 0; j < needle.size(); j ++)
36                 if (haystack.at(i + j) != needle.at(j)) {
37                     flag = false;
38                     break;
39                 }
40
41             if (flag)
42                 return i;
43         }
44
45         return -1;
46     }
47 };
48
49 int main(int argc, char* argv[])
50 {
51     Solution    testSolution;
52     string      result;
53
54     vector<vector<string>> sVec = {{"aaa","aaaa"}, {"", ""}, {"aaaaa", "bba"}, {"hello", "ll"}};
55
56     /*
57      -1
58      -1
59      0
60      0
61      -1
62      -1
63      2
64      2
65      */
66     for (auto s : sVec) {
67         cout << testSolution.strStr(s[0], s[1]) << endl;
68         cout << testSolution.strStr1(s[0], s[1]) << endl;
69     }
70
71     return 0;
72 }

View Code

转载于:https://www.cnblogs.com/pegasus923/p/5615555.html

LeetCode 28. Implement strStr()相关推荐

  1. 【To Do】LeetCode 28. Implement strStr() 和KMP算法

    LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...

  2. LeetCode - 28. Implement strStr()

    28. Implement strStr() Problem's Link -------------------------------------------------------------- ...

  3. leetCode 28. Implement strStr() 字符串

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  4. [leetcode] 28. Implement strStr() 解题报告

    题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...

  5. LeetCode 28 Implement strStr()(实现strStr()函数)

    翻译 实现strStr()函数.返回针(needle)在草垛/针垛(haystack)上第一次出现的索引, 如果不存在其中则返回-1.其实也就是说字符串str2在字符串str1中第一次出现的索引而已. ...

  6. leetcode 28. Implement strStr() 实现strStr()

    C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(need ...

  7. LeetCode - Easy - 28. Implement strStr()

    Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...

  8. leetcode python3 简单题28. Implement strStr()

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...

  9. 【LeetCode】28. Implement strStr()

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...

  10. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

最新文章

  1. 江苏开放大学计算机应用基础第四次作业,江苏开放大学-计算机应用基础第四次.doc...
  2. 【Qt】QCloseEvent的使用小结
  3. Web API 接口-JavaScript全部api接口文档
  4. Python第三周 学习笔记(2)
  5. 2019-05-23 IRIS嗅探器;用IRIS嗅探数据;
  6. 简书python爬虫权威_python爬虫 --- 简书评论
  7. 三维重建 几何方法 深度学习_基于深度学习的视觉三维重建研究总结
  8. verilog将像素数据写入txt_FPGA仿真必备(1)——Matlab生成.mif文件/.txt文件
  9. php 动态 常量,PHP中的动态常量?
  10. Excel快速删除空白行与调整行高列宽的方法,学会了很实用
  11. Java 实现 图片OCR文字识别
  12. 「x86」- 特权级(Privilege Level)学习笔记 @20210215
  13. RFID应用安全+物联网安全标准
  14. centos搭建mysql、nginx、nodejs、screen
  15. 广和通携手联发科技正式发布基于MediaTek T830 平台5G模组FG370的可快速落地FWA解决方案
  16. 微信上的文件怎样用计算机打出来,微信上的文件传到电脑上怎么打印出来
  17. Symbian知识汇集
  18. 什么是亚像素(子像素)?sub-pixel
  19. QT学习之QMainWindow详解
  20. MATLAB/Simulink搭建电动助力转向模型

热门文章

  1. Sympy符号计算库
  2. python变量及其作用域,闭包
  3. 基于ssh框架mysql的jsp系统远吗_JSP+SSH+Mysql实现的学生管理系统
  4. ubuntu MySQL-python 安装失败解决方法
  5. vue中使用kindeditor编辑器_Vue中使用wangEditor富文本编辑器(示例代码)
  6. L1-014 简单题 (5 分)—团体程序设计天梯赛
  7. githug-54-git练习
  8. Python入门:局部变量与全局变量1
  9. 数据结构和算法:线性表链式存储的简单实现
  10. Jmeter学习笔记ONE