프로그램 개발/미분류
[LeetCode/Python]Array and String - Implement strStr()
(ㅇㅅㅎ)
2020. 8. 28. 13:40
728x90
반응형
문제
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
haystack에서 needle이 처음 발견된 인덱스를 반환하거나 인덱스가 발견되지 않는다면 -1을 반환합니다.
Input : haystack = "hello", needle = "ll"
Output : 2
이 문제도 저는 알고리즘으로 풀었다기보다 기존 함수를 이용했습니다.
참고만 하시길 바랍니다.
추후에 알고리즘으로 푼다면 추가하도록 하겠습니다.
제 코드를 보시려면 더보기를 클릭하시면 됩니다.
더보기
파이썬 코드
find를 이용하여 간단하게 풀었습니다.
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
return haystack.find(needle) if needle in haystack else -1
|
반응형