-
[LeetCode] Implement strStr()알고리즘 2019. 9. 30. 01:13
문제
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll" Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba" Output: -1
Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
Approach
string find()가 이 문제 해결 그 자체이다. 없으면 npos고 있으면 시작 인덱스를 알려주기 때문에...
나는 새롭게 substr로 풀어보았다.
이게 무슨 의미가 있는 문제인가
Code
https://github.com/chi3236/algorithm/blob/master/LeetCode_Implement_strStr().cpp
'알고리즘' 카테고리의 다른 글
[LeetCode] Longest Valid Parentheses (0) 2019.10.09 [LeetCode] Divide Two Integers (0) 2019.10.02 [LeetCode] Remove Duplicates from Sorted Array (0) 2019.09.29 [LeetCode] Merge k Sorted Lists (0) 2019.09.28 [LeetCode] Generate Parentheses (0) 2019.09.26