题目描述:
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
int strStr(string haystack, string needle) 
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
要完成的函数:
int strStr(string haystack, string needle)
代码:
int strStr(string haystack, string needle) {if(needle.size()==0&&haystack.size()!=0)return 0;else if(needle.size()!=0&&haystack.size()==0)return -1;else if(needle.size()==0&&haystack.size()==0)return 0;else{int changdu=haystack.size()-needle.size();// **for(int i=0;i<=changdu;i++) //**{if(haystack.substr(i,needle.size())==needle)return i;}return -1;}}
注意事项:
1、边界情况的判定。haystack为空?needle为空?haystack比needle长度小?
2、haystack.substr(x,y),x为起始点index,y为长度,注意不可以让x和y超过haystack的限制
3、string的size不是int类型,估计为unsigned类型。比如haystack="",needle="a",当cout<<haystack.size()-needle.size()<<endl;的时候,得到的会是一堆乱码,这时候强制类型转换,cout<<int(haystack.size()-needle.size())<<endl;才会得到-1的结果,这点在//**那里体现出来了。笔者在第一次submit的时候也犯了这个错误。

注意事项:

1、边界情况的判定。haystack为空?needle为空?haystack比needle长度小?

2、haystack.substr(x,y),x为起始点index,y为长度,注意不可以让x和y超过haystack的限制。

3、string的size不是int类型,估计为unsigned类型。比如haystack="",needle="a",当cout<<haystack.size()-needle.size()<<endl;的时候,得到的会是很奇怪的数字,这时候强制类型转换,cout<<int(haystack.size()-needle.size())<<endl;才会得到-1的结果,这点在代码中//**体现出来了。

//各位老哥知道怎么调整博客的行间距吗?好像编辑器没有行间距的选项,

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. P1516 青蛙的约会
  2. JFrame 居中显示
  3. html中什么标签可作容器,HTML容器标签和文本标签
  4. SAP UI5 初学者教程之二十三 - 列表控件的排序 Sort 和分组 Group 试读版
  5. 双稳态继电器工作原理图_固态继电器只有单一的作用吗?带你了解不一样的固态继电器...
  6. Axios的Vue插件(添加全局请求/响应拦截器)
  7. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 23丨学生们参加各科测试的次数【难度简单】​
  8. mysql操作符_MySql 中的=操作符
  9. 百度在美国遭集体起诉;iPhone 11 成苹果最畅销机型;OpenSSL 曝高危漏洞 | 极客头条...
  10. VmBox硬盘容量调整
  11. 游戏服务器架构通识 BigWorlds丨skynet
  12. [转载整理]计算机视觉及机器学习牛人和资料
  13. Python QT5文件对话框总是错误代码-1073740791 (0xC0000409)
  14. 有孚网络协办“大数据助力精准医疗产业论坛”北京站圆满落幕
  15. java adobe ocr_Acrobat自带的OCR识别文本功能提取图片文字
  16. 华为三层交换机路由配置案例_华为三层交换与路由配置
  17. 解决Ubuntu DVD 用wubi 安装时候要重新下载镜像的笨方法
  18. 适合学生党无线蓝牙耳机,好用又实惠蓝牙耳机推荐
  19. IBM_System_x3650服务器固件升级手顺
  20. 面试官:请你谈谈ConcurrentHashMap

热门文章

  1. 使用BDM和JTAG调试MPC8xx
  2. 中职计算机最好大专学校,成都计算机大专哪个好,成都最好的计算机大专
  3. 安卓平板运行windows软件,安卓平板运行虚拟win10
  4. 理光复印机怎么扫描到文件夹服务器里,理光复印机扫描到文件夹设置方法.docx...
  5. mysql每天创建表_Navicat for MySQL 每天自动创建数据表
  6. java hashbasedtable_Guava类库学习--Table(双键的Map)
  7. 20个必不可少的Python库也是基本的第三方库(转载)
  8. 面试问题(java cas有什么优点和问题)
  9. 基于java的推箱子游戏系统设计与实现(项目报告+答辩PPT+源代码+部署视频)
  10. gpu和显卡的关系,gpu和cpu的区别