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 16进制查看命令、工具
转自:https://blog.csdn.net/chenglian_999/article/details/4672177 2009年10月14日 21:45:00 chenglian_999 阅读 ...
- Vue:(三)路由
(一)基础介绍 vue-router用来构建SPA <router-link></router-link>或者this.$router.push({path:' '}) < ...
- TypeScript作业
题目: 了解神话故事盘古开天辟地或者女娲开世造物,通过typescript程序模拟出天地的变化过程或者万物的衍生过程 参考博客园大神: https://www.cnblogs.com/tansm/p/ ...
- Django 编写模板并渲染的示例
>>> from django.template import Template, Context >>> raw_template = ""& ...
- postman(二):使用postman发送get or post请求
总结一下如何使用postman发送get或post请求 请求 一.GET请求 通常用于请求服务器发送某个资源,请求的数据会附在URL之后,以?分割URL和传输数据,多个参数用&连接 1.请求方 ...
- MVC实战之排球计分(五)—— Controller的设计与实现
控制器 控制器接受用户的输入并调用模型和视图去完成用户的需求.所以当单击Web页面中的超链接和发送HTML表单时, 控制器本身不输出任何东西和做任何处理.它只是接收请求并决定调用哪个模型构件去处理请求 ...
- 【转】在.net Core 中像以前那样的使用HttpContext.Current
1.首先我们要创建一个静态类 public static class MyHttpContext { public static IServiceProvider ServiceProvider; p ...
- Python之时间(time)模块
在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(time.time( ...
- commons-lang3工具类学习(三)
六.ObjectUtils Object工具类 allNotNull(Object... values) 检查所有元素是否为空,返回一个boolean 如果有一个元素为空返回false,所有元素不为空 ...
- 破解 JS(原型)继承
总体分为四大类:利用空对象作为中介继承.Object.create 继承.setPrototypeOf 继承.拷贝继承 function Animal(name, age) { this.name = ...