[leetcode]28. Implement strStr()实现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.
题意:
实现strStr() : Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
Solution1:Two Pointers, finding substring[i...j] in str1,such that it equals str2

code:
/*
Time: O(n^2).
Space: O(1).
*/ class Solution {
public int strStr(String s1, String s2) {
//题意确认 return 0 when needle is an empty string
if(s2.length() == 0) return 0; //for(int i = 0; i < s1.length(); i++){ 确保s1中含有s2,则扫s1的指针的范围可以缩小到s1.length() - s2.length() + 1
for(int i = 0; i < s1.length() - s2.length() + 1; i++){
int j = i;
int k = 0;
while( j < s1.length() && k < s2.length() && s1.charAt(j) == s2.charAt(k)){
j++;
k++;
}
if( k == s2.length()){
return i;
}
}
return -1;
}
}
[leetcode]28. Implement strStr()实现strStr()的更多相关文章
- 44. leetcode 28. Implement strStr()
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- [LeetCode] 28. Implement strStr() 实现strStr()函数
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- Java [leetcode 28]Implement strStr()
题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- [LeetCode] 28. Implement strStr() 解题思路
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 28——Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- [LeetCode] 28. Implement strStr() ☆
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode——28. Implement strStr()
题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...
随机推荐
- java 实现自定义事件
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; i ...
- 1.2 VMware安装过程
1.新建虚拟机后,选择[我以后再安装],否则安装过程看不到. 2.在虚拟机设置里,内存设置不能超过物理内存的一半. 3.创建硬盘 4.设置CD/ROM使用ISO镜像文件 5.创建[快照] 6.克隆虚拟 ...
- 对JVM的简单了解
- Flask--SQLAlchemy--基本查询备忘
SQLAlchemy查询过滤器: 查询所有用户数据 User.query.all() 查询有多少个用户 User.query.count() 查询第1个用户 User.query.first() 查询 ...
- mysqlbinlog基于时间点恢复
基于时间点恢复 /data/mysq/mysqlbin.000026 #mysqlbinlog文件,恢复如下内容: 注意:按照时间点恢复时,可能同一个时间点有其他的操作,要结合上下文的时间选取~ # ...
- shell脚本大小写转换
几个方法 1.tr命令 2.sed替换 3.awk的tolower() toupper() 4.perl语言 详见 http://blog.51cto.com/wangxiaoyu/197623 L ...
- 格式化输出=========》format 和 %
str.format() 实现格式化输出的功能 s1 = "i am {0},gae{1}".format("alex",18) 普通版,直接输入元祖 ...
- WordPress版微信小程序2.4版发布
自从发布2017年9月16日WordPress版微信小程序2.2.8版本后,这个一个多月来,WordPress版微信小程序,在经过一些比较小的更新后,今天发布阶段性的版本:2.4版 .这版本主要是功能 ...
- 游戏中转盘概率的算法---python实现
加入转盘的内容及概率如下 转盘倍数 0.5 0.6 0.7 0.8 1 1.2 1.5 1.8 2 机率 0.2 0.15 0.15 0.2 0.2 0.1 0.1 0.05 0.05 下面来实现转盘 ...
- 关于163发邮件报错535 Error:authentication failed解决方法
关于发邮件报错535 Error:authentication failed解决方法 调用163邮箱服务器来发送邮件,我们需要开启POP3/SMTP服务,这时163邮件会让我们设置客户端授权码,这个授 ...