实现strStr
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的更多相关文章
- [PHP源码阅读]strpos、strstr和stripos、stristr函数
我在github有对PHP源码更详细的注解.感兴趣的可以围观一下,给个star.PHP5.4源码注解.可以通过commit记录查看已添加的注解. strpos mixed strpos ( strin ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- strstr 函数的实现
strstr函数:返回主串中子字符串的位置后的所有字符. #include <stdio.h> const char *my_strstr(const char *str, const c ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LintCode StrStr
1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...
- strstr函数
原型:char * strstr( char *haystack, char *needle ) 用法:#include <string.h> 功能:在haystack中寻找needle ...
- strstr函数的用法
C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型: extern char *strstr(char *str1, const char *str2); 语法: ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- C语言(函数)学习之strstr strcasestr
C语言(函数)学习之[strstr]&[strcasestr]一.strstr函数使用[1]函数原型char*strstr(constchar*haystack,constchar*needl ...
随机推荐
- 8、Spring Cloud Zuul
1.Zuul简介 Zuul包含了对请求的路由和过滤两个最主要的功能. 路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础. 过滤器功能则负责对请求的处理过程进行干预,是实现请 ...
- 云小课 | 需求任务还未分解,该咋整!项目管理Scrum项目工作分解的心酸谁能知?
温馨提醒:本文约3000字,需要阅读5分钟,共分为8个部分,建议分段阅读! 软件开发过程中,从产品概念形成到产品规划.往往要做详细的需求分析和项目规划等,因此,选对一款项目管理工具对开发者就显得尤为重 ...
- Restful规则及JPA导致内存溢出
HTTP动词 对于资源的具体操作类型,由HTTP动词表示. 常用的HTTP动词有下面五个(括号里是对应的SQL命令). GET(SELECT):从服务器取出资源(一项或多项). POST(CREATE ...
- 初始Node
node是什么? 一句话: 服务器 什么是服务器: 一句话: 客户端访问 并且能够响应 为什么: 一句话: 执行效率高 #安装 #控制台 切换磁盘: e: 改变目录: cd 目录 cd.. ...
- 算法(Java实现)—— KMP算法
KMP算法 应用场景 字符串匹配问题 有一个字符串str1 = " hello hello llo hhello lloh helo" 一个子串str2 = "hello ...
- Redis史上最全文章教程
Redis 2020 史上最详细Redis教程 本篇文章并不讲解Redis,只是收集 Redis的优质文章教程 ,文章包含三部分: 理论.编程实战 .面试题. 需要有一定编程功底的人学习 ,如果基础不 ...
- python按位操作以及进制转换
a = raw_input() b = raw_input() c1 = int(str(a), 2)#2进制转化为10进制 c2 = int(str(b), 2) c = c1 ^ c2#按位异或 ...
- C#中的深度学习(四):使用Keras.NET识别硬币
在本文中,我们将研究一个卷积神经网络来解决硬币识别问题,并且我们将在Keras.NET中实现一个卷积神经网络. 在这里,我们将介绍卷积神经网络(CNN),并提出一个CNN的架构,我们将训练它来识别硬币 ...
- php + redis 实现关注功能
产品价值 1: 关注功能 2: 功能分析之"关注"功能 3: 平平无奇的「关注」功能,背后有4点重大价值 应用场景 在做PC或者APP端时,掺杂点社交概念就有关注和粉丝功能; 数据 ...
- java 合并两个list 并去重
//两个list合并并去除重复 public static void main(String[] args) throws Exception { List list1 =new ArrayList( ...