LintCode: strStr
2016-01-07 11:26
218 查看
C++
(1) null
(2) length is 0
(3) return value
(4) strlen
(1) null
(2) length is 0
(3) return value
(4) strlen
class Solution { public: /** * Returns a index to the first occurrence of target in source, * or -1 if target is not part of source. * @param source string to be scanned. * @param target string containing the sequence of characters to match. */ int strStr(const char *source, const char *target) { // write your code here if (source == NULL || target == NULL) return -1; int i, j, len_s = strlen(source), len_t = strlen(target); if (len_s == 0 && len_t == 0) return 0; i = 0; while (source[i] != '\0') { if (i + len_t > len_s) return -1; j = 0; while (target[j] != '\0') { if (source[i + j] == target[j]) { j++; } else { break; } } if (target[j] == '\0') return i; i++; } return -1; } };
相关文章推荐
- 关于进程、线程的学习分享
- OC中NSArray
- composer 的初步使用
- K阶斐波那契数列
- 深度学习之常见tricks
- mysql 性能优化方案
- 老板发红包“钓鱼罚款”虽不违法失了员工心
- VS2015集成QT5.6.0
- 起底半成品生鲜行业之殇,下一步如何破局?
- jquery json实现省市级级联
- iOS应用程序的生命周期
- 2 Maven-安装和配置
- 7个去伪存真的JavaScript面试题
- eclipse相关知识点
- html5的本地存储问题
- Lucene Steming分词(包含单复数 snowball)
- log4net配置
- JDBC对数据库时间的操作
- POI 读取Excel文档中的数据——兼容Excel2003和Excel2007
- Mvc生命周期深度剖析