Difficulty:easy

 More:【目录】LeetCode Java实现

Description

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.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

Intuition

Use two pointers

Here is a brute-force solution, I will implement KMP algorithm in the future.

Solution

Brute force solution:

    public int strStr(String haystack, String needle) {if(haystack==null || needle==null)return -1;for(int i=0;;i++){for(int j=0;;j++){if(j==needle.length())  return i;if(i+j==haystack.length())  return -1;if(haystack.charAt(i+j)!=needle.charAt(j))    break;}}}

  

Complexity

Time complexity : 

Assume that n = length of haystack and m = length of needle, then the runtime complexity is O(nm).

Space complexity :  O(1)

What I've learned

1. It is a great question to ask "What should we return when needle is an empty string?"  during an interview.

 More:【目录】LeetCode Java实现

转载于:https://www.cnblogs.com/yongh/p/10018767.html

【LeetCode】28. Implement strStr()相关推荐

  1. LeetCode - Easy - 28. Implement strStr()

    Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...

  2. 【To Do】LeetCode 28. Implement strStr() 和KMP算法

    LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...

  3. 【LeetCode】剑指 Offer 28. 对称的二叉树

    [LeetCode]剑指 Offer 28. 对称的二叉树 文章目录 [LeetCode]剑指 Offer 28. 对称的二叉树 一.递归 一.递归 对称二叉树定义:对于书中任意两个对称结点 L 和 ...

  4. LeetCode - 28. Implement strStr()

    28. Implement strStr() Problem's Link -------------------------------------------------------------- ...

  5. leetCode 28. Implement strStr() 字符串

    28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...

  6. 【Leetcode】62. 不同路径

    题目 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为&qu ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA)

    [LeetCode]309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA) 题目地址: https: ...

  9. 【LeetCode】正则表达式匹配

    https://www.imooc.com/article/281353?block_id=tuijian_wz [LeetCode]正则表达式匹配 2019.03.04 19:53 598浏览 题目 ...

最新文章

  1. Wapiti一款小巧的开源安全测试漏洞检测工具
  2. 使用JMS实现请求/应答程序
  3. Algorithms_算法思想_递归分治
  4. alert()的功能_功能强大的Flutter 视频播放插件
  5. python基础07_tuple_dict
  6. 搜索引擎排序DEMO
  7. Postman的使用说明
  8. 车牌识别:HyperLPR车牌识别代码解析
  9. 自动化的人肉搜索引擎即将出现?
  10. MATLAB(1)---将mat文件转换为csv文件
  11. Power BI 中的货币换算
  12. 安德斯.埃里克森 的研究
  13. strtus1 html5,struts1.x的入门级学习教程
  14. 【POJ3349】snowflakes
  15. 货郎问题java_动态规划----货郎担问题
  16. 新品国产C2000,独立双核32位CPU,主频高达400MHz,QX320F280049
  17. 汇编中的test和cmp指令[Z]
  18. 知识图谱关键技术总览
  19. 上海喔趣科技有限公司面经 Java实习生(记录第一次面试)已oc
  20. cassandra 学习笔记

热门文章

  1. 嵌入式系统中断实验c语言,中断的实验现象
  2. 帝国cms内容页调用php,帝国CMS内容页调用上一篇与下一篇方法汇总
  3. SQL面试题--(26~46)
  4. extern 关键字的作用
  5. VK Cup 2018 Round 1: A. Primal Sport
  6. DFS序--树的问题转化为区间问题
  7. pytorch 中 torch.optim.Adam 方法的使用和参数的解释
  8. dp动态规划_最长上升子序列问题
  9. jenkins简介及docker部署
  10. 安卓自定义相机拍照功能全解(不调用系统相机)