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的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  3. LeetCode OJ--Implement strStr()

    http://oj.leetcode.com/problems/implement-strstr/ 判断一个串是否为另一个串的子串 比较简单的方法,复杂度为O(m*n),另外还可以用KMP时间复杂度为 ...

  4. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  5. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  6. leetcode implement strStr python

    #kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...

  7. LeetCode: Implement strStr() [027]

    [题目] Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if ...

  8. leetcode 实现strStr()

    实现strStr()函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返回 ...

  9. LeetCode Implement strStr() 实现strstr()

    如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...

随机推荐

  1. PHP钩子的简单介绍

    <?php /** * 钩子类 */ class Hook { static public function execute($type, $model='') { if($model == ' ...

  2. (免费电影)苹果手机合并.ts视频

    代码教程:https://mp.weixin.qq.com/s/6Oo8TOruePUxotC11zp0ag

  3. 微信小程序,加载更多

    html <!-- 头部 --> <view class='tab'> <view class="tab-new {{selected_new?'active' ...

  4. LeetCode--021--合并两个有序链表(java)

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2- ...

  5. Python的日期函数datetime使用

    前记:Python的时间模块,time,datetime功能很丰富,要多源码学习一下,常用的strftime()和strptime()等等要灵活使用 import datetime now_time ...

  6. hdu5421Victor and String 两端加点的pam

    题意:要求维护两端加点的字符串,以及查询本质回文串个数和所有回文串个数 题解:pam,两端加点过程详见ioi2017国家集训队论文,维护一个最长回文前缀和最长回文后缀即可,fail不用两个,能前后共用 ...

  7. Matlab:高阶常微分三种边界条件的特殊解法(隐式Euler)

    函数文件1: function b=F(f,x0,u,h) b(1,1)=x0(1)-h*x0(2)-u(1); b(2,1)=x0(2)+h*x0(1)^2-u(2)-h*f; 函数文件2: fun ...

  8. [spring源码] 小白级别的源码解析(一)

    一直都在用spring,但是每次一遇到spring深入的问题,就是比较懵的状态.最近花了段时间学习了一下spring源码. 1,spring版本介绍 虽然工作中,一直在用到spring,可能有时候,并 ...

  9. 一个简单的CD/CI流程思考,续

    经过各种优化,最终一个非常简单的pipeline出现了,图中没有包含单元测试及静态代码检查的部分,有时间补上.至少实现了提交即构建,也能迅速反馈给开发者. 但是最大的问题是,研发团队还是习惯依赖于部署 ...

  10. 算法-最通俗易懂的KMP算法详解

    有些算法,适合从它产生的动机,如何设计与解决问题这样正向地去介绍.但KMP算法真的不适合这样去学.最好的办法是先搞清楚它所用的数据结构是什么,再搞清楚怎么用,最后为什么的问题就会有恍然大悟的感觉.我试 ...