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 |
Tags
- Can't resolve
- camera access
- augmentedDevice
- custom printing
- react-native
- react-native-dotenv
- html
- vercel git lfs
- github lfs
- Recoil
- Failed to compiled
- device in use
- 이미지 데이터 타입
- animation
- 티스토리 성능
- electron-packager
- github pdf
- adb pair
- camera permission
- ffi-napi
- Git
- adb connect
- silent printing
- dvh
- npm package
- ELECTRON
- rolldown
- nextjs
- github 100mb
- Each child in a list should have a unique "key" prop.
Archives
- Today
- Total
Bleeding edge
[leetcode] 28. Implement strStr() - 자바스크립트 0607 본문
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이다.haystack안에 needle이 포함되어 있다면, needle의 indexOf를 찾으라는 문제이다. 이때, needle이 주어지지 않았다면 0이나오게하고, 포함되지 않아있다면 -1을 내보내야한다.
이 문제는 문제풀이를 세우는 건 간단하지만 Boundary Condition을 세우는 것이 매우 중요한 문제다.(문제 풀이 할 때 오답이 나왔던 이유)
1. needle에 변수가 안들어간 경우에는 0을 return 한다
if (!needle) return 0
2. haystack에 needle이 없으면 -1을 리턴할 것
return -1
그리고 일반적인 문제 풀이인
if (haystack.includes(needle)) return haystack.indexOf(needle)
includes로 유무를 확인한뒤, return indexof를 넣는다. 문제를 풀면서 6분정도를 더 소요했던게... 문제를 needle.length를 내보내는줄알고.. 시간을 쓸데 없이보냈다.. 문제를 잘읽어야겠다.. 흑..
전체 문제풀이
var strStr = function (haystack, needle) {
if (!needle) return 0
if (haystack.includes(needle)) return haystack.indexOf(needle)
return -1
};
'코딩테스트 공부' 카테고리의 다른 글
[LeetCode]796. Rotate String - 자바스크립트 0609 (0) | 2022.06.09 |
---|---|
[leetcode] 34. Find First and Last Position of Element in Sorted Array - 자바스크립트 0607 (0) | 2022.06.07 |
[leetcode] 14. Longest Common Prefix - 자바스크립트 0607 (0) | 2022.06.07 |
[프로그래머스] 멀쩡한 사각형 - 자바스크립트 0606 (0) | 2022.06.06 |
[프로그래머스] 직사각형 별찍기 - 자바스크립트 0606 (0) | 2022.06.06 |