Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

题意:

实现strStr() : Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

Solution1:Two Pointers, finding substring[i...j] in str1,such that it equals str2

code:

 /*
Time: O(n^2).
Space: O(1).
*/ class Solution {
public int strStr(String s1, String s2) {
//题意确认 return 0 when needle is an empty string
if(s2.length() == 0) return 0; //for(int i = 0; i < s1.length(); i++){ 确保s1中含有s2,则扫s1的指针的范围可以缩小到s1.length() - s2.length() + 1
for(int i = 0; i < s1.length() - s2.length() + 1; i++){
int j = i;
int k = 0;
while( j < s1.length() && k < s2.length() && s1.charAt(j) == s2.charAt(k)){
j++;
k++;
}
if( k == s2.length()){
return i;
}
}
return -1;
}
}

[leetcode]28. Implement strStr()实现strStr()的更多相关文章

  1. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  2. [LeetCode] 28. Implement strStr() 实现strStr()函数

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  4. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. [LeetCode] 28. Implement strStr() 解题思路

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  6. Leetcode 28——Implement strStr()

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  8. [LeetCode] 28. Implement strStr() ☆

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. LeetCode——28. Implement strStr()

    题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...

随机推荐

  1. Dynamics 365 CRM 添加自定义按钮

    在添加自定义按钮之前,我们需要下载这个工具 RibbonWorkbench, 它是专门针对自定义命令栏和Ribbon区域. 下载之后是一个zip压缩包. 怎样安装RibbonWorkbench: Se ...

  2. 1.1.18 Word里将“字体加红”自定义为快捷方式

    1.单击[文件]>[选项]: 2.选择[自定义功能区],单击[键盘快捷方式]旁的[自定义]: 3.在[类别]菜单中选择[开始选项卡]:在[命令]菜单中选择[FontColorPicker] (或 ...

  3. [转]boost::python开发环境搭建

    转自:http://www.cnblogs.com/gaoxing/p/4317051.html 本来想用mingw编译boost::python模块,网上看了下资料太少,只有使用vs2012 操作环 ...

  4. Properties类学习笔记

    1.Properties是一个hashTable子类,但它只装String类型的值2.一个Properties集中有一个子Properties集为它的默认属性集,如果在Properties中找不到相关 ...

  5. day2模块初识别

    sys_mod.py import sys print(sys.argv) #['C:/Users/Administrator/desktop/s17/day2/sys_mod.py'] 打印脚本的绝 ...

  6. MOBA英雄AI设计分享

    转自:http://www.gamelook.com.cn/2018/07/333877 文/wataloo 1  设计概要 1.1  设计原则和目的 英雄AI的目的主要有: 1.新手过渡局,让玩家刚 ...

  7. mybatis的简单使用调用mapper接口

    mybatis 是apache下的一个面向sql编程的半自动化的ORM持久层的框架.特点:面向sql编程,达到高性能的使用目的. 下面是简单使用 现导入jar包,只有mybatis和数据库驱动包(这里 ...

  8. 用GDB调试程序(三)

    四.维护停止点 上面说了如何设置程序的停止点,GDB中的停止点也就是上述的三类.在GDB中,如果你觉得已定义好的停止点没有用了,你可以使用delete.clear.disable.enable这几个命 ...

  9. wepy 小程序云开发

    小程序前段时间更新了云开发的功能,刚听到对时候觉得简直是个人开发者对福音,尤其是对我这样对后端不是很懂对前端傻傻,简直不能太方便,就试了下. 体验二维码: 源码:https://github.com/ ...

  10. 性能测试day07_性能瓶颈和分析

    其实如果之前都做的很到位的话,那么再加上APM工具(dynaTrace等),监控到非常细节,那么我们跑一个业务,我们就能完全清楚的知道每个请求的时间,也能知道请求所产生sql的时间,这样你自然而然都知 ...