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 ...
随机推荐
- (4) 二叉平衡树, AVL树
1.为什么要有平衡二叉树? 上一节我们讲了一般的二叉查找树, 其期望深度为O(log2n), 其各操作的时间复杂度O(log2n)同时也是由此决定的.但是在某些情况下(如在插入的序列是有序的时候), ...
- Windows 版本的iTunes 修改iPhone的备份路径
帮朋友解决修改iPhone的备份路径问题,故写篇博客整理记录一下. 所需工具 Junction工具 下载该工具然后将文件放到C:\Windows 目录下,如下图: 找到iTunes的备份路径 Wind ...
- LINUX下查看日志
LINUX的日志都在 /var/log 目录下: 进入此文件查看目录详情: 查看某个日志的命令: 1.cat messages可以查看某个日志文件. 2.要达到实时更新,可以通过tail命令查看 ...
- css-position
值 描述 absolute 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位. 元素的位置通过 "left", "top", " ...
- JSP Filter用法
1.filter的作用 在HttpServletRequest请求到达Servlet之前,拦截客户的HttpServletRequest 根据需要检查HttpServletRequest的相关信息,修 ...
- Swift_1基础
// swift中导入类库使用import,不再使用<>和""import Foundation // 输出print("Hello, World!" ...
- C#自定义大小与改变大下的方法
在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Wind ...
- 微信小程序-视图容器组件
view 视图容器 例如: <view class="section"> <view class="section__title">fl ...
- sublime使用小技巧——自动保存后缀名与自动匹配语法
1,打开sublime 2,新建文件 3,ctrl+shift+p 4,输入ss(set syntax),在下拉列表中选择需要的语言 列表很多,可以输入相关快捷键 如:html-->ssh cs ...
- 流媒体测试笔记记录之————解决问题video.js 播放m3u8格式的文件,根据官方的文档添加videojs-contrib-hls也不行的原因解决了
详细代码Github:https://github.com/Tinywan/PHPSharedLibrary/tree/master/Tpl/Html5/VideoJS 想播放hls协议的就是m3u8 ...