LeetCode 28. Implement strStr()
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()相关推荐
- 【To Do】LeetCode 28. Implement strStr() 和KMP算法
LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...
- LeetCode - 28. Implement strStr()
28. Implement strStr() Problem's Link -------------------------------------------------------------- ...
- leetCode 28. Implement strStr() 字符串
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- [leetcode] 28. Implement strStr() 解题报告
题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...
- LeetCode 28 Implement strStr()(实现strStr()函数)
翻译 实现strStr()函数.返回针(needle)在草垛/针垛(haystack)上第一次出现的索引, 如果不存在其中则返回-1.其实也就是说字符串str2在字符串str1中第一次出现的索引而已. ...
- leetcode 28. Implement strStr() 实现strStr()
C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(need ...
- LeetCode - Easy - 28. Implement strStr()
Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...
- leetcode python3 简单题28. Implement strStr()
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十八题 (1)题目 英文: Implement strStr(). Return ...
- 【LeetCode】28. Implement strStr()
Difficulty:easy More:[目录]LeetCode Java实现 Description Implement strStr(). Return the index of the fi ...
- 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 ...
最新文章
- 江苏开放大学计算机应用基础第四次作业,江苏开放大学-计算机应用基础第四次.doc...
- 【Qt】QCloseEvent的使用小结
- Web API 接口-JavaScript全部api接口文档
- Python第三周 学习笔记(2)
- 2019-05-23 IRIS嗅探器;用IRIS嗅探数据;
- 简书python爬虫权威_python爬虫 --- 简书评论
- 三维重建 几何方法 深度学习_基于深度学习的视觉三维重建研究总结
- verilog将像素数据写入txt_FPGA仿真必备(1)——Matlab生成.mif文件/.txt文件
- php 动态 常量,PHP中的动态常量?
- Excel快速删除空白行与调整行高列宽的方法,学会了很实用
- Java 实现 图片OCR文字识别
- 「x86」- 特权级(Privilege Level)学习笔记 @20210215
- RFID应用安全+物联网安全标准
- centos搭建mysql、nginx、nodejs、screen
- 广和通携手联发科技正式发布基于MediaTek T830 平台5G模组FG370的可快速落地FWA解决方案
- 微信上的文件怎样用计算机打出来,微信上的文件传到电脑上怎么打印出来
- Symbian知识汇集
- 什么是亚像素(子像素)?sub-pixel
- QT学习之QMainWindow详解
- MATLAB/Simulink搭建电动助力转向模型