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;
}
};

LintCode 13. Implement strStr()的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. 28. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  3. Leetcode 详解(Implement strstr)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  4. [leetcode 27]Implement strStr()

    1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  5. Leetcode #28. Implement strStr()

    Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...

  6. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  7. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  8. Implement strStr()

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  9. Implement strStr() [LeetCode]

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

随机推荐

  1. [mysql][【优化集合】mysql数据库优化集合

    三个层面: 1.系统层面 2.mysql配置参数 3.sql语句优化 =========================================================== 一.系统层 ...

  2. YARN和MapReduce的内存设置参考

    如何确定Yarn中容器Container,Mapreduce相关参数的内存设置,对于初始集群,由于不知道集群的类型(如cpu密集.内存密集)我们需要根据经验提供给我们一个参考配置值,来作为基础的配置. ...

  3. 【转】 GRASP(通用职责分配软件模式)模式

    转自:http://www.cnblogs.com/sevenyuan/archive/2010/03/05/1678730.html 及:http://blog.csdn.net/lovelion ...

  4. kafka命令总结

    ./kafka-console-consumer.sh  --bootstrap-server IP:9092  --topic user-asset-change-v1 --partition 2  ...

  5. os模块学习(转)

    python 文件操作   一.python中文件管理 文件管理是很多应用程序的基本功能和重要组成部分.Python可以使文件管理极其简单,特别是和其它语言相对比. 1.介绍 几乎以任何语言编写的众多 ...

  6. 【Atcoder】ARC084 Small Multiple

    [题意]求一个k的倍数使其数位和最小,输出数位和,k<=10^5. [算法]最短路 [题解]考虑极端情况数字是可能爆long long的(例如k*num=100...000),所以确定基本方向是 ...

  7. A Simple Math Problem(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 思路:矩阵快速幂模板题,不过因为刚刚入门矩阵快速幂,所以经常把数组f存反,导致本地错误一晚,差点 ...

  8. HTML语意化

    1.什么是HTML语义化? 根据内容的结构化(内容语义化),选择合适的标签(代码语义化)便于开发者阅读.写出更优雅的代码的同时让浏览器的爬虫和机器很好地解析.  2.为什么要语义化? 为了在没有CSS ...

  9. 关于RecylerView:1.在ScrollView的RecylerView滑动事件的处理。2.item之间的距离 小数取整

    1.在ScrollView的RecylerView滑动事件的处理. 在布局文件中在RecylerView外包裹一层相对布局 2.RecylerView item之间的距离 (1)编写SpaceItem ...

  10. V4L2(二)虚拟摄像头驱动vivi深入分析【转】

    转自:http://www.cnblogs.com/tureno/articles/6694463.html 转载于: http://blog.csdn.net/lizuobin2/article/d ...