strStr
来源:互联网 发布:网络爬虫能干什么 编辑:程序博客网 时间:2024/05/29 06:37
For a given source string and a target string, you should output the first index(from 0) of target string in source string.
If target does not exist in source, just return -1.
Have you met this question in a real interview? Yes
Example
If source = “source” and target = “target”, return -1.
If source = “abcdabcdefg” and target = “bcd”, return 1.
Challenge
O(n2) is acceptable. Can you implement an O(n) algorithm? (hint: KMP)
class Solution { /** * Returns a index to the first occurrence of target in source, * or -1 if target is not part of source. * @param source string to be scanned. * @param target string containing the sequence of characters to match. */ public int strStr(String source, String target) { if (source == null || target == null) { return -1; } for (int i = 0; i < source.length() - target.length() + 1; i++) { int j = 0; for (j = 0; j < target.length(); j++) { if (source.charAt(i + j) != target.charAt(j)) { break; } } // finished loop, target found if (j == target.length()) { return i; } } return -1; }}
0 0
- strstr
- strstr
- strstr
- strstr()
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strstr
- strStr
- strstr
- 南大软院大神养成计划--js
- Merge Sorted Array
- ASP.NET学习笔记[12] - Gridview里的下拉框筛选效果
- 1015. 德才论 (25)
- NYOJ 198 数数
- strStr
- NYOJ 562 盒子游戏
- android开发总的:activity全屏设置,主题设置,透明度设置及模式设置等基础知识
- webapp开发的小案例
- @property参数
- Fizz Buzz
- python模拟登陆篇——requests & urllib2方式 &有图片验证码情况
- BFS模板
- ios实现简单的键盘回收的方法