LintCode 13. Implement strStr()
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()的更多相关文章
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Leetcode 详解(Implement strstr)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
- Leetcode #28. Implement strStr()
Brute Force算法,时间复杂度 O(mn) def strStr(haystack, needle): m = len(haystack) n = len(needle) if n == 0: ...
- 【leetcode】Implement strStr() (easy)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- [LeetCode] Implement strStr()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Implement strStr() [LeetCode]
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
随机推荐
- POJ 3111 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 10507 Accepted: 2709 Case Time ...
- java nio buffer读取数据乱码问题
public static void main(String[] args) throws IOException { String charsetName = "GBK"; St ...
- rocketmq单机搭建
RocketMQ 是alibaba开源的消息队列. 本文使用的是开源版本v3.18 系统: centos6.x最小化安装 需要用到的软件包: jdk-7u67-linux-x64.tar.gz ali ...
- [洛谷P1941] 飞扬的小鸟
洛谷题目链接:飞扬的小鸟 题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了 ...
- 【设计模式】 模式PK:包装模式群PK
1.概述 我们讲了这么多的设计模式,大家有没有发觉在很多的模式中有些角色是不干活的?它们只是充当黔首作用,你有问题,找我,但我不处理,我让其他人处理.最典型的就是代理模式了,代理角色接收请求然后传递到 ...
- 【洛谷 P4116】 Qtree3 (树链剖分)
题目链接 树剖练手题,想复习下树剖. 第一次提交\(T\)成QQC 看我 ??? 看了数据范围的确挺恶心的,我的复杂度是\(O(Mlog^2N)\)的,数据范围有三段 For 1/3 of the t ...
- centos 挂载数据盘
第一.检查硬盘设备是否有数据盘 fdisk -l 第二.数据硬盘分区 fdisk /dev/vdb 第三.ext3格式化分区 mkfs.ext3 /dev/vdb1 第四.挂载新分区 A - 新建目录 ...
- Less & Sass
CSS不是一种编程语言.它开发网页样式,但是没法用它编程.也就是说,CSS基本上是设计师的工具,它没有变量,也没有条件语句,只是一行行单纯的描述.有人就开始为CSS加入编程元素,这被叫做"C ...
- JS 本地属性与继承属性
判断是否拥有某种属性 1.in 运算符 var obj = {name:'jack'}; alert('name' in obj); // --> true alert('toString' i ...
- js函数定义方法
1.函数声明 其语法为 function functionName(){ //函数体 } 首先是function关键字,然后是函数名,其重要特征是函数声明提升,即在执行代码之前会先读取函数声明,使其在 ...