实现 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() 定义相符。

#include <iostream>
using namespace std; int strStr(string haystack, string needle)
{
if(haystack.length()<needle.length())
return -;
if(haystack.length()==||needle.length()==)
return ; int ans=-;
for(int i=;i<haystack.length()-needle.length()+;i++)
{ if(haystack[i]==needle[])
{
int k;
//ans=i;
for(k=;k<needle.length();k++)
{
if(haystack[i+k]!=needle[k])
{k=;break;}
}
if(k==needle.length()) return i;
} }
return ans;
} int main() {
string s="mississippi"; string ss= "issip";
int ans=strStr(s,ss);
std::cout << ans << std::endl;
return ;
}

#leetcode刷题之路28-实现 strStr() 函数的更多相关文章

  1. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  2. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  3. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  5. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  6. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  7. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

  8. #leetcode刷题之路6- Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L     C     I ...

  9. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

随机推荐

  1. Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. 微信小程序开发7-JavaScript脚本

    1.小程序的主要开发语言是 JavaScript ,开发者使用 JavaScript 来开发业务逻辑以及调用小程序的 API 来完成业务需求. 2.ECMAScript 在大部分开发者看来,ECMAS ...

  3. Appium元素定位(uiautomatorviewer)

    一.uiautomatorviewer元素定位 1.adroid-sdk的安装目录tools下有1个自带的工具uiautomatorviewer,打开后,如下所示: 点击后,如图所示: 步骤: a.链 ...

  4. [翻译] JSAnimatedImagesView

    JSAnimatedImagesView 本人测试的效果: Description:描述 Easy to use UIView subclass to quickly add a cool anima ...

  5. Effective C++(7) 为多态基类声明virtual析构函数 or Not

    问题聚焦: 已经对一个对象执行了delete语句,还会发生内存泄漏吗? 先来看个demo: // 计时器类 class TimeKeeper { public: TimeKeeper(); ~Time ...

  6. NJCTF 2017 web pictures'wall(详解)

    题目: 图片墙上有图片 url:http://218.2.197.235:23719/ writeup: 首先各种尝试登陆,发现任意用户及密码都可以登陆,但登陆后的页面提示的是“Root has pr ...

  7. December 23rd 2016 Week 52nd Friday

    Life is a horse, and either you ride it or it rides you. 人生像一匹马,你不驾驭它,它便驾驭你. It is the same meaning ...

  8. ZT 设计模式六大原则(2):里氏替换原则

    设计模式六大原则(2):里氏替换原则 分类: 设计模式 2012-02-22 08:46 23330人阅读 评论(41) 收藏 举报 设计模式class扩展string编程2010 肯定有不少人跟我刚 ...

  9. iPhone的设备名转换

    def convertDeviceName(self, deviceName): """ 转换deviceName(如iPhone 6,2)为用户习惯形式(如iPhone ...

  10. pip 升级 Appium-Python-Client

    第一种方法: pip install --upgrade Appium-Python-Client 如果出现权限提醒: sudo -H pip install --upgrade Appium-Pyt ...