题目:

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. Cocos2D中节点Z序的计算规则

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...

  2. 高仿腾讯QQ即时通讯IM项目

    前言:其实这个项目早就开发完成了,在本人的github上,本来没打算写成博客的形式,因为一个项目要写出来要花很久,但是最近看到很多 人在我的github上download后随意发布到网上,本来上传到g ...

  3. 5.3、Android Studio录像

    Android Monitor允许你从设备中录制一段MP4格式的视频,最长允许3分钟. 录制视频 在硬件设备中录制视频: 1. 打开一个项目 2. 在设备中运行应用 3. 显示Android Moni ...

  4. CUDA5.5 的环境变量设置

    为了方便,我写了这个文件用于设置cuda5.5的环境变量. 其中有些环境变量可能用不到,大家根据需要修改就是了. export CUDA_HOME=/usr/local/cuda-5.5 export ...

  5. (NO.00005)iOS实现炸弹人游戏(九):游戏主角(二)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 上篇介绍了游戏主角的初始化方法,下面我们一次来实现主角的其他方 ...

  6. 剑指Offer——算法复杂度中的O(logN)底数是多少

    剑指Offer--算法复杂度中的O(logN)底数是多少 前言 无论是计算机算法概论.还是数据结构书中,关于算法的时间复杂度很多都用包含O(logN)这样的描述,但是却没有明确说logN的底数究竟是多 ...

  7. StringBuffer与StringBuilder详解

    刚刚在参加网易实习生在线考试的时候,出了一道选择题谈到了StringBuilder这个类的一些选项,虽然那道题自己做对了,但是也提醒了我应该好好了解一些StringBuffer与StringBuild ...

  8. 【Linux 操作系统】 Secure CRT 终端配置 -- 配置语法高亮 光标 和 字体

    . 1. Secure CRT 中没有想要的字体 Windows 8 下没有 Courier New 字体, 需要在系统的字体上进行配置, 进入 C:\Windows\Fonts 目录, 下面是目录的 ...

  9. HTML5 input 类型: email及url

    原文地址:HTML5′s "email" and "url" Input Types 原文日期: 2010年09月15日 翻译日期: 2013年08月13日 在 ...

  10. MFC的两个问题

    1.启动WinApp的时候,报 ASSERT(AfxGetThread() == NULL)错误依赖的MFC DLL工程设置里面加上_USRDLL2. MFC误报内存泄露全局对象释放的问题,添加mfc ...