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 ...
随机推荐
- ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails ()
centos7.5 删除表空间文件失败 问题: mysql> alter table country discard tablespace; ERROR 1451 (23000): Cannot ...
- SVN更新的时候前面的子母的意思(A C D M G U R I)
U:update 表示从服务器收到文件更新了 G:表示本地文件以及服务器文件都已更新,而且成功的合并了 其他的如下: A:add 表示有文件或者目录添加到工作目录 R:replace,从服务器替换,表 ...
- nginx访问不到
nginx访问不到 今天,一朋友的一台linux服务器上部署了nginx,但是外部(公网)就是不能访问,于是协助其排查.整体思路如下: 1.确认nginx配置是否ok. 2.确认网络是否可达. 3.是 ...
- SpringCloud学习2-Springboot监控模块(actuator)
前言 学习一项新技术最大的困难是什么? 是资料.让人高兴的是找到了一本系统学习Spring Cloud的教程,<Spring Cloud微服务实战>, 接下来的学习目标将以此书顺序演进. ...
- Docker——入门实战
I. Docker简介Docker是一种新兴的虚拟化技术,能够一定程度上的代替传统虚拟机.不过,Docker 跟传统的虚拟化方式相比具有众多的优势.我也将Docker类比于Python虚拟环境,可以有 ...
- 大数据新手之路一:安装JDK
Ubuntu16.04 1.下载jdk-8u192-linux-x64.tar.gz https://www.oracle.com/technetwork/java/javase/downloads/ ...
- 联盟链初识以及Fabric环境搭建流程
这篇文章首先简单介绍了联盟链是什么,再详细的介绍了Fabric环境搭建的整个流程. 区块链分类: 以参与方式分类,区块链可以分为:公有链.联盟链和私有链. 定义: 我们知道区块链就是一个分布式的,去中 ...
- grpc(二)记一次grpc debug--io.grpc.StatusRuntimeException: UNKNOWN
1.起初是dingding一直报错: instance:服务器名 err:GrpcClient#placeOrder: io.grpc.StatusRuntimeException: UNKNOWN ...
- 移动端滑动轮播,原生JS
因为公司需要自定义两个轮播图联动,又不想引入第三方库,所以自己研究了下. 下面只是一个简单的轮播图,由此再拓展一下即可实现两个轮播图联动. <!DOCTYPE html> <html ...
- 946. Validate Stack Sequences验证栈序列
网址:https://leetcode.com/problems/validate-stack-sequences/ 参考:https://leetcode.com/problems/validate ...