LintCode(easy)字符串查找
2016-07-23 15:39
309 查看
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) { if(!target||!source) return -1; string A(source); int m; string B(target); vector<int> v1; // 截取字符串前三个字符 if (A.size() < B.size()) return -1; else{ if (B.size() > 3){ for (int i = 0; i < A.size(); i++){ if (A[i] == B[0] && A[i + 1] == B[1] && A[i + 2] == B[2]) v1.push_back(i + 3); } if (v1.size() == 0) return -1; for (auto c : v1){ for (m = 0; m < B.size()-3; m++) if (A[c+m] != B[3+m]) break; if (m == B.size()-3) return c - 3; } } else{ for (int i = 0; i < A.size() - B.size() + 2; i++){ for (m = 0; m < B.size() ; m++) if (A[i+m] != B[m]) break; if (m == B.size()) return i+m - B.size(); } } } return -1; } };
相关文章推荐
- UVA 10167 Birthday Cake
- 【杭电5363】Key Set
- [置顶] Android java程序员必备技能,集合与数组中遍历元素,增强for循环的使用详解及代码
- Java 中的静态内部类
- R语言知识体系概览
- Android Layout浅析
- POJ---3468
- 微信朋友圈广告开发总结
- UINavigationController 详解
- codeforces 701E E. Connecting Universities(树的重心)
- DP练习 - 机器分配
- ssh整合中web.xml配置文件
- Yii2简单实现多语言配置的方法
- Python:时间模块:time和datetime
- HDU----1689
- 【java项目】迁移到linux下
- Django部署(Nginx)
- OXWALL优化思路
- CSS Display(显示)和visibility(可见性)
- 一句话看懂getactivity和this区别