leetcode-28.实现strStr()
题意
实现 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() 定义相符。
算法
如果haystack字符串为空,直接返回0;
若needle长度大于haystack,返回-1;
遍历给定haystack字符串,如果存在元素与needle字符串的首字符相同,继续;否则,返回 -1;
比较后续字符串,若都相同,返回当前索引值。(可以使用string.substr(index1, len))来实现
code
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle == "")
return ;
int ans = -;
int i, j;
bool flag;
for(i=; i<haystack.length(); i++)
{
if(haystack[i] == needle[])
{
flag = true;
for(j=; j<needle.length(); j++)
{
if(haystack[i+j] != needle[j])
{
flag = false;
break;
}
}
if(flag == true)
{
ans = i;
break;
}
}
}
return ans;
}
};
后来更新的
leetcode-28.实现strStr()的更多相关文章
- 前端与算法 leetcode 28.实现 strStr()
# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和 ...
- Java实现 LeetCode 28 实现strStr()
28. 实现 strStr() 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- <每日 1 OJ> -LeetCode 28. 实现 strStr()
题目: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存 ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
随机推荐
- 工欲善其事,必先利其器-Python编辑器选择(2)
前言:工欲善其事.必先利其器 一款顺手的好的编辑器可以让程序员写代码更得心应手,效率也会更高,但是编辑器本身没有好坏,只有使用者使用起来是否顺手而已,这里简单给大家介绍几款常用的可以编辑Python的 ...
- python(leetcode)-344反转字符串
编写一个函数,其作用是将输入的字符串反转过来.输入字符串以字符数组 char[] 的形式给出. 不要给另外的数组分配额外的空间,你必须原地修改输入数组.使用 O(1) 的额外空间解决这一问题. 你可以 ...
- maven下载其源代码包并关联
有时我们的源码可能会失去关联,然后需要我们在当前项目中(含有pom.xml文件的那个目录)按住shift然后在当前项目文件夹空白处右键,选择在此处打开命令输入一下命令: 1. 下载所有在POM中的的s ...
- Nginx 搭建图片缓存服务器-转
文章:https://waver.me/2019/04/11/Nginx-Cache-Server/ 参考: Nginx 配置详解Nginx 简易教程Nginx 配置总结
- 【EF6学习笔记】(三)排序、过滤查询及分页
本篇原文地址:Sorting, Filtering, and Paging 说明:学习笔记参考原文中的流程,为了增加实际操作性,并能够深入理解,部分地方根据实际情况做了一些调整:并且根据自己的理解做了 ...
- docker - 容器里安装redis
在docker中安装redis 使用命令行安装redis 下载并解压 wget http://download.redis.io/releases/redis-3.2.6.tar.gz tar -xv ...
- spring学习(二) ———— AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
- 第一册:lesson seventy five。
原文: Uncomfortable Shoes. Do you have any shoes like this? What size? Size five. What color? Black. I ...
- EF to Sqlite
测试下来,使用到下面的版本: EF6.1 System.Data.SQLite.EF6.1.0.93.0 System.Data.SQLite.Core.1.0.93.0 注意事项: 设置Autoi ...
- Android Studio 使用Toast
Toast 是Android系统中体重的一种非常好的提醒方式 在程序中可以将很小的一段消息提醒给用户 在一段时间后自动消失,不会占用如何屏幕空间 Button button1=(Button) fin ...