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 ...
随机推荐
- Linux相关代码
Linux ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ---- ...
- UTF-8格式txt文件读取字节前三位问题
今天试着读取一份UTF-8格式的txt文件,内容如下 12345 但是每次读取之后转为String类型,输出字符串长度总是为6,并且第一位打印在控制台后不占任何空间. 经过debug查看字节码后发现, ...
- shiro 分布式缓存用户信息
很多分布式缓存登录用户信息一般都是存在redis类似的缓存里面.其中实现细节或者拆分都是大同小异. 一般用户登录权限管理都用shiro处理. 如果仔细分应该就是一下3种. 1,有一个单独的用户权限管理 ...
- arXiv 提交 pre-print 文章的相关注意事项
arXiv 提交 pre-print 文章的相关注意事项 2018-11-25 22:38:28 1. 有一个可以正常上传 paper 的 arXiv 账号:https://arxiv.org/ 这 ...
- MSSQL 数据库 buildindex 出错
错误1: Executing the query "ALTER INDEX [IX_liveConfigState_Service_ServiceId_..." failed wi ...
- MyBatis mapper parameterType
1. 传入简单类型 JAVA代码: public User get(Long id) { return (User) getSqlSession().selectOne("com.liu ...
- angular-material(一)
1.引入文件(angular-material.css.angular.min.js.angular-animate.js.angular-aria.min.js.angular-material.j ...
- python+selenium,实现带有验证码的自动化登录功能
python+selenium的环境准备,请自行安装完成,这里直接贴代码,方便做项目时直接使用. import time from selenium import webdriver from PIL ...
- 力扣(LeetCode)605. 种花问题
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花 ...
- 利用Fiddler编写Jmeter接口测试
利用Fiddler抓包APP应用接口,在Jmeter编写接口测试脚本 1.用Fiddler对Android用用进行抓包 Fiddler介绍: Fiddler是一款非常流行并且实用的http抓包工具,它 ...