LintCode 13. Implement strStr()

题目描述

对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。
如果不存在,则返回 -1。

C++ 实现

class Solution {
public:
/*
* @param source: source string to be scanned.
* @param target: target string containing the sequence of characters to match
* @return: a index to the first occurrence of target in source, or -1
if target is not part of source.
*/
int strStr(const char *source, const char *target) {
// write your code here
if(source==NULL||target==NULL) return -1;
int s_len = strlen(source);
int t_len = strlen(target);
int i = 0;
int j = 0;
while(i<s_len&&j<t_len){
if(source[i]==target[j]){
i++;
j++;
}else{
i = i-j+1;
j = 0;
}
}
if(j==t_len)
return i-j;
return -1;
}
};

最新文章

  1. python BeautifulSoup模块的简要介绍
  2. xcode配置绝对路径与相对路径
  3. HeapSort 堆排序 基于伪代码实现
  4. asp.net mvc 多级文件夹
  5. JMeter 响应文本乱码处理
  6. 上不了Google是码农的悲哀
  7. 自定义UIPageControl圆点的图片
  8. Verilog
  9. IOS开发一些资源收集
  10. 解开发者之痛:中国移动MySQL数据库优化最佳实践(转)
  11. c++暂停
  12. c语言关键字总结
  13. Android 使用弹出对话框,报Unable to add window错误
  14. [译]ASP.NET Core 2.0 中间件
  15. 报错:严重: Servlet.service() for servlet [springmvc] in context with path [ ] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
  16. HTML、CSS、JS中常用的东西在IE中兼容问题汇总
  17. 【读书笔记】segment routing mpls数据平面-1
  18. fullScreen.html
  19. [转]TDD之Dummy Stub Fake Mock
  20. High-radix routers

热门文章

  1. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
  2. Android数据过滤器:Filter
  3. 问题BeanFactory not initialized or already closed - call &#39;refresh&#39; before access
  4. Eclipse中安装Tomcat
  5. [USACO13FEB]出租车Taxi
  6. 【设计模式】 模式PK:装饰模式VS适配器模式
  7. elasticsearch 创建索引,以及检索一条数据
  8. 当你启动Redis的时候,Redis做了什么
  9. Vue 使用中的小技巧(山东数漫江湖)
  10. SQL Server Delete Duplicate Rows