【LeetCode】28. Implement strStr()
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()相关推荐
- LeetCode - Easy - 28. Implement strStr()
Topic Two Pointers, String Description https://leetcode.com/problems/implement-strstr/ Implement str ...
- 【To Do】LeetCode 28. Implement strStr() 和KMP算法
LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...
- 【LeetCode】剑指 Offer 28. 对称的二叉树
[LeetCode]剑指 Offer 28. 对称的二叉树 文章目录 [LeetCode]剑指 Offer 28. 对称的二叉树 一.递归 一.递归 对称二叉树定义:对于书中任意两个对称结点 L 和 ...
- LeetCode - 28. Implement strStr()
28. Implement strStr() Problem's Link -------------------------------------------------------------- ...
- leetCode 28. Implement strStr() 字符串
28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in ha ...
- 【Leetcode】62. 不同路径
题目 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为&qu ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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: ...
- 【LeetCode】正则表达式匹配
https://www.imooc.com/article/281353?block_id=tuijian_wz [LeetCode]正则表达式匹配 2019.03.04 19:53 598浏览 题目 ...
最新文章
- Wapiti一款小巧的开源安全测试漏洞检测工具
- 使用JMS实现请求/应答程序
- Algorithms_算法思想_递归分治
- alert()的功能_功能强大的Flutter 视频播放插件
- python基础07_tuple_dict
- 搜索引擎排序DEMO
- Postman的使用说明
- 车牌识别:HyperLPR车牌识别代码解析
- 自动化的人肉搜索引擎即将出现?
- MATLAB(1)---将mat文件转换为csv文件
- Power BI 中的货币换算
- 安德斯.埃里克森 的研究
- strtus1 html5,struts1.x的入门级学习教程
- 【POJ3349】snowflakes
- 货郎问题java_动态规划----货郎担问题
- 新品国产C2000,独立双核32位CPU,主频高达400MHz,QX320F280049
- 汇编中的test和cmp指令[Z]
- 知识图谱关键技术总览
- 上海喔趣科技有限公司面经 Java实习生(记录第一次面试)已oc
- cassandra 学习笔记