28.Implement strStr()【leetcod】
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack
public class Solution {
public int strStr(String haystack, String needle) {
if(haystack==null||needle==null)
return -1;
int lenHay =haystack.length();
int lenNeed=needle.length();
for(int i=0;i<=lenHay-lenNeed;i++){
int j=0;
for(j=0;j<lenNeed;j++){
if(haystack.charAt(i+j)!=needle.charAt(j)){
return -1;
// break;
}
}
if(j==lenNeed)
return i;
}
return -1;
}
}
解题思路:首先本题考查的为strStr()函数的实现
strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。
判断大字符串A和子串B,他们长度分别为L1和L2,这个时候i 在0-(L1-L2)区间内:记为可能出现的返回值。即子串的第j个字母在A串中的位置为i+j
如果最后子串的每个字母在母串中都有匹配到那么范围i的位置即可,否则证明不包含子串,返回-1即可
士不可以三日不读书,故士别三日当刮目相待!晚安,北京!
28.Implement strStr()【leetcod】的更多相关文章
- 28. Implement strStr()【easy】
28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...
- c语言 Implement strStr()【Leetcode】
实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #def ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- leetCode练题——28. Implement strStr()
1.题目 28. Implement strStr()——Easy Implement strStr(). Return the index of the first occurrence of ne ...
- 【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()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- 【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
随机推荐
- 【Android Developers Training】 87. 序言:同步到云
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- 【Android Developers Training】 82. 序言:传输数据时减少对电池寿命的影响
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- mac下git配置和jenkins打包
今天自己配置了下jenkins,还算是比较顺利,把整个过程和大家分享下. 1. 查看秘钥是否存在 打开终端查看是否已经存在SSH密钥:cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除, ...
- Unity 游戏框架搭建 (一) 概述
为了重构手头的一款项目,翻出来当时未接触Unity时候收藏的视频<Unity项目架构设计与开发管理>,对于我这种初学者来说全是干货.简单的总结了一下,以后慢慢提炼. 关于Unity的架 ...
- 关于jquery全选反选 批量删除的一点心得
废话不多说直接上代码: 下面是jsp页面的html代码: <table id="contentTable" class=""> <thead& ...
- 计算机浏览器存储技术cookie、sessionStorage、localStorage
HTTP无状态协议是指协议对于事务处理没有记忆能力.会话跟踪协议的状态是指下一次传输可以"记住"这次传输信息的能力,无状态是指同一个会话(注意什么叫同一个会话)的连续两个请求互相不 ...
- zookeeper初试
实验环境: os-platform: windows7 x64 jdk: 1.7 参考文档: http://www.ibm.com/developerworks/cn/opensource/os-cn ...
- JQuery中常用的选择器
属性选择器 1> [attribute] 概述:匹配包含给定属性的元素. 示例 jQuery 代码:$("div[id]") 描述:查找所有含有 id 属性的 div 元素 ...
- 快学Scala之继承
## 1. 继承 Scala语言通过 extends 关键字来继承类. 那么继承一个类有什么好处呢? 子类除了拥有继承自超类的方法和字段(即为val(常量), var(变量)所定义的), 还可 ...
- 常见MD5加密解密值及免费解密网站
常用的MD5解密 MD5(admin,16) = 7a57a5a743 MD5(admin,16) = 7a57a5a743894a0e MD5(admin888,16) = 469e ...