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
                            
                        
                          
                          - react-native-dotenv
- device in use
- react-native
- Git
- nextjs
- html
- Failed to compiled
- github 100mb
- camera permission
- github pdf
- vercel git lfs
- ffi-napi
- Each child in a list should have a unique "key" prop.
- rolldown
- dvh
- npm package
- 이미지 데이터 타입
- silent printing
- adb connect
- custom printing
- 티스토리 성능
- github lfs
- electron-packager
- camera access
- animation
- augmentedDevice
- Can't resolve
- Recoil
- adb pair
- ELECTRON
                            Archives
                            
                        
                          
                          - Today
- Total
Bleeding edge
[LeetCode] 338. Counting Bits - 자바스크립트 - 0623 본문
https://leetcode.com/problems/counting-bits/
Counting Bits - 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
0~n까지를2진법으로 바꾸고, 1만카운트하고 push하는 문제이다. 만일 문제의 효율성을 올리고 싶다면, 지금 아래와같이 for문으로 0~n을 넣고 변환할게 아니라, while문을 활용하고 while문은 return 할 리스트의 n+1만큼 길이가 될 때까지, 리스트의 내용은 2진법에서 나올 수 있는 1의 경우의 수를 더하는 방법으로 풀이하면 된다. 이 포스트에서는 for문을 이용한 심플한 풀이를 이용할 것이다.
1. 10 진법을 담을 decimal arr를만든다.
    const decimal = []2. 0~n까지의 숫자를 1에서 만든 arr에 담는다.
    const decimal = []
    for (let i = 0; i < n + 1; i++) {
        decimal.push(i)
    }3. 각 component들을 map을 이용하여 1을 세도록 만든다.
    const bi = decimal.map((v) => v.toString(2).split("").filter(v=>v==="1"))4. return bi
    return bi
전체 풀이
var countBits = function (n) {
    const decimal = []
    for (let i = 0; i < n + 1; i++) {
        decimal.push(i)
    }
    const bi = decimal.map((v) => v.toString(2).split("").filter(v=>v==="1"))
    return bi
};'코딩테스트 공부' 카테고리의 다른 글
| [LeetCode] 1051. Height Checker - 자바스크립트 0624 (0) | 2022.06.24 | 
|---|---|
| [LeetCode] 2288. Apply Discount to Prices - 자바스크립트 0623 (0) | 2022.06.23 | 
| [LeetCode] 2126. Destroying Asteroids - 자바스크립트 0621 (0) | 2022.06.21 | 
| [LeetCode] 2053. Kth Distinct String in an Array - 자바스크립트 0621 (0) | 2022.06.21 | 
| [LeetCode] 1154. Day of the Year - 자바스크립트 0621 (0) | 2022.06.21 | 
 
          