Number of 1 Bits - LeetCode 191 Java | Count Set Bits using (n & 1) Bitwise Trick | | Java Owl
Vivek Kumar
0:00 / 0:00
Number of 1 Bits - LeetCode 191 Java | Count Set Bits using (n & 1) Bitwise Trick | | Java Owl
6 просмотров · 7 дней назад
Vivek Kumar
8 подписчиков
6 просмотров · 7 дней назад
🦉 Mastering LeetCode While the World Sleeps ☕ | LeetCode 191 – Number of 1 Bits | Java Solution
In this video, we solve LeetCode Problem 191: Number of 1 Bits (Hamming Weight) using Java with a simple bitwise approach.
We check the last bit with (n & 1), count it if it's 1, then right shift n by 1. This is the most interview-friendly method - no string conversion, O(number of bits) time.
📌 In This Video
✅ Problem explanation - Count set bits / Hamming Weight
✅ Binary dry run - 11 = 1011 - 3 ones, 9 = 1001 - 2 ones
✅ Java implementation - Your exact code
✅ Time Complexity - O(k) where k = number of bits, Space O(1)
✅ Bonus Trick - Brian Kernighan: n & (n-1) removes one set bit at a time - O(set bits)
✅ Interview tip & edge cases for unsigned int
🔗 Useful Links
📄 LeetCode Problem: https://leetcode.com/problems/number-...
💻 My LeetCode Solution: https://leetcode.com/problems/number-...
👨💻 My LeetCode Profile: https://leetcode.com/u/vivekxsh/
📄 GFG Practice: https://www.geeksforgeeks.org/problem...
❤ Support the Channel
If you found this video helpful, please Like 👍, Subscribe 🔔, and Share 📤. Your support motivates me to create more high-quality coding tutorials, DSA explanations, and LeetCode solutions.
💬 Have questions or another problem you'd like me to solve? Leave a comment below!
Happy Coding! 🚀
#leetcode #leetcode191 #numberof1bits #hammingweight #bitmanipulation #java #leetcodejava #dsa #bitwiseoperator #javaowl #faang #interviewprep #javainterview