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
- leetcode
- Climbing Stairs
- React useEffect 안티패턴
- RxJS 마블 다이어그램
- React hooks 남용 사례
- RxJS 생성 오퍼레이터
- React useMemo 사용법
- 달래스터디
- useCallback 성능 최적화
- React 리렌더링 최적화
- React useCallback 사용법
- 자바스크립트 고차 함수 vs Observable
- RxJS 오퍼레이터
- RxJS 에러 처리
- 스쿼드조직
- Blind75
- useMemo 성능 최적화
- 개발자커뮤니케이션
- leedcode
- 알고리즘스터디
- Observable vs Array
- RxJS 멀티캐스팅
- RxJS 변환 오퍼레이터
- 협업문화
- contains duplicate
- React 성능 최적화 방법
- DaleStudy
- 알고리즘
- RxJS 결합 오퍼레이터
- RxJS 함수형 프로그래밍
Archives
- Today
- Total
수쿵의 IT월드
DaleStudy | Leetcode Study 3주차 - Number of 1 Bits 본문

문제
Given a positive integer n, write a function that returns the number of set bits in its binary representation (also known as the Hamming weight).
Example 1:
Input: n = 11
Output: 3
Explanation:
The input binary string 1011 has a total of three set bits.
Example 2:
Input: n = 128
Output: 1
Explanation:
The input binary string 10000000 has a total of one set bit.
Example 3:
Input: n = 2147483645
Output: 30
Explanation:
The input binary string 1111111111111111111111111111101 has a total of thirty set bits.
Constraints:
- 1 <= n <= 2^31 - 1
풀이
function hammingWeight(n: number): number {
return n.toString(2).replace(/0/g, '').length;
};
이 문제는, 주어진 인풋의 값을 이진수로 변경했을 때, "1"의 갯수를 찾으면된다. 자바스크립트의 toString 메서드를 이용하면 2진수로 숫자를 변경할 수 있고, 거기에 정규식을 이용해서 "0"을 빼준 뒤 그 길이를 구해주면 끝난다.
Link: https://leetcode.com/problems/number-of-1-bits/description/