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
- 이미지 데이터 타입
- adb pair
- Can't resolve
- ELECTRON
- Recoil
- custom printing
- camera permission
- rolldown
- dvh
- github pdf
- adb connect
- augmentedDevice
- electron-packager
- github lfs
- nextjs
- Each child in a list should have a unique "key" prop.
- html
- react-native-dotenv
- npm package
- Failed to compiled
- device in use
- react-native
- ffi-napi
- vercel git lfs
- camera access
- animation
- Git
- github 100mb
- 티스토리 성능
- silent printing
Archives
- Today
- Total
Bleeding edge
[LeetCode] 1528. Shuffle String - 자바스크립트 0613 본문
https://leetcode.com/problems/shuffle-string/
Shuffle String - 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
문제에 str한개와 array 하나가 주어진다.
array에 주어진 순서대로 str에 있는 글자를 옮기면된다.
문제 풀이
1. 문자를 한글자 떼고, array의 글자와 붙여서 새로운 정렬에 대입한다
const calc = []
for (let i = 0; i < s.length; i++) {
calc.push([s[i], indices[i]])
}
2. calc에 주어진 순서대로 정렬을 분류한다.
이과정에서 위에 주어진 그림대로 배치가 된다.
calc.sort((a, b) => a[1] - b[1])
3. calc에 있는 모든 arr에 [0]을 answer에 붙인다
let answer = ""
for (let i = 0; i < calc.length; i++) {
answer += calc[i][0]
}
전체 코드
var restoreString = function (s, indices) {
const calc = []
for (let i = 0; i < s.length; i++) {
calc.push([s[i], indices[i]])
}
calc.sort((a, b) => a[1] - b[1])
let answer = ""
for (let i = 0; i < calc.length; i++) {
answer += calc[i][0]
}
return answer
};
'코딩테스트 공부' 카테고리의 다른 글
[LeetCode] 3. Longest Substring Without Repeating Characters - 자바스크립트 0614 (0) | 2022.06.14 |
---|---|
[LeetCode] 79. Word Search - 자바스크립트 0613 (0) | 2022.06.13 |
[LeetCode] 2235. Add Two Integers - 자바스크립트 0613 (0) | 2022.06.13 |
[LeetCode] 2295. Replace Elements in an Array - 자바스크립트 0610 (0) | 2022.06.10 |
[LeetCode] 2287. Rearrange Characters to Make Target String - 자바스크립트 0610 (0) | 2022.06.10 |