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
- hackerrank
- 미니프로젝트
- Til
- 뇌정리
- 2020년 일정
- LeetCode
- post
- 성적프로그램
- java
- jsp
- 함수형 코딩
- 주간회고
- 2020년 정보처리기사 4회
- 정보처리기사
- algorithms
- 책리뷰
- sqldeveloper
- Python
- If
- 알고리즘
- 항해99
- Real MySQL
- 서평
- Jackson
- git
- 회고
- 필기
- 스터디
- 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 |