题目:

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 -;
}
};

最新文章

  1. Android Studio doesn&#39;t list my android phone
  2. AJAX文件上传实践与分析,带HTML5文件上传API。
  3. Android Studio-目录结构
  4. NOIp DP 1003 爆零记
  5. C# 配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler”节
  6. c++字符串互相转换
  7. 武汉科技大学ACM :1001: 零起点学算法34——继续求多项式
  8. 动画api说明
  9. dokcer 运行和进入容器
  10. P/Invoke与逆向P/Invoke
  11. spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava
  12. HDU 1013.Digital Roots【模拟或数论】【8月16】
  13. 【博弈】【HDU】取石子游戏
  14. 我为什么推荐你使用kindle
  15. matlab中的实时音频
  16. MySQL/Oracle数据库优化总结
  17. Python内置函数(17)——divmod
  18. 图解HTTP第九章
  19. 使用Spring+MySql实现读写分离(二)spring整合多数据库
  20. Metasploit (二)

热门文章

  1. WebClient禁止自动重定向
  2. leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
  3. js高度line-height及宽度text-align:center居中插件
  4. Web大文件(夹)上传(断点续传)控件-Xproer.HttpUploader6
  5. Java 获取本地IP地址
  6. 图像处理之基础---卷积及其快速算法的C++实现
  7. 微信小程序设置控件权重
  8. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:使用BootstrapPagination以分页形式展示数据信息
  9. css多余字符显示省略号
  10. matlab使用usb和gige 网口相机