Implement strStr().
来源:互联网 发布:网络最好的诈骗文章 编辑:程序博客网 时间:2024/06/07 11:13
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
class Solution { public int strStr(String haystack, String needle) { if (haystack.length() < 0) return -1; if (needle.length() == 0) return 0; int j = 0; for (int i = 0; i < haystack.length() - needle.length() + 1; i++) { if (haystack.charAt(i) == needle.charAt(j)) { int k = i; for (; j < needle.length(); j++) { if (haystack.charAt(k) == needle.charAt(j)) { k++; } else { j = 0; break; } } if (j == needle.length()) { return i; } } } return -1; }}
哈哈,刚开始思想被阻塞或者说不能熟悉使用JAVA String类自带的indexOf()方法时采用参考网上最笨的办法写的这种方法。下面是一行代码就被leetcode accpted的代码,,
class Solution { public int strStr(String haystack, String needle) { return haystack.indexOf(needle); }}
被通过时,想哭死。。
阅读全文
0 0
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Implement strStr()
- Android中使用Lambda
- 2017.10.3离线赛总结
- LruCache 源码解析
- 高级程序员之面试宝典
- C语言中 \r, \n, \b 的区别
- Implement strStr().
- C++类简单学习(六)
- oracle11g重建控制文件-关闭状态控制文件丢失--markdown格式
- qt调试时监视数组变量
- 关于dpkg一些小问题
- Bootstrap 栅格系统 学习总结 Bootstrap框架是如今最流行的前端框架之一,Bootstrap功能强大,简单易学,很符合实际应用场景。 只是Bootstrap的内容较多,新手
- 类关键字以及多态
- SVN服务端在Linux环境下的安装和配置(多仓库)
- CF 495C. Treasure 模拟(括号配对)