Leetcode : eImplement strStr
Leetcode : eImplement strStr
描述
对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。
如果不让你采用正则表达式,你会怎么做呢?
思路:
1、 先排除source为null、target为null的情况
2、 如果target的length为0,则返回0
3、 如果target的length>source的length,则返回-1
4、 定义一个for循环遍历source字符串,循环比对target字符串
4.1、在for循环外部定义一个索引index,用来索引target,每一次循环,只有比对成功,index++;然后比对index和target的length长度,如果相同,则返回下标
4.2、当比对不成功时,index重新赋值为0;需要重新比对一下,source[i]和target[0]
好了,我们跟着思路来实现一下代码
public static int strstr(String source , String target ) {
if(source == null || target == null ) {
return -1;
}
int m = source.length();
int n = target.length();
if(n == 0) {
return 0;
}
if(n > m ) {
return -1;
}
int index = 0;
for(int i = 0 ; i < m ; i++ ) {
if( source.charAt(i) == target.charAt(index) ) {
index++;
if(index == n) {
//index多加了一次
return i - index + 1 ;
}
}else {
index = 0;
//source[i]需要和target[0]重新比对一下
if( source.charAt(i) == target.charAt(index) ) {
index++;
}
}
}
return -1;
}
补充:

所需知识:
List是有序(有序是指放入地顺序)的collection。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素地整数索引访问元素。
List通常允许有重复的元素;更确切地讲,List通常允许满足e1.equals(e2)的元素对,并且如果List本身允许null元素的话,通常它们允许多个null元素;List只是一个接口
Set是一个不包含重复元素的collection。更确切地讲,set不包含满足e1.equals(e2)的元素对,并且最多包含一个null元素
Set: HashSet(无序的),LinkedHashSet,TreeSet,EnumSet(后三个有序)
知道了这些,这题的答案明显就是A了
以后Leetcode系列的文章都是这种风格了,题+题;对java面试还是比较有用的......
Leetcode : eImplement strStr的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [Leetcode] implement strStr() (C++)
Github leetcode 我的解题仓库 https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...
- LeetCode OJ--Implement strStr()
http://oj.leetcode.com/problems/implement-strstr/ 判断一个串是否为另一个串的子串 比较简单的方法,复杂度为O(m*n),另外还可以用KMP时间复杂度为 ...
- LeetCode Implement strStr()(Sunday算法)
LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- leetcode implement strStr python
#kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...
- LeetCode: Implement strStr() [027]
[题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...
- leetcode 实现strStr()
实现strStr()函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返回 ...
- LeetCode Implement strStr() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
随机推荐
- 单元测试系列之三:JUnit单元测试规范
更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6762032.html Junit测试代 ...
- 如何在基于Bytom开发过程中使用Bigchaindb
上期我们讲了在基于比原开发过程中链外存储可以用分布式存储IPFS,这期我们还给大家介绍另外一种链外存储的解决方案.bigchaindb:https://www.bigchaindb.com,下面我们讲 ...
- [转载]C++之路起航——标准模板库(deque)
转自:https://www.cnblogs.com/grhyxzc/p/5074061.html deque(双端队列):http://baike.baidu.com/link?url=JTvA2c ...
- ABAP search help (搜索帮助) 几种种方法
ABAP search help (搜索帮助) 几种种方法 域范围 ABAP 的搜索帮助有很多种方法,掌握下面的几种基本差不多了 *&------------------------- ...
- Anaconda部署python环境
Anaconda安装 首先进入到anaconda的官网,如下图所示,会看到anaconda的下载页面: 2.下拉或者单击图中的Windows选项,得到如下图所示的界面,此时可以根据自己需要的版本进行相 ...
- submit form to convert to a Java Bean model.
实体类中无需构造函数. Since we haven’t specified a constructor, Java will provide a default constructor that w ...
- mpvue构建小程序(步骤+地址)
mpvue 是一个使用 Vue.js 开发小程序的前端框架(美团的开源项目).框架基于 Vue.js 核心,mpvue 修改了 Vue.js 的 runtime 和 compiler 实现,使其可以运 ...
- Maven项目中使用本地JAR包
<dependency> <groupId>com.TEST</groupId> <artifactId>hm-test</artifactId& ...
- mysql查看配置生效
mysql> show status like 'Threads%';+-------------------+-------+| Variable_name | Value |+--- ...
- Vue 结合 Axios 接口超时统一处理
引语:当网路慢的时候.又或者公司服务器不在内地的时候,接口数据请求不回来超时报错的情况相信大家肯定遇到过的,这里我把我公司项目请求超时的处理方法分享下,希望看过后有帮助. axios基本用法就不多说了 ...