diff --git a/Java/number-complement.java b/Java/number-complement.java new file mode 100644 index 00000000..99d46bd7 --- /dev/null +++ b/Java/number-complement.java @@ -0,0 +1,22 @@ +class Solution { + public int findComplement(int num) { + + int numberOfBits = numberOfBits(num); + + return num ^ ((1 << numberOfBits) - 1); + } + + /** + * Method to find out the total number of bits in a number. + **/ + public static int numberOfBits(int num){ + int countOfBits = 0; + + while(num != 0){ + num = num >> 1; + countOfBits++; + } + + return countOfBits; + } +} diff --git a/README.md b/README.md index 1ddd6a1a..59755a22 100644 --- a/README.md +++ b/README.md @@ -68,11 +68,12 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu # Bit Manipulation -| # | Title | Solution | Time | Space | Difficulty | Tag | Note | +| # | Title | Solution | Time | Space | Difficulty | Tag | Tutorial | | ---- | --------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | --------- | | 0136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java)
[Python](./Python/single-number.py)
[C++](./C++/Single-Number.cpp) | _O(n)_ | _O(1)_ | Easy | | Using XOR | -| 0260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | | -| 0520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | | +| 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | | +| 476 | [Number Complement](https://leetcode.com/problems/number-complement/) | [Java](./Java/number-complement.java) | _O(1)_ | _O(1)_ | Easy | | | +| 520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
@@ -357,7 +358,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if ## Authors -- [Gourav Rusiya](https://github.com/GouravRusiya30/) +- | [Gourav Rusiya](https://github.com/GouravRusiya30/)