LeetCode28. Implement strStr()
2017-03-27 12:22
393 查看
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
从一个字符串中查找给定字符串第一次出现的位置
Runtime: 6 ms
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
从一个字符串中查找给定字符串第一次出现的位置
#include<string> #include<iostream> using namespace std; class Solution { public: int strStr(string haystack, string needle) { int res=haystack.find(needle); return res=(res==string::npos)?-1:res; } }; //class Solution { //public: // int strStr(string haystack, string needle) { // if(haystack.empty()&&needle.empty()) // return 0; // string::iterator it=search(haystack.begin(),haystack.end(),needle.begin(),needle.end()); // return it==haystack.end()?-1:it-haystack.begin(); // } //}; void main(){ string str1="helloyouare"; string str2="you"; Solution So; int ind =So.strStr(str1,str2); cout<<ind<<endl; }
Runtime: 6 ms
相关文章推荐
- leetcode 28. Implement strStr()
- LeetCode 28. Implement strStr()
- leetcode 28. Implement strStr() 实现strStr()函数
- leetcode:28. Implement strStr()
- 【一天一道LeetCode】#28. Implement strStr()
- LeetCode 28. Implement strStr()
- Leetcode-28. Implement strStr()
- Leetcode28. Implement strStr()
- 28. Implement strStr()-leetcode-java
- 【leetcode】28. Implement strStr()
- LeetCode-28. Implement strStr()
- LeetCode *** 28. Implement strStr()
- Leetcode 28. Implement strStr()
- Sunday算法的一种Java实现(Leetcode28. Implement strStr())
- Leetcode 28. Implement strStr() KMP算法解题
- [LeetCode] 28. Implement strStr() 实现strStr()函数
- LeetCode 28. Implement strStr()
- LeetCode笔记:28. Implement strStr()
- python写算法题:leetcode: 28. Implement strStr()
- LeetCode 28. Implement strStr()