【Lintcode】013.strStr
2024-08-24 20:00:43
题目:
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
.
Clarification
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.
Example
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 -;
}
};
最新文章
- Android Studio doesn&#39;t list my android phone
- AJAX文件上传实践与分析,带HTML5文件上传API。
- Android Studio-目录结构
- NOIp DP 1003 爆零记
- C# 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler”节
- c++字符串互相转换
- 武汉科技大学ACM :1001: 零起点学算法34——继续求多项式
- 动画api说明
- dokcer 运行和进入容器
- P/Invoke与逆向P/Invoke
- spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava
- HDU 1013.Digital Roots【模拟或数论】【8月16】
- 【博弈】【HDU】取石子游戏
- 我为什么推荐你使用kindle
- matlab中的实时音频
- MySQL/Oracle数据库优化总结
- Python内置函数(17)——divmod
- 图解HTTP第九章
- 使用Spring+MySql实现读写分离(二)spring整合多数据库
- Metasploit (二)
热门文章
- WebClient禁止自动重定向
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
- js高度line-height及宽度text-align:center居中插件
- Web大文件(夹)上传(断点续传)控件-Xproer.HttpUploader6
- Java 获取本地IP地址
- 图像处理之基础---卷积及其快速算法的C++实现
- 微信小程序设置控件权重
- EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:使用BootstrapPagination以分页形式展示数据信息
- css多余字符显示省略号
- matlab使用usb和gige 网口相机