Leetcode 详解(Implement strstr)
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
注:直接看题目可能会有些偏差,因为容易认为 needle是一个字符,那就也太容易了,实则,haystack和needle都是字符串(注意是字符串不是数组)
解法思路:
暴力搜索:
public class Solution {
public int strStr(String haystack, String needle) {
int i=0,j=0;
while(i<haystack.length()&&j<=(needle.length()-1))
{
if(haystack.charAt(i)==needle.charAt(j))
{
i++;
j++;
}
else
{
i=i-j+1;
j=0;
}
}
if(j==needle.length())
return i-j;
else return -1;
}
}
感受下 clean code
public int strStr(String haystack, String needle) {
for (int i = 0; ; i++) {
for (int j = 0; ; j++) {
if (j == needle.length()) return i;
if (i + j == haystack.length()) return -1;
if (needle.charAt(j) != haystack.charAt(i + j)) break;
}
}
}
Leetcode 详解(Implement strstr)的更多相关文章
- 【LeetCode】28. Implement strStr() (2 solutions)
Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haysta ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- 【一天一道LeetCode】#28. Implement strStr()
一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...
- LeetCode(28)题解:Implement strStr()
https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...
- 【LeetCode】28. Implement strStr() 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 find函数 遍历+切片 日期 题目地址:https ...
- 【LeetCode】28 - Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- LeetCode OJ:Implement strStr()(实现子字符串查找)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】028. Implement strStr()
Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
随机推荐
- react-native 调用 TouchableOpacity (触摸透明) 时报了一个警告
`useNativeDriver` is not supported because the native animated module is missing. Falling back to JS ...
- Git使用指南(1)——Git配置命令
配置用户信息 git config --global user.name bongxin git config --global user.email bongxin@yeah.net 配置文本编辑器 ...
- HDU 1272 小希的迷宫 并查集
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- FFmpeg官方文档之————先进音频编码(AAC)
先进音频编码(AAC)的后继格式到MP3,和以MPEG-4部分3(ISO / IEC 14496-3)被定义.它通常用于MP4容器格式; 对于音乐,通常使用.m4a扩展名.第二最常见的用途是在MKV( ...
- ~/.config/user-dirs.dirs【桌面设置】
# This file is written by xdg-user-dirs-update # If you want to change or add directories, just edit ...
- css3 文字过长用...代替
white-space: nowrap; text-overflow: ellipsis; overflow: hidden; white-space:nowrap 代表多个空格/回车不换行: tex ...
- NPOI的使用Excel模板导出
private string ExportScMeeting(DataTable source) { string templateFile = Server.MapPath(@"Excel ...
- CMD 规范是不是就是 commonJS 规范?
作者:giscafer链接:https://www.zhihu.com/question/20576942/answer/58094030来源:知乎著作权归作者所有,转载请联系作者获得授权. CMD是 ...
- zabbix3.0.4 部署之七 (zabbix3.0.4 邮件报警) & 微信报警
1 [root@sv-zabbix ~]# cat /usr/local/zabbix/share/zabbix/alertscripts/sendEmail.sh #!/bin/bash#SMTP_ ...
- VWmare workstation12.5中出现的问题
安装VWmare Workstation12.5之后,出现只能从主机复制文件到虚拟机,但是虚拟机的文件不能直接拖到主机中进行复制.所以我们需要重新安装VWmare Tools. 在虚拟机的VM-> ...