Commented code to explain algorithms in 190 and 191

This commit is contained in:
SaurusXI 2019-10-04 17:04:24 +05:30
parent d2ad4b0109
commit 08f62bf397
4 changed files with 17 additions and 14 deletions

View File

@ -32,6 +32,7 @@ LeetCode
|169|[Majority Element](https://leetcode.com/problems/majority-element/) | [C](./src/169.c)|Easy|
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [C](./src/173.c)|Medium|
|190|[Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C](./src/190.c)|Easy|
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C](./src/191.c)|Easy|
|203|[Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/) | [C](./src/203.c)|Easy|
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [C](./src/206.c)|Easy|
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C](./src/215.c)|Medium|
@ -47,7 +48,6 @@ LeetCode
|389|[Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C](./src/389.c)|Easy|
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C](./src/404.c)|Easy|
|442|[Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [C](./src/442.c)|Medium|
|461|[Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C](./src/461.c)|Easy|
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [C](./src/509.c)|Easy|
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C](./src/520.c)|Easy|
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c)|Easy|

View File

@ -1,10 +1,13 @@
uint32_t reverseBits(uint32_t n) {
uint TotalBits = 32;
uint32_t reverse_int = 0;
uint32_t reverse_int = 0; //stored in memory as 32 bits, each bit valued 0
uint i;
for(i = 0; i < TotalBits; i++) {
if((n & (UINT32_C(1) << i)))
reverse_int = reverse_int | (UINT32_C(1) << (TotalBits - 1 - i));
if((n & (UINT32_C(1) << i))) //if the bit on the ith position of 32 bit input is 1, then proceed
//Further note the use of UINT32_C to convert 1 to unsigned 32 bit int, since just 1 is treated as int which cannot be shifted left more than 30 times
reverse_int = reverse_int | (UINT32_C(1) << (TotalBits - 1 - i)); //Convert the ith bit from the end in reverse_int from 0 to 1, if ith bit from beginning in n is 1
//This is achieved by using bitwise OR on reverse_int (where ith bit from end is currently 0) and
//1 shifted left 31 - i bits (to ith bit from the end)
}
return reverse_int;
}

10
leetcode/src/191.c Normal file
View File

@ -0,0 +1,10 @@
int hammingWeight(uint32_t n) {
int TotalBits = 32;
int i, weight = 0;
for(i = 0; i < TotalBits; i++) {
if(n & (UINT32_C(1) << i)) //if the bit on the ith position of 32 bit input is 1, then proceed
//Further note the use of UINT32_C to convert 1 to unsigned 32 bit int, as just 1 is treated as int which cannot be shifted left more than 30 times
weight += 1;
}
return weight;
}

View File

@ -1,10 +0,0 @@
int hammingDistance(int x, int y){
int difference = x ^ y;
int TotalBits = sizeof(difference)*8;
int i, distance = 0;
for(i = 0; i < TotalBits; i++) {
if(difference & (UINT32_C(1) << i))
distance += 1;
}
return distance;
}