본문 바로가기
LeetCode

LeetCode 28 Implement strStr()

by 콩순이냉장고 2021. 7. 6.

문제 URL : 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

 

문제접근법 : haystack의 문자열안에 needle이 처음으로 존재하는 첫번째 인덱스를 구하는겁니다.

따로설명 안드리겠습니다.  스스로 찾는구현을 해도되고 라이브러리 함수를 사용해도되기때문에 아주 쉬운문제입니다.

 

소스코드 :

 

c++

1
2
3
4
5
6
class Solution {
public:
    int strStr(string haystack, string needle) {
        return haystack.find(needle);
    }
};
cs

 

자바

1
2
3
4
5
class Solution {
    public int strStr(String haystack, String needle) {
        return haystack.indexOf(needle);
    }
}
cs

 

 

궁금한점 혹은 모르는점 논리적인오류등 어떤질문이라도 댓글은 언제나 환영입니다.

 

'LeetCode' 카테고리의 다른 글

LeetCode 38 Count and Say  (0) 2021.07.15
LeetCode ZigZag Conversion  (0) 2021.07.14
LeetCode 59 Spiral Matrix II  (0) 2021.02.10
LeetCode #100 Same Tree  (0) 2021.01.06
LeetCode #17 Letter Combinations of a Phone Number  (0) 2021.01.06