题目:

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

思路:

  • 题意:判断一个字符串是否包含另外一个
  • 写一个函数判断一个字符串从坐标k,是否相等于另一个,遍历一下,i < a.length() -b.length()+1,b。length() < a.length()

    -

代码:

public class Solution {
    public int strStr(String haystack, String needle) {
        if(haystack == null||needle == null||needle.length() > haystack.length()){
            return -1;
        }
        for(int a = 0;a < (haystack.length() - needle.length()+1);a++){
            if(equal(haystack,a,needle)){
                return a;
            }
        }
        return -1;
    }
    public boolean equal(String a,int k,String b){
        for(int m = 0;m < b.length();m++){
            if(b.charAt(m) != a.charAt(k)){
                return false;
            }
            k++;
        }
        return true;
    }
}

leetcode(57)- Implement strStr()的更多相关文章

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

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

  2. [leetcode 27]Implement strStr()

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

  3. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  4. 【leetcode】Implement strStr() (easy)

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

  5. 【leetcode】Implement strStr()

    Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...

  6. Java for LeetCode 028 Implement strStr()

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

  7. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  8. [LeetCode] 28. Implement strStr() 解题思路

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

  9. 44. leetcode 28. Implement strStr()

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

随机推荐

  1. SQL基本函数

    字符型函数 函数名称 描述 LOWER 将特定的字符串转化为小写,只影响字母字符串. UPPER 将整个字符串转换成大写,只影响字母字符串. INITCAP 将字符串中每一个单词的第一个字母转换为大写 ...

  2. 携程React Native实践

    React Native(下文简称 RN)开源已经一年多时间,国内各大互联网公司都在使用,携程也在今年 5 月份投入资源开始引入,并推广给多个业务团队使用,本文将会分享我们遇到的一些问题以及我们的优化 ...

  3. [ExtJS5学习笔记]第七节 Extjs5的组件components及其模板事件方法学习

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38487519 本文作者:sushengmiyan ------------------ ...

  4. 两种配置大数据环境的方法Ambari以及hadoop源代码安装的步骤

    1.Ambari安装 Ambari & HDP(Hortonworks Data Platform) ********************************************* ...

  5. 【Unity Shaders】游戏性和画面特效——创建一个夜视效果的画面特效

    本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...

  6. Mybatis源码分析之结果封装ResultSetHandler和DefaultResultSetHandler

    ResultSetHandler负责处理两件事: (1)处理Statement执行后产生的结果集,生成结果列表 (2)处理存储过程执行后的输出参数ResultSetHandler是一个接口,提供了两个 ...

  7. Java-IO之ByteArrayInputStream

    ByteArrayInputStream是字节数组输入流,继承于InputStream.它包含了一个内部缓冲区,该缓冲区包含从流中读取的字节,其实内部缓冲区就是一个字节数组,而ByteArrayInp ...

  8. JSP标签JSTL(4)--URL

    <c:url>标签作用是将一个URL地址格式化为一个字符串,并且保存在一个变量当中.它具有URL自动重写功能.value指定的URL可以是当前工程的一个URL地址,也可以是其他web工程的 ...

  9. SVM 使用朗格朗日乘子得到权重向量

    紧跟前一篇SVM博文,下面我们用数学推导来导出权重的计算方法.

  10. JUI/DWZ 分页 Servlet

    分页介绍 参考:官方用户手册中的"分页组件"         分页思路服务器返回当前页的数据,总条数,再由js来生成分页标签.分页是配合服务器端来处理的, 不是存js做的分页. 因 ...