博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LintCode: strStr
阅读量:7196 次
发布时间:2019-06-29

本文共 1179 字,大约阅读时间需要 3 分钟。

C++

(1) null

(2) length is 0

(3) return value

(4) strlen

1 class Solution { 2 public: 3     /** 4      * Returns a index to the first occurrence of target in source, 5      * or -1  if target is not part of source. 6      * @param source string to be scanned. 7      * @param target string containing the sequence of characters to match. 8      */ 9     int strStr(const char *source, const char *target) {10         // write your code here11         if (source == NULL || target == NULL) return -1;12         13         int i, j, len_s = strlen(source), len_t = strlen(target);14         15         if (len_s == 0 && len_t == 0) return 0;16         17         i = 0;18         while (source[i] != '\0') {19             if (i + len_t > len_s) return -1;20             j = 0;21             while (target[j] != '\0') {22                 if (source[i + j] == target[j]) {23                     j++;24                 } else {25                     break;26                 }27             }28             if (target[j] == '\0')  return i;29             i++;30         }31         return -1;32     }33 };

 

本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5109195.html,如需转载请自行联系原作者

你可能感兴趣的文章
arm c中字节对齐
查看>>
51cto我来了
查看>>
Linux的Web服务基础概念
查看>>
LNMP搭建
查看>>
RecyclerView的基础使用
查看>>
nginx学习笔记
查看>>
安装PHP扩展eaccelerator加速器
查看>>
SVN 学习
查看>>
SmartSVN设置ignoreList
查看>>
ios-网址中的中文或者非法字符转换
查看>>
自定义通知与系统通知的学习(详解)
查看>>
[翻译]Selenium自动化测试框官网翻译-目录
查看>>
Application tried to present modal view controller on itself
查看>>
再见2011,奋斗2012
查看>>
菜鸟学Linux 第051篇笔记 web-server, http
查看>>
我的友情链接
查看>>
Windows Server 2008遗忘管理员密码后的解决方法
查看>>
传统电脑与终端机的区别优势
查看>>
"廖雪峰的Git教程"学习笔记
查看>>
mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey
查看>>