Skip to content

Commit 98ff850

Browse files
committed
Added Java Implementations
1 parent 90c2f6d commit 98ff850

14 files changed

+444
-9
lines changed

Bit-Manipulation/NumberOf1Bits191.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
2+
3+
// Note:
4+
5+
// Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
6+
// In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer. -3.
7+
8+
9+
// Example 1:
10+
11+
// Input: n = 00000000000000000000000000001011
12+
// Output: 3
13+
// Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
14+
// Example 2:
15+
16+
// Input: n = 00000000000000000000000010000000
17+
// Output: 1
18+
// Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
19+
// Example 3:
20+
21+
// Input: n = 11111111111111111111111111111101
22+
// Output: 31
23+
// Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
24+
25+
26+
// Constraints:
27+
28+
// The input must be a binary string of length 32.
29+
30+
31+
// Follow up: If this function is called many times, how would you optimize it?
32+
33+
class Solution {
34+
public:
35+
int hammingWeight(uint32_t n) {
36+
int count = 0;
37+
for(int i = 0; i < 32; i++){
38+
count += n & 1;
39+
n >>= 1;
40+
}
41+
return count;
42+
}
43+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
2+
3+
// Note:
4+
5+
// Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
6+
// In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer. -3.
7+
8+
9+
// Example 1:
10+
11+
// Input: n = 00000000000000000000000000001011
12+
// Output: 3
13+
// Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits.
14+
// Example 2:
15+
16+
// Input: n = 00000000000000000000000010000000
17+
// Output: 1
18+
// Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
19+
// Example 3:
20+
21+
// Input: n = 11111111111111111111111111111101
22+
// Output: 31
23+
// Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
24+
25+
26+
// Constraints:
27+
28+
// The input must be a binary string of length 32.
29+
30+
31+
// Follow up: If this function is called many times, how would you optimize it?
32+
33+
34+
public class Solution {
35+
// you need to treat n as an unsigned value
36+
public int hammingWeight(int n) {
37+
int count = 0;
38+
for(int i = 0; i < 32; i++){
39+
count += n & 1;
40+
n >>= 1;
41+
}
42+
return count;
43+
}
44+
}

Bit-Manipulation/Power-of-Two.cpp renamed to Bit-Manipulation/Power-of-Two-231.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// Constraints:
2020
// 0 ≤ N ≤ 1018
2121

22+
// Method 1
2223
class Solution{
2324
public:
2425
// Function to check if given number n is a power of two.
@@ -27,3 +28,25 @@ class Solution{
2728
return !(n & (n-1));
2829
}
2930
};
31+
32+
// Method 2
33+
class Solution {
34+
public:
35+
bool isPowerOfTwo(int n) {
36+
if(n==0) return false;
37+
return ((ceil(log2(n)))==floor(log2(n)));
38+
}
39+
};
40+
41+
// Method 3
42+
43+
class Solution {
44+
public:
45+
bool isPowerOfTwo(int n) {
46+
if(n<=0) return false;
47+
while(n%2 == 0){
48+
n/=2;
49+
}
50+
return n==1;
51+
}
52+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// https://practice.geeksforgeeks.org/problems/power-of-2-1587115620/1#
2+
// Given a non-negative integer N. The task is to check if N is a power of 2. More formally, check if N can be expressed as 2x for some x.
3+
// Example 1:
4+
5+
// Input: N = 1
6+
// Output: true
7+
// Explanation:
8+
// 1 is equal to 2 raised to 0 (20 = 1).
9+
// Example 2:
10+
11+
// Input: N = 98
12+
// Output: false
13+
// Explanation:
14+
// 98 cannot be obtained by any power of 2.
15+
16+
// Expected Time Complexity: O(log N).
17+
// Expected Auxiliary Space: O(1).
18+
19+
// Constraints:
20+
// 0 ≤ N ≤ 1018
21+
22+
23+
// Method 1
24+
25+
class Solution {
26+
public boolean isPowerOfTwo(int n) {
27+
if(n <= 0) return false;
28+
while(n%2 == 0)
29+
n /= 2;
30+
return n == 1;
31+
}
32+
}
33+
34+
// Method 2
35+
36+
class Solution {
37+
public boolean isPowerOfTwo(int n) {
38+
if(n <= 0) return false;
39+
int temp = n & (n-1);
40+
return temp == 0;
41+
}
42+
}

Bit-Manipulation/ReverseBits190.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Reverse bits of a given 32 bits unsigned integer.
2+
3+
// Note:
4+
5+
// Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
6+
// In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.
7+
8+
9+
// Example 1:
10+
11+
// Input: n = 00000010100101000001111010011100
12+
// Output: 964176192 (00111001011110000010100101000000)
13+
// Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
14+
// Example 2:
15+
16+
// Input: n = 11111111111111111111111111111101
17+
// Output: 3221225471 (10111111111111111111111111111111)
18+
// Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
19+
20+
21+
// Constraints:
22+
23+
// The input must be a binary string of length 32
24+
25+
26+
// Follow up: If this function is called many times, how would you optimize it?
27+
28+
// Understandable Code:
29+
30+
class Solution {
31+
public:
32+
uint32_t reverseBits(uint32_t n) {
33+
uint32_t result = 0;
34+
for(int i = 0; i < 32; i++){
35+
uint32_t lsb = n & 1;
36+
uint32_t reverseLsb = lsb << (31 - i);
37+
result = result | reverseLsb;
38+
n >>= 1;
39+
}
40+
return result;
41+
}
42+
};
43+
44+
// Concise :P
45+
46+
class Solution {
47+
public:
48+
uint32_t reverseBits(uint32_t n) {
49+
uint32_t ans=0;
50+
for(int i=0; i<32; i++){
51+
ans += ((1<<(31-i)) & n) ? 1<<i:0;
52+
}
53+
return ans;
54+
}
55+
};

Bit-Manipulation/ReverseBits190.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Reverse bits of a given 32 bits unsigned integer.
2+
3+
// Note:
4+
5+
// Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
6+
// In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.
7+
8+
9+
// Example 1:
10+
11+
// Input: n = 00000010100101000001111010011100
12+
// Output: 964176192 (00111001011110000010100101000000)
13+
// Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
14+
// Example 2:
15+
16+
// Input: n = 11111111111111111111111111111101
17+
// Output: 3221225471 (10111111111111111111111111111111)
18+
// Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
19+
20+
21+
// Constraints:
22+
23+
// The input must be a binary string of length 32
24+
25+
26+
// Follow up: If this function is called many times, how would you optimize it?
27+
28+
public class Solution {
29+
// you need treat n as an unsigned value
30+
public int reverseBits(int n) {
31+
int result = 0;
32+
for(int i = 0; i < 32; i++){
33+
int lsb = n & 1;
34+
int reverseLsb = lsb << (31 - i);
35+
result = result | reverseLsb;
36+
n >>= 1;
37+
}
38+
return result;
39+
}
40+
}

Bit-Manipulation/SingleNumber136.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
2+
// You must implement a solution with a linear runtime complexity and use only constant extra space.
3+
4+
// Example 1:
5+
6+
// Input: nums = [2,2,1]
7+
// Output: 1
8+
// Example 2:
9+
10+
// Input: nums = [4,1,2,1,2]
11+
// Output: 4
12+
// Example 3:
13+
14+
// Input: nums = [1]
15+
// Output: 1
16+
17+
// Constraints:
18+
// 1 <= nums.length <= 3 * 104
19+
// -3 * 104 <= nums[i] <= 3 * 104
20+
// Each element in the array appears twice except for one element which appears only once.
21+
22+
// METHOD 1
23+
24+
class Solution {
25+
public:
26+
int singleNumber(vector<int>& nums) {
27+
int ans = 0;
28+
for(int num : nums)
29+
ans ^= num;
30+
return ans;
31+
}
32+
};
33+
34+
// METHOD 2
35+
36+
class Solution {
37+
public:
38+
int singleNumber(vector<int>& nums) {
39+
unordered_map<int, int> m;
40+
for(int num : nums){
41+
m[num]++;
42+
}
43+
for(auto it = m.begin(); it != m.end(); ++it){
44+
if(it->second == 1) return it->first;
45+
}
46+
return 0;
47+
}
48+
};

Bit-Manipulation/SingleNumber136.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
2+
// You must implement a solution with a linear runtime complexity and use only constant extra space.
3+
4+
// Example 1:
5+
6+
// Input: nums = [2,2,1]
7+
// Output: 1
8+
// Example 2:
9+
10+
// Input: nums = [4,1,2,1,2]
11+
// Output: 4
12+
// Example 3:
13+
14+
// Input: nums = [1]
15+
// Output: 1
16+
17+
// Constraints:
18+
// 1 <= nums.length <= 3 * 104
19+
// -3 * 104 <= nums[i] <= 3 * 104
20+
// Each element in the array appears twice except for one element which appears only once.
21+
22+
// METHOD 1
23+
class Solution {
24+
public int singleNumber(int[] nums) {
25+
int ans = 0;
26+
for(int num : nums)
27+
ans ^= num;
28+
return ans;
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
2+
3+
// Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.
4+
5+
6+
7+
// Example 1:
8+
9+
// Input: nums = [1,2,3,1]
10+
// Output: 4
11+
// Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
12+
// Total amount you can rob = 1 + 3 = 4.
13+
// Example 2:
14+
15+
// Input: nums = [2,7,9,3,1]
16+
// Output: 12
17+
// Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
18+
// Total amount you can rob = 2 + 9 + 1 = 12.
19+
20+
21+
class Solution {
22+
public int rob(int[] nums) {
23+
int n = nums.length;
24+
if(n == 0) return 0;
25+
if(n == 1) return nums[0];
26+
int[] dp = new int[n];
27+
dp[0] = nums[0];
28+
dp[1] = Math.max(nums[0], nums[1]);
29+
for(int i = 2; i < nums.length; i++)
30+
dp[i] = Math.max(dp[i-1], nums[i] + dp[i-2]);
31+
return Math.max(dp[nums.length - 1], dp[nums.length - 2]);
32+
}
33+
}

0 commit comments

Comments
 (0)