Implement strStr().

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

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

Constraints:

haystack and needle consist only of lowercase English characters.

实现代码

实现的关键就是使用substring()方法,将整个字符串分成与目标字符串的长度相同的小段,然后使用equals方法比较,如果有相同的,则返回开始的角标

 1 package easy;
2
3 class SolutionStr{
4 public int strStr(String hystack,String needle){
5 int len1 = hystack.length();
6 int len2 = needle.length();
7 if(len2 > len1){
8 return -1;
9 }
10 if(len2 == 0){
11 return 0;
12 }
13 for(int i = 0;i < len1 - len2 + 1; ++i){
14 if(hystack.substring(i,i+len2).equals(needle)){
15 return i;
16 }
17 }
18 return -1;
19 }
20 }
21 public class ImplstrStr {
22 public static void main(String[] args) {
23 SolutionStr solution = new SolutionStr();
24 System.out.println(solution.strStr("leetcode","ee"));
25 }
26 }

实现strStr的更多相关文章

  1. [PHP源码阅读]strpos、strstr和stripos、stristr函数

    我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...

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

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

  3. strstr 函数的实现

    strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...

  4. 28. Implement strStr()

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

  5. Leetcode 详解(Implement strstr)

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

  6. LintCode StrStr

    1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...

  7. strstr函数

    原型:char * strstr( char *haystack,  char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...

  8. strstr函数的用法

    C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型:      extern char *strstr(char *str1, const char *str2); 语法: ...

  9. [leetcode 27]Implement strStr()

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

  10. C语言(函数)学习之strstr strcasestr

    C语言(函数)学习之[strstr]&[strcasestr]一.strstr函数使用[1]函数原型char*strstr(constchar*haystack,constchar*needl ...

随机推荐

  1. oracle修改数据文件目录

    一.停库修改数据文件目录.文件名 1.当前数据文件目录 SQL> select file_name from dba_data_files; FILE_NAME ---------------- ...

  2. Python命令行模块(sys.argv,argparse,click)

    Python作为一门脚本语言,经常作为脚本接受命令行传入参数,Python接受命令行参数大概有三种方式.因为在日常工作场景会经常使用到,这里对这几种方式进行总结. 命令行参数模块 这里命令行参数模块平 ...

  3. Java及Javascript中的浮点运算

    在进行金额计算,及某些精确计算时,会出现意想不到的很多小数的情况. 对Java 采用BigDecimal,如下代码示例 package number; import java.math.BigDeci ...

  4. ​grafana 的主体架构是如何设计的?

    ​grafana 的主体架构是如何设计的? grafana 是非常强大的可视化项目,它最早从 kibana 生成出来,渐渐也已经形成了自己的生态了.研究完 grafana 生态之后,只有一句话:可视化 ...

  5. 5分钟看懂系列:Python 线程池原理及实现

    概述 传统多线程方案会使用"即时创建, 即时销毁"的策略.尽管与创建进程相比,创建线程的时间已经大大的缩短,但是如果提交给线程的任务是执行时间较短,而且执行次数极其频繁,那么服务器 ...

  6. HCIP --- BGP 总结

    AS:自治系统  --逻辑管理域(例如移动.电信.联通),AS号范围:0-65535,其中,1-64511:公有AS,64512-65535:私有AS IGP:内部网关协议,在一个AS之内传递的路由协 ...

  7. 类似818tu.c微信小说分销系统设计之多公众号网页授权自动登录源码分享

    /** 转载请保留原地址以及版权声明,请勿恶意修改 *  作者:杨浩瑞  QQ:1420213383  独立博客:http://www.yxxrui.cn * [后台]http://xiaoshuo. ...

  8. 各公有云1核1G的云主机跑分对比

    本文主要测评华为云.腾讯云.阿里云 1H1G服务器的性能,为保证结果有效性,使用环境如下: 1.1H1G Ubuntu 16.04_x64 2.Unixbench Version 5.1.3,详细信息 ...

  9. git文件锁定不更新和忽略

    git文件的忽略 新建未提交的文件直接添加.gitignore 提交之后的文件已被git追踪 这时需要清除git缓存 忽略文件 git rm --cached ./src/main/resources ...

  10. 将Maven镜像更换为国内阿里云仓库

    1.国内访问maven默认远程中央镜像特别慢 2.用阿里的镜像替代远程中央镜像 3.大部分jar包都可以在阿里镜像中找到,部分jar包在阿里镜像中没有,需要单独配置镜像 换为国内镜像,让你感受飞一般的 ...