250x250
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
- nextjs
- 구름톤 챌린지
- 날짜 테스트
- Google 애널리틱스
- 헤르메스 엔진
- 호텔 대실
- 테스트 Date
- ResizeObserver
- 연결 요소 제거하기
- JavaScript
- Jest uuid syntax
- nextjs-performance
- Hermes Engine
- 테이블 해시 함수
- 과제 진행하기
- 구름톤챌린지
- 최솟갑 구하기
- 자바스크립트
- 귤 고르기
- 중첩 점
- 프로그래머스
- jest
- Leetcode #javascript #알고리즘 #Algorithms #js
- 구름톤
- mock date
- mutationobserver
- 리액트네이티브 엔진
- 리액트네이티브
- 통신망분석
- create-next-app
Archives
- Today
- Total
나만보는개발공부블로그
Implement strStr() 본문
설명
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
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().
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
Example 3:
Input: haystack = "", needle = ""
Output: 0
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
if (needle.length === 0) return 0;
for (let i = 0; i < haystack.length; ++i) {
let j = i, k = 0;
while (haystack[j] === needle[k] && k < needle.length) {
j++;
k++;
}
if (k === needle.length) {
return i;
}
}
return -1;
};
'Algorithms > leetcode' 카테고리의 다른 글
Reverse Linked List (0) | 2021.03.23 |
---|---|
Valid Palindrome (0) | 2021.03.19 |
merge-two-sorted-lists (0) | 2021.02.11 |
Climbing Stair (0) | 2021.02.10 |
Longest Common Prefix (0) | 2021.02.09 |