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 |
Tags
- npm package
- adb pair
- Recoil
- Can't resolve
- react-native
- html
- github pdf
- rolldown
- ffi-napi
- github 100mb
- Git
- animation
- github lfs
- 이미지 데이터 타입
- dvh
- custom printing
- silent printing
- device in use
- 티스토리 성능
- Each child in a list should have a unique "key" prop.
- ELECTRON
- adb connect
- camera access
- vercel git lfs
- nextjs
- electron-packager
- augmentedDevice
- Failed to compiled
- camera permission
- react-native-dotenv
Archives
- Today
- Total
Bleeding edge
[LeetCode] 1768. Merge Strings Alternately - 자바스크립트 0630 본문
https://leetcode.com/problems/merge-strings-alternately/
Merge Strings Alternately - 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
최대한 깔끔하게 보이게 하려고 풀이를 했다. 우선 이문제같은경우는 condition || falsy 를 사용하여 값을 가지고있는지 아닌지를 사용하는게 좋은 문제이다.
1. word1과 word2중에 길이가 더 긴 것을 구한다. 정답을 받을 result를 선언한다
const max = Math.max(word1.length, word2.length)
let result = ""
2. max를 기준으로 for문을 만들고 word1[i] || falsy가 참이면 값을 붙이고 없으면 null로 넘어가는 삼항 조건문을 word1, word2 각각 만든다.
for (let i = 0; i < max; i++) {
word1[i] || false ? result = result + word1[i] : null
word2[i] || false ? result = result + word2[i] : null
}
3. result를 return한다.
전체 풀이
var mergeAlternately = function (word1, word2) {
const max = Math.max(word1.length, word2.length)
let result = ""
for (let i = 0; i < max; i++) {
word1[i] || false ? result = result + word1[i] : null
word2[i] || false ? result = result + word2[i] : null
}
return result
};
'코딩테스트 공부' 카테고리의 다른 글
[LeetCode] 2150. Find All Lonely Numbers in the Array - 자바스크립트 0630 (0) | 2022.06.30 |
---|---|
[LeetCode] 2085. Count Common Words With One Occurrence - 자바스크립트 0630 (0) | 2022.06.30 |
[LeetCode] 1447. Simplified Fractions - 자바스크립트 0628 (0) | 2022.06.28 |
[LeetCode] 2180. Count Integers With Even Digit Sum - 자바스크립트 0628 (0) | 2022.06.28 |
[LeetCode] 2032. Two Out of Three - 자바스크립트 0628 (0) | 2022.06.28 |