【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 ...
随机推荐
- Html简单的整页切换
恩,语言组织不是很好,直接上代码吧.... <!DOCTYPE html> <html> <head lang="en"> <meta c ...
- cmake学习之- set
最后更新: 2019-06-06 一.简单介绍 set 和 unset 为一对相反的指令,分别为设置变量以及取消设置变量. 什么是变量? 变量就是用于保存值的存储单元. set 有哪几种? set 有 ...
- 小白学习python之路(二):安装开发工具
引言 上一章我们安装配置了python3.7,这一章我们安装python的开发工具,我用的pycharm2019 安装 工具连接:https://u20538204.ctfile.com/fs/205 ...
- CentOS6下基于Nginx搭建mp4/flv流媒体服务器
CentOS6下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具) 1.先添加几个RPM下载源 1.1)安装RPMforge的CentOS6源 [roo ...
- 【BZOJ4212】神牛的养成计划 Trie树+可持久化Trie树
[BZOJ4212]神牛的养成计划 Description Hzwer成功培育出神牛细胞,可最终培育出的生物体却让他大失所望...... 后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变 ...
- 九度OJ 1080:进制转换 (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4583 解决:1076 题目描述: 将M进制的数X转换为N进制的数输出. 输入: 输入的第一行包括两个整数:M和N(2<=M,N< ...
- PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析(转)
sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort() 函数用于对数组单元从低到高进行排序并保持索引关系. arsort() 函数用于 ...
- 关于SAP S4 HANA 的13个问题
SAP S/4HANA的路线图是怎样的?价格是多少?下一步还将添加哪些新模块?莫不闻专业SAP问答平台结合SAP HANA及SAP HANA应用商务套件开发全球负责人Uwe Grigoleit帮大家整 ...
- SpringBoot学习笔记(1):配置Mybatis
SpringBoot学习笔记(1):配置Mybatis 反思:如果自己写的笔记自己都看不懂,那就不要拿出来丢人现眼! IDEA插件 Free MyBatis Plugin插件可以让我们的MyBatis ...
- 6 《锋利的jQuery》Ajax的应用(略。)
Ajax的优势 1.不需要插件支持 2.优秀的用户体验 3.提高web程序的性能(传输数据的方式,按需发送) 4.减轻服务器和带宽的负担 Ajax的不足 1.浏览器对XMLHttpRequest对象支 ...