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. bzoj 2086 [Poi2010]Blocks 单调栈

    [Poi2010]Blocks Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 788  Solved: 356[Submit][Status][Dis ...

  2. bzoj 1564 [NOI2009]二叉查找树 区间DP

    [NOI2009]二叉查找树 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 906  Solved: 630[Submit][Status][Discu ...

  3. ZooKeeper管理员指南——部署与管理ZooKeeper

    1.部署 本章节主要讲述如何部署ZooKeeper,包括以下三部分的内容: 系统环境 集群模式的配置 单机模式的配置 系统环境和集群模式配置这两节内容大体讲述了如何部署一个能够用于生产环境的ZK集群. ...

  4. html实现圆角矩形

    问题:如何通过div+css以及定位来实现圆角矩形? 解决方法概述: 内容:首先在<body>标签内部里添加一个大层(大层用来固定整体大框架),然后大层内包含四个小层(四个小层里分别放四个 ...

  5. Beagleboneblack的MLO文件干了些啥

    Beagleboneblack在启动linux之前还有三个启动阶段: ROM code  -->  MLO  -->  u-boot --> kernel 先看看ROM code干了 ...

  6. Jenkins有用的插件

    1. Multijob plugin: 多个任务同时运行 2. ssh slave plugin: 用于安装slave? Allows to launch over ssh, using a java ...

  7. Robot Framework 自定义关键字 Ignore error

    以上是关键字的完整写法. 一下是调用该关键字的实例.

  8. 【BZOJ】1610: [Usaco2008 Feb]Line连线游戏

    [算法]计算几何 [题解]计算所有斜率排序去重. 实数判断相等用fabs(...)≤eps. ★斜率题一定要注意斜率不存在的情况!!! 其实我觉得这份代码可以hack的…… #include<c ...

  9. Little Mathematics Knowledge 数学小常识

    The sum of arithmetic sequence The sum of geometric sequence A special formula : n·n! = (n+1)! - n! ...

  10. 转载 JAVA SE 连接ACCESS

    本代码实现连接 本机数据库的方法. 操作步骤: 1.进入控制面板,打开“管理工具→数据源(ODBC)”,弹出“ODBC数据源管理器”,在“用户DSN”选项卡中,单击选中名称为“Visio Databa ...