LeetCode-28 实现strStr()
文章目录
- 题目描述
- 我的解法
- 反思
- 优化
- 其他思路
- 总结
- Github
题目描述
给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。如果不存在,则返回 -1。
示例 1:
输入: haystack = “hello”, needle = “ll”
输出: 2
示例 2:
输入: haystack = “aaaaa”, needle = “bba”
输出: -1
说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与C语言的 strstr() 以及 Java的 indexOf() 定义相符。
我的解法
public int strStr(String haystack, String needle) {//特殊情况if(null == needle || "".equals(needle))return 0;if(null == haystack || "".equals(haystack)||!haystack.contains(needle))return -1;//接下来肯定是haystack包含needle的情况char needleFirstChar = needle.charAt(0);//获取needle的第一位字符int returnInt = haystack.indexOf(needleFirstChar);//找到needle第一位字符在haystack中第一次出现的位置,初始化String partHaystack = haystack.substring(returnInt,returnInt+needle.length());//partHaystack是从returnInt开始截取needle长度的字符串while(!needle.equals(partHaystack)){//先判断partHaystack是否跟指定字符串相等returnInt++;returnInt += haystack.substring(returnInt).indexOf(needleFirstChar);//取从returnInt之后以为到末尾组成的新字符串中,第一次出现needleFirstChar的位置partHaystack = haystack.substring(returnInt,returnInt+needle.length());//从该位置截取needle长度的字符串用于作比较}return returnInt;}
用间:5ms
战胜:90.31%
反思
其实String类的contains方法就提供了一个参数为String的类似的方法。不过参数不能为空,否则会报空指针异常“NullPointerException”
/*** Returns the index within this string of the first occurrence of the* specified substring.** <p>The returned index is the smallest value <i>k</i> for which:* <blockquote><pre>* this.startsWith(str, <i>k</i>)* </pre></blockquote>* If no such value of <i>k</i> exists, then {@code -1} is returned.** @param str the substring to search for.* @return the index of the first occurrence of the specified substring,* or {@code -1} if there is no such occurrence.*/public int indexOf(String str) {return indexOf(str, 0);}/*** Returns the index within this string of the first occurrence of the* specified substring, starting at the specified index.** <p>The returned index is the smallest value <i>k</i> for which:* <blockquote><pre>* <i>k</i> >= fromIndex {@code &&} this.startsWith(str, <i>k</i>)* </pre></blockquote>* If no such value of <i>k</i> exists, then {@code -1} is returned.** @param str the substring to search for.* @param fromIndex the index from which to start the search.* @return the index of the first occurrence of the specified substring,* starting at the specified index,* or {@code -1} if there is no such occurrence.*/public int indexOf(String str, int fromIndex) {return indexOf(value, 0, value.length,str.value, 0, str.value.length, fromIndex);}
/*** Code shared by String and StringBuffer to do searches. The* source is the character array being searched, and the target* is the string being searched for.** @param source the characters being searched.* @param sourceOffset offset of the source string.* @param sourceCount count of the source string.* @param target the characters being searched for.* @param targetOffset offset of the target string.* @param targetCount count of the target string.* @param fromIndex the index to begin searching from.*/static int indexOf(char[] source, int sourceOffset, int sourceCount,char[] target, int targetOffset, int targetCount,int fromIndex) {if (fromIndex >= sourceCount) {return (targetCount == 0 ? sourceCount : -1);}if (fromIndex < 0) {fromIndex = 0;}if (targetCount == 0) {return fromIndex;}char first = target[targetOffset];int max = sourceOffset + (sourceCount - targetCount);for (int i = sourceOffset + fromIndex; i <= max; i++) {/* Look for first character. */if (source[i] != first) {while (++i <= max && source[i] != first);}/* Found first character, now look at the rest of v2 */if (i <= max) {int j = i + 1;int end = j + targetCount - 1;for (int k = targetOffset + 1; j < end && source[j]== target[k]; j++, k++);if (j == end) {/* Found whole string. */return i - sourceOffset;}}}return -1;}
优化
public int strStr1(String haystack, String needle) {//特殊情况if(null == needle || "".equals(needle))return 0;elsereturn haystack.indexOf(needle);}
用时:6ms(可能电脑及网络性能影响)
战胜:80.45%
其他思路
我最先想到可以先找到needle首个字符在haystack的位置,在用for循环遍历haystack这个字符串needle的长度位的字符,让他们一一比较,如果不匹配则再找到下一个与needle首个字符一致的字符位置,接着遍历。这样可以省去重复建造字符串所导致的系统开销,从而稍稍能缩短运行时间。但其实String类原生的实现方法跟这种方法差不多,具体可以参考一下人家写的代码。
总结
- while循环是先判断后循环 ,而do–while循环是先循环后判断
- 关于substring的双参方法substring(a, b),指的是截取字符串 [a,b)左闭右开 位字符
The substring begins at the specified {@code beginIndex} and
extends to the character at index {@code endIndex - 1}.
Thus the length of the substring is {@code endIndex-beginIndex} - 还需多看jdk源码
Github
LeetCode刷题笔记
LeetCode-28 实现strStr()相关推荐
- 【To Do】LeetCode 28. Implement strStr() 和KMP算法
LeetCode 28. Implement strStr() Solution1:我的答案 有投机取巧之嫌啊~ 注意string中的查找函数在查找时 参考网址:https://www.cnblogs ...
- 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 ...
- strstr函数_[LeetCode] 28. 实现strStr()
题目链接: https://leetcode-cn.com/problems/implement-strstr/ 题目描述 实现 strStr() 函数. 给定一个 haystack 字符串和一个 n ...
- LeetCode 28. 实现strStr()
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- [leetcode] 28. Implement strStr() 解题报告
题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...
- leetcode 28.实现strStr()
题目 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在 ...
- leetcode 28. Implement strStr() 实现strStr()
C++代码,题目相对不是很难 1 class Solution { 2 public: 3 int strStr(string haystack, string needle) { 4 if(need ...
- LeetCode 28 实现 strStr()
https://leetcode-cn.com/problems/implement-strstr/ 解决方案 class Solution {public int strStr(String hay ...
- Leetcode 28. 实现 strStr()
原题链接 解:KMP算法 class Solution { public:int strStr(string s, string p) {if (p.empty()) return 0;int n = ...
最新文章
- R语言统计代码运行耗时实战:计算代码运行时间、使用proc.time函数计算代码运行时间
- Sqli-labs less 25a
- 《计算机科学导论》一2.3 非位置化数字系统
- [置顶] 深入浅出Javascript(三)创建自定义对象以及属性、方法
- 【数据结构(C语言版)系列一】 线性表
- jsp使用cookie实现记住用户名和密码
- sql 如何查询上次的记录_学会SQL并不难,小白学习记录之五(多表查询)
- 王思聪又双被限制消费了!
- JavaScript、PHP、Golang、Haskell、Elixir,哪个才是最佳编程语言?
- python 是否可以一键修图_ps如何快速批量修图?
- 何时适合进行自动化测试?(上)
- php的curl封装类
- STC学习:光敏计数
- python基础编程第三版 pdf_python基础教程第三版 中文 高清 PDF
- 前端的CSP CSP如何落地,了解一下
- 电脑文件剪切拷贝后丢失如何找回?分享文件丢失恢复办法!
- android通过c调用shmat函数,cmake - 尝试在Chipmunk上运行cmake发生错误,如何修复cmake文件? - 堆栈内存溢出...
- 公开课发布:《SQL开发中容易被忽略的BUG》by郑老师
- 海边旅行必备物品清单
- Java研发团队高效能规范