题目:

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. Linux--FTP和MAIL服务器

     1) FTP协议 FTP(FileTransfer Protocol,文件传输协议)用于管理计算机之间的文件传送.FTP 是Internet 上使用非常广泛的一种通讯协议,它是由支持Intern ...

  2. 如何禁止App在后台运行以及如何保存和恢复App的状态

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果禁止App在后台运行 iOS上的App类似于Windows ...

  3. gradle测试出现IllegalArgumentException

    今天clone了一份代码,跑gradle test时出现failed,从report上来看是这个错误:IllegalArgumentException,具体如下: java.lang.IllegalA ...

  4. java 判断是否是周末

    package untitled7; import java.util.Date; import java.text.SimpleDateFormat; import java.util.Calend ...

  5. C#attribute-----------初级

    前言: attribute是 .net FrameWork 提出的技术,可以为自己的代码添加注解,从而实现些特殊功能. 一. attribute功能 attribute被译作特性,既然是特性,必然功能 ...

  6. Java多种方式读文件,追加文件内容,等对文件的各种操作

    一.多种方式读文件内容. 1.按字节读取文件内容 2.按字符读取文件内容 3.按行读取文件内容 4.随机读取文件内容 import java.io.BufferedReader; import jav ...

  7. 学习pthreads,多线程的创建和终止

    在多CPU多线程的编程中,通过作者的学习发现,pthreads的运用越来越广泛,它是线程的POSIX标准,定义了创建和操作线程的一整套API.环境的配置见上一篇博文,配置好环境后只需要添加#inclu ...

  8. Android开发艺术探索——新的征程,程序人生路漫漫!

    Android开发艺术探索--新的征程,程序人生路漫漫! 偶尔写点东西分享,但是我还是比较喜欢写笔记,看书,群英传看完了,是学到了点东西,开始看这本更加深入Android的书籍了,不知道适不适合自己, ...

  9. [WinForm]动态显示本地目录图片与悬浮窗

    加载显示: if (File.Exists(@"D:\产品图片\" + item + ".jpg")) { //需要判断是否存在图片 Image img = I ...

  10. 【翻译】Ext JS 5.0.1 中的新功能

    原文:What's New in Ext JS 5.0.1 今天,我们很高兴的宣布Ext JS 5.0.1发布了!此维护版本基于Sencha社区的反馈做了一些改进.下面让我们来了解一下这些改变. 可访 ...