일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 모르고리즘
- DFS
- 자고 싶다
- 네트워크
- HAVE A GOOD DAY
- 우유가옆으로넘어지면아야
- SSAFY 테스트
- 텐션 업 10기!
- 텐션 업 10기 화이팅
- I am Korean
- DP
- 우유아야
- SSAFY IM/A
- SeongSeobDang
- 우유가 옆으로 넘어지면 아야
- BFS
- 수학
- Have a good day :)
- Java 환경 설정
- SSAFY 10기 화이팅
- amazon
- have a nice day
- 자료구조
- 아자아자 화이팅
- Have a nice day.
- 코로나 싫어요
- LeetCode #릿코드 #좋은 하루 되세요 #Have a nice day
- SSAFY 화이팅
- Hamming weight
- Today
- Total
Hope Everyone Is Happy
[Easy] 191. Number of 1Bits (Java) 본문
[Easy] 191. Number of 1Bits (Java)
J 크 2023. 11. 29. 17:39https://leetcode.com/problems/number-of-1-bits/
Number of 1 Bits - LeetCode
Can you solve this real interview question? Number of 1 Bits - Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight [http://en.wikipedia.org/wiki/Hamming_w
leetcode.com
음주 이슈,,,
※ Question Summary
▶ Given an unsigned integer and return the number of '1' bits it has (known as the Hamming weight)
▶ Use integer as unsigned integer since there is no unsigned integer in Java
▶ Input : Unsigned integer n
▶ Output : Return the number of '1'bits it has
◈ Constraints
- The input must be a binary string of length 32.
◈ Input - 1
00000000000000000000000000001011
◈ Output - 1
3
◈ Input - 2
00000000000000000000000010000000
◈ Output - 2
1
◈ Input - 3
11111111111111111111111111111101
◈ Output - 3
31
◎ HOW TO SOLVE IT
▶ An unsigned integer has 4 bytes, which means it needs to be checked for 32 bits
▶ Check 'n & 1' 32 times, and each time after checking, shift 'n' to the right by 1 bit
▶ return the count of 1bits
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int nCount = 0;
for(int i = 0; i < 32; i++) {
int nBit = n & 1;
if(nBit == 1)
nCount++;
n = n >> 1;
}
return nCount;
}
}
I HOPE YOUR DAY GOES WELL :)
'※ 릿코드 ( LeetCode ) > [Java] 문제 풀이 ( Solve the problems)' 카테고리의 다른 글
[Medium] 419. Battleships in a Board (Java) (3) | 2023.12.05 |
---|---|
[Medium] 366. Find Leaves of Binary Tree (Java) (0) | 2023.12.04 |
[Hard] 329. Longest Increasing Path in a Matrix (Java) (0) | 2023.11.28 |
[Medium] 253. Meeting Rooms II (Java) (0) | 2023.11.27 |
Pascal's Triangle (Java) (4) | 2023.07.18 |