[LeetCode] 28. Implement strStr() ☆
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解法:
这道题让我们在一个字符串中找另一个字符串第一次出现的位置,那我们首先要做一些判断,如果子字符串为空,则返回0,如果子字符串长度大于母字符串长度,则返回-1。然后我们开始遍历母字符串,我们并不需要遍历整个母字符串,而是遍历到剩下的长度和子字符串相等的位置即可,这样可以提高运算效率。然后对于每一个字符,我们都遍历一遍子字符串,一个一个字符的对应比较,如果对应位置有不等的,则跳出循环,如果一直都没有跳出循环,则说明子字符串出现了,则返回起始位置即可,代码如下:
public class Solution {
public int strStr(String haystack, String needle) {
if (haystack == null || needle == null) {
return -1;
}
if (needle.length() == 0) {
return 0;
}
int m = haystack.length(), n = needle.length();
if (m < n) {
return -1;
}
for (int i = 0; i <= m - n; i++) {
boolean found = true;
for (int j = 0; j < n; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
found = false;
break;
}
}
if (found) {
return i;
}
}
return -1;
}
}
[LeetCode] 28. Implement 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()实现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()
题目: class Solution { public: int strStr(string haystack, string needle) { if(needle.empty()){ return ...
随机推荐
- Yogurt factory
Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the ...
- 漫漫征途,java开发(未完待续)
前言 2018年,大二上,有幸加入服务外包实验室的考核,在考核中,主动加入xxx项目的后端,一是为了积累项目经验,二是为了学到更多东西,进入了之后发现原来要学的这么多,时间这么紧!但唯有学习! 心得体 ...
- linux桌面使用鼠标中间健粘帖
使用linux桌面很久了,一直习惯鼠标左键选中,右健弹出菜单复制粘帖. 没想到linux使用鼠标中间健粘帖,很方便. 参考:Linux鼠标中键复制粘贴之谜[Felix蛋疼科普贴] 用鼠标左键单击待复制 ...
- 复利计算C语言转java的相关代码
static void principal()// 计算本金 { int N, m; double i, F, P; System.out.printf("复利终值:"); F = ...
- 2nd 简单四则运算更新
简单四则运算更新 功能:由随机数决定出题为10个以内的数字,并确定是否出现括号(仅限一对),顺序输出表达式,并用栈的方式进行计算,判断正误.其他功能有待进一步实现. 头文件 #include < ...
- 【leetcode】198.HouseRobber
198.HouseRobber You are a professional robber planning to rob houses along a street. Each house has ...
- jenkin重新注册用户
http://www.cnblogs.com/xiao-fy/
- PHP之implode()方法
implode — 将一个一维数组的值转化为字符串 string implode ( string $glue , array $pieces ) string implode ( array $pi ...
- mysql(四)log
慢查询: https://blog.csdn.net/leshami/article/details/39829605 日志组成: https://blog.csdn.net/leshami/arti ...
- hbase快速入门
hbase 是什么? Apache HBase is an open-source, distributed, versioned, non-relational database modeled a ...