leetcode 28. Implement strStr()
2016-05-16 14:31
381 查看
原题链接:28. Implement strStr()
【思路】
用 i 表示 haystack 的索引,每次对于 i,用 count 记录匹配到的最大长度,如果 count = needle 的长度,则返回 true,否则 i+1,再次进行匹配,如果都没找到,则返回 false:
cases passed. Runtime: 3
ms Your runtime beats 59.00% of javasubmissions.
【思路】
用 i 表示 haystack 的索引,每次对于 i,用 count 记录匹配到的最大长度,如果 count = needle 的长度,则返回 true,否则 i+1,再次进行匹配,如果都没找到,则返回 false:
public int strStr(String haystack, String needle) { int len1 = haystack.length(), len2 = needle.length(); for (int i = 0; i < len1 - len2 + 1; i++) { int count = 0; while (count < len2 && haystack.charAt(i + count) == needle.charAt(count)) count++; if (count == len2) return i; } return -1; }72 / 72 test
cases passed. Runtime: 3
ms Your runtime beats 59.00% of javasubmissions.
相关文章推荐
- MHA手动切换 原创2 (主参与复制)
- samba安装
- EXCEL导入解析
- 怀揣爱与宽容,让自己内心更强大
- 创业沙拉北京站亲历记
- Lucene 快速入门
- Spring (3.2.4) 常用jar 包解析
- Xmanager连接 Linux 6的配置
- Storyboard学习五(简单注册页面实现)
- 01背包专题
- BestCoder Round #83 1001&&HDU-5680 zxa and set (水)
- springmvc + hibernate + mysql + maven 简单应用
- QT-Demo-Colck-01
- Bootstrap4一次重大更新 几乎涉及每行代码
- HTTP 消息结构
- mac升级到10.11后使用CocoaPod出现-bash: pod: command not found 解决办法
- C++第10/11周项目 继承与派生 【项目4- 教师兼干部类】(复习新版)
- WCF NetTcpBinding.ReaderQuotas.MaxDepth 引用异常故障排除
- 02 Struts2开发环境的搭建和第一个Struts2应用开发
- inline 内联函数的使用