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 ...
随机推荐
- 闭包中this指向window的原因
var t={ b:1, w:function a(){ var b=2; alert(this.b); //弹出t对象的b属性 alert(b); //弹出a函数的b变量 return functi ...
- css 样式 设置图片成为圆形
<div style="float: left;border-radius:70%; height: 80px; overflow:hidden;"> <img ...
- JUnit【1】断言用法之assertEquals/True/False/ArrayEquals
前段时间去亚信面试,被问到写一个冒泡排序,心想这多新鲜,刷刷几下写好.面试官突然问,你怎么对这个程序进行单元测试? 单元测试?! 懵圈... 单元测试 代码是为了什么, ...
- 从Java熟练到Android入门
刚刚从学校出来,唉,从Java转入Android. 当初老师告诉我们Android不重要,结果,Android的所有课不是在玩手机就是在说话,没认真听也没认真看,作业也没认真做,现在想想好后悔啊,以至 ...
- C# 模拟跑马灯效果(2种)
#region 跑马灯效果方法 /// <summary> /// 文字进入左侧后从右侧出来 /// </summary> private void LabelRun() { ...
- angular.js ng-repeat渲染时出现闪烁问题解决
当我们前端运用到angular.js框架时,想必大家都会遇到一些坑.其中,我也来分享一个常见的angular.js渲染时出现的坑. 当我们进行页面渲染时,绑定表达式最开始会用{{data.name}} ...
- c# 上传附件大小限制的问题
在c# 相关的asp.net 中.需要设置附件的大小.需要修改2部分. 1.修改metabase.XML 以Windows2003 为例子. 打开 C:\Windows\System32\Inets ...
- 工厂设计模式 Factory
Factory 主要用来实例化有共同接口的类,工厂模式可以动态决定应该实例化那一个类. 例如:汽车销售商场 该模式将创建对象的过程放在了一个静态方法中来实现.在实际编程中,如果需要大量的创建对象,该模 ...
- Java之初识
今天开始学习Java 1.什么是Java? Java是1995年由sun公司推出的一门极富创造力的面向对象编程语言,是由Java之父詹姆斯格斯林博士设计的. Java名字的由来:据说,java刚刚设计 ...
- c#编程-线程同步
线程同步 上一篇介绍了如何开启线程,线程间相互传递参数,及线程中本地变量和全局共享变量区别. 本篇主要说明线程同步. 如果有多个线程同时访问共享数据的时候,就必须要用线程同步,防止共享数据被破坏.如果 ...