Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 주간회고
- 필기
- Python
- 2020년 정보처리기사 4회
- 스터디
- algorithms
- java
- post
- Real MySQL
- 정보처리기사
- git
- 성적프로그램
- 책리뷰
- 회고
- 알고리즘
- 항해99
- 미니프로젝트
- 코드숨
- sqldeveloper
- 함수형 코딩
- Jackson
- LeetCode
- Til
- 뇌정리
- If
- 2020년 일정
- 서평
- hackerrank
- jsp
- 2020년 제4회 정보처리기사 필기 문제 분석
Archives
- Today
- Total
조컴퓨터
28. Implement strStr() 본문

? indexOf 로 바로 풀린다.
class Solution {
public int strStr(String haystack, String needle) {
int num = haystack.indexOf(needle);
if( needle.length() == 0 ) {
num = 0;
}
return num;
}
}
너무 빨리 끝나서 다른 방향을 더 생각해 보았다.
class Solution {
public int strStr(String haystack, String needle) {
if( needle.length() == 0 ) return 0;
if( haystack.length() == 0 ) return -1;
int i = 0, j = 0;
while( i<haystack.length() && j<needle.length() ) {
if( haystack.charAt(i) == needle.charAt(j) ) {
i++;
j++;
} else {
i = i-j+1;
j = 0;
}
}
if( j == needle.length() ) {
return i-j ;
}
return -1;
}
}
'LeetCode > Algorithms' 카테고리의 다른 글
53. Maximum Subarray (0) | 2021.10.27 |
---|---|
35. Search Insert Position (0) | 2021.10.20 |
27. Remove Element (0) | 2021.10.19 |
26. Remove Duplicates from Sorted Array (0) | 2021.10.17 |
*21. Merge Two Sorted Lists (0) | 2021.10.17 |