LeetCode(28): 实现strStr()
Easy!
题目描述:
实现 strStr() 函数。
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1:
输入: haystack = "hello", needle = "ll" 输出: 2
示例 2:
输入: haystack = "aaaaa", needle = "bba" 输出: -1
说明:
当 needle
是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle
是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
解题思路:
这道题让我们在一个字符串中找另一个字符串第一次出现的位置,那我们首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回-1。
然后我们开始遍历母字符串,我们并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符串相等的位置即可,这样可以提高运算效率。然后对于每一个字符,我们都遍历一遍子字符串,一个一个字符的对应比较,如果对应位置有不等的,则跳出循环,如果一直都没有跳出循环,则说明子字符串出现了,则返回起始位置即可。
C++解法:
1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if (needle.empty()) return 0; 5 int m = haystack.size(), n = needle.size(); 6 if (m < n) return -1; 7 for (int i = 0; i <= m - n; ++i) { 8 int j = 0; 9 for (j = 0; j < n; ++j) { 10 if (haystack[i + j] != needle[j]) break; 11 } 12 if (j == n) return i; 13 } 14 return -1; 15 } 16 };
转载于:https://www.cnblogs.com/ariel-dreamland/p/9134059.html
LeetCode(28): 实现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 ...
- strstr函数_[LeetCode] 28. 实现strStr()
题目链接: https://leetcode-cn.com/problems/implement-strstr/ 题目描述 实现 strStr() 函数. 给定一个 haystack 字符串和一个 n ...
- LeetCode 28. 实现strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- [leetcode] 28. Implement strStr() 解题报告
题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...
- leetcode 28.实现strStr()
题目 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在 ...
- leetcode 28. Implement strStr() 实现strStr()
C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(need ...
- LeetCode 28 实现 strStr()
https://leetcode-cn.com/problems/implement-strstr/ 解决方案 class Solution {public int strStr(String hay ...
- Leetcode 28. 实现 strStr()
原题链接 解:KMP算法 class Solution { public:int strStr(string s, string p) {if (p.empty()) return 0;int n = ...
最新文章
- unity3d--为组件添加声音
- intellij idea 实用快捷键
- 利用ImageMagick命令执行漏洞拿下Facebook四万美元奖金
- oracle sql练习_数据分析之学习SQL
- opencv 读写XML YML
- 属性 Owner 不可用于 数据库...
- eslint airbnb 不允许尾随逗号
- high-speed A/D performance metrics and Amplifie...
- springboot整合ehcache+redis实现双缓存
- 学习HTML5+CSS3的第一天
- excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
- 【华为认证考试扫盲】超详细的华为认证入门基础知识,考证必看。
- 2021版itunes不备份更新ios系统
- 被面试官问到项目中的难点?是时候对自己的项目进行总结了(记一次项目问题总结)
- Arduino Leonardo教程:如何回车,特殊按键定义,DIY超便宜的键盘主控
- mailgun php版本,php – Mailgun发送带附件的邮件
- 微信小程序 非webview分享给好友及生成分享海报
- http网站怎么配置https防劫持
- C++ 栈的括号匹配
- 怎样提问 上 -Leo读提问的智慧 2
热门文章
- Linux中的15个‘echo’ 命令实例
- AtomicReference 原子引用
- 1 课外笔记之网页动画——jsp系列问题
- 在javaweb中通过servlet类和普通类读取资源文件
- js 之for..in、表单及事件触发
- ajax servlet增删改查,Servlet ajax 文件上传和JDBC+Servler用户表增删改查
- python里clear和copy_python之字典
- 如何快速实现 Wordpress 博客域名更换?
- 少儿编程150讲轻松学Scratch(三)-关卡类游戏《鱼塘》
- q版地图制作软件_Flash动画的图形元件实例-Q版人物侧面行走