728x90
https://leetcode.com/problems/implement-strstr/
Implement strStr() - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문자열 1에 문자열 2가 포함되어있다면 어디서부터 시작하는지를 묻는 문제, string::npos를 사용하면 된다.
int strStr(string haystack, string needle) {
int answer = 0;
size_t nPos = haystack.find(needle);
if(nPos == string::npos) return -1;
else answer = nPos;
return answer;
}
728x90
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode/easy] Add Binary (0) | 2022.08.12 |
---|---|
[LeetCode/easy] Length of Last Word (0) | 2022.08.12 |
[LeetCode/easy] Valid Parentheses (0) | 2022.08.12 |
[LeetCode/easy] Longest Common Prefix (0) | 2022.04.18 |
[LeetCode/easy] Roman To Integer (0) | 2022.04.18 |