[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 ...
随机推荐
- HDU 5656 CA Loves GCD 01背包+gcd
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5656 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- 运维工程师如果将web服务http专变为https
1:生成私钥 2:生成证书签署请求 3:在提供CA签署的web网站上,提交生成的证书签署请求 4:下载已经签署的CA证书 5:将证书的信息保留在web服务器中,且应用到提供web服务的 ...
- Vue数据绑定和响应式原理
Vue数据绑定和响应式原理 当实例化一个Vue构造函数,会执行 Vue 的 init 方法,在 init 方法中主要执行三部分内容,一是初始化环境变量,而是处理 Vue 组件数据,三是解析挂载组件.以 ...
- c#中,字符串前加@是什么意思
让转移字符"\"保持原意,不要转义,如一个地址字符串string path="c:\abc\";默认的"\"是作为转义来使用的,而不是一个真 ...
- delphi完美经典--第十八章
第18章数据感知组件 一.TDBText组件 用来以只读.一次一条记录的方式,显示DataSet中的某一字段值.因同样继承自TCustomLabel,TDBText组件除了数据感知功能外,与标准组件T ...
- JMeter脚本增强之参数化
JMeter测试脚本录制或者编写,在Web应用和App上的操作方式可能有一点点区别(其实也差不多,哈哈),但是当脚本录制好了之后,对测试脚本的强化,包括参数化.关联.文本检查.集合点设置,甚至再往后的 ...
- 'phantomjs.exe' executable needs to be in PATH. (selenium PhantomJS python)
今天selenium PhantomJS python用了下,发现报错,提示我:'phantomjs.exe' executable needs to be in PATH. from seleniu ...
- 【数据库】mysql中复制表结构的方法小结
mysql中用命令行复制表结构的方法主要有一下几种: 1.只复制表结构到新表 ? 1 CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2 或者 ? 1 CREATE ...
- ubuntu adduser
ubuntu adduser 添加用户 hexan 到目录 /home/hexan 下.$ sudo adduser hexan -home /home/hexan 添加一个普通用户adduser [ ...
- Java 工作2年后需要达到怎么样的技术水平
有人回答说这只能是大企业或者互联网企业的工程师才能拿到.也许是的,小公司或者非互联网企业拿两万的不太可能是码农了,应该是已经转管理后才有可能.还有区域问题,这个不在我的考虑范围内,因为除了北上广深杭, ...