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
- adb connect
- Git
- npm package
- augmentedDevice
- 티스토리 성능
- github lfs
- custom printing
- github 100mb
- Each child in a list should have a unique "key" prop.
- rolldown
- electron-packager
- 이미지 데이터 타입
- Can't resolve
- nextjs
- adb pair
- react-native
- camera permission
- silent printing
- html
- animation
- camera access
- react-native-dotenv
- vercel git lfs
- github pdf
- Recoil
- device in use
- ELECTRON
- ffi-napi
- dvh
- Failed to compiled
Archives
- Today
- Total
Bleeding edge
[LeetCode] Reverse Prefix of Word - 자바스크립트 0617 본문
https://leetcode.com/problems/reverse-prefix-of-word/submissions/
Reverse Prefix of Word - 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
풀이
1. 주어진 word에 ch가 없으면 return ch
if (!word.includes(ch)) return word
2. word안 에서의 ch의 index를 구한다
const chIndex = word.indexOf(ch)
3. 0부터 chIndex까지의 문자를 slice한다. 이때 slice는 맨뒤에 범위가 본인을 포함하지 않기 때문에 chIndex+1로 마지막에 인덱스를 넣는다
4. 3에서 구한 것을 revsere한다. revserse는 문자에서 바로 사용할 수 없으니 split("").revserse().join("")을 사용한다
let prefix = word.slice(0, chIndex + 1).split("").reverse().join("")
5. 남아 있는 것을 구하고 4번에서 구한것과 붙인뒤 return 한다
const rest = word.slice(chIndex + 1)
return prefix + rest
전체풀이
var reversePrefix = function (word, ch) {
if (!word.includes(ch)) return word
const chIndex = word.indexOf(ch)
let prefix = word.slice(0, chIndex + 1).split("").reverse().join("")
const rest = word.slice(chIndex + 1)
return prefix + rest
};
'코딩테스트 공부' 카테고리의 다른 글
[LeetCode] 66. Plus One - 자바스크립트 0620 (0) | 2022.06.20 |
---|---|
[LeetCode] 1701. Average Waiting Time - 자바스크립트 0617 (0) | 2022.06.17 |
[LeetCode] 1299. Replace Elements with Greatest Element on Right Side - 자바스크립트 0617 (0) | 2022.06.17 |
[LeetCode] 75. Sort Colors - 자바스크립트 0616 (0) | 2022.06.16 |
[LeetCode] 136. Single Number - 자바스크립트 0616 (0) | 2022.06.16 |