mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
25
Easy/implement-strstr/solution.cpp
Normal file
25
Easy/implement-strstr/solution.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int strStr(std::string haystack, std::string needle) {
|
||||
for (int i = 0; i < haystack.size(); ++i) {
|
||||
if (haystack.at(i) == needle.at(0)) {
|
||||
bool found = false;
|
||||
for (int j = 0; i + j < haystack.size() && j < needle.size(); ++j) {
|
||||
if (haystack.at(i + j) != needle.at(j)) {
|
||||
break;
|
||||
} else if (j == needle.size() - 1) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user