【Lintcode】013.strStr
题目:
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
.
Do I need to implement KMP Algorithm in a real interview?
- Not necessary. When you meet this problem in a real interview, the interviewer may just want to test your basic implementation ability. But make sure your confirm with the interviewer first.
If source = "source"
and target = "target"
, return -1
.
If source = "abcdabcdefg"
and target = "bcd"
, return 1
.
题解:
Solution 1 ()
class Solution {
public:
int strStr(const char *source, const char *target) {
if (source == NULL || target == NULL) {
return -;
}
int len1 = strlen(source);
int len2 = strlen(target);
for (int i = , j = ; i < len1 - len2 + ; i++) {
for (j = ; j < len2; j++) {
if (source[i + j] != target[j]) {
break;
}
}
if (j == len2) {
return i;
}
}
return -;
}
};
【Lintcode】013.strStr的更多相关文章
- 【lintcode】 二分法总结 I
二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...
- 【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() Implement strStr(). Returns the index of the first occurrence of needle in haysta ...
- 【Leetcode】【Easy】Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 【LeetCode】Implement strStr()(实现strStr())
这道题是LeetCode里的第28道题. 题目描述: 实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle ...
- 【Lintcode】074.First Bad Version
题目: The code base version is an integer start from 1 to n. One day, someone committed a bad version ...
- 【LintCode】转换字符串到整数
问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...
- 【LintCode】判断一个字符串是否包含另一个字符串的所有字符
问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返 ...
- 【LintCode】链表求和
问题分析: 我们通过遍历两个链表拿到每个位的值,两个值加上前一位进位值(0或者1)模10就是该位的值,除以10就是向高位的进位值(0或者1). 由于两个链表可以不一样长,所以要及时判断,一旦为null ...
随机推荐
- TP实例化模型的两种方式 M() D()
TP框架中实例化模型的两种方式 #如果使用自己自定义的函数,那么就用D $mode=D('model'); #如果使用是系统自带的函数,那么就是用M $model=M('model');
- 【文献阅读】Augmenting Supervised Neural Networks with Unsupervised Objectives-ICML-2016
一.Abstract 从近期对unsupervised learning 的研究得到启发,在large-scale setting 上,本文把unsupervised learning 与superv ...
- android菜鸟学习笔记16----Android项目打包安装过程(Run as Android Application)
右击项目名称,Run as Android Appication之后,Android项目打包安装过程: 1.打包生成.apk文件: 1)把源码中的.java文件编译生成.class文件 2)将所有的. ...
- Mac环境,React native错误解决方案
运行react-native run-android,报错如下图: 运行react-native run-ios正常,但 react-native run-android时,提示错误: 在网上 ...
- CUDA:零拷贝主机内存
The easy way to achieve copy/compute overlap!1.Enable Host Mapping* Runtime: cudaSetDeviceFlags() wi ...
- EF异常捕捉
try{// 写数据库}catch (DbEntityValidationException dbEx){ }
- <ReversingEngineering>关于windows32位系统下的dll注入技术经验汇
上个学期把自己闷在图书馆一直在看关于逆向工程技术方面的书,从入门到初级,现在也敢说自己一条腿已经迈进了这片知识的大门里,因为该博客刚开通先将一些经验记录下来,也是留给自己一方面做个参照. <逆向 ...
- Sql 工资第二高(考虑并列)
--题目:Employee表中有ID,Name,Salary三列,求薪资排序第二高的Employee的Name select * FROM [Employee] --等于2时为空,因为有并列第一 SE ...
- 织梦CMS博客风格模板
织梦CMS博客风格模板,织梦CMS,博客模板,CMS模板.程序模板. 模板地址:http://www.huiyi8.com/sc/7248.html
- iOS 如何查看app提交审核是否使用广告标识符(IDFA)
相信很多人被苹果拒绝过 : 您的 App 正在使用广告标识符 (IDFA).您必须先提供关于 IDFA 的使用信息或将其从 App 中移除,然后再上传您的二进制文件. 那么如何查看app里哪里用到ID ...