mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge pull request #357 from hrishikeshSuresh/problem_121
added solution & modified README.md for problem 121
This commit is contained in:
commit
c069038311
@ -24,6 +24,7 @@ LeetCode
|
|||||||
|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [C](./src/109.c)|Medium|
|
|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [C](./src/109.c)|Medium|
|
||||||
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [C](./src/110.c)|Easy|
|
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [C](./src/110.c)|Easy|
|
||||||
|112|[Path Sum](https://leetcode.com/problems/path-sum/) | [C](./src/112.c)|Easy|
|
|112|[Path Sum](https://leetcode.com/problems/path-sum/) | [C](./src/112.c)|Easy|
|
||||||
|
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [C](./src/121.c)|Easy|
|
||||||
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [C](./src/125.c)|Easy|
|
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [C](./src/125.c)|Easy|
|
||||||
|136|[Single Number](https://leetcode.com/problems/single-number/) | [C](./src/136.c)|Easy|
|
|136|[Single Number](https://leetcode.com/problems/single-number/) | [C](./src/136.c)|Easy|
|
||||||
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [C](./src/141.c)|Easy|
|
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [C](./src/141.c)|Easy|
|
||||||
@ -50,6 +51,7 @@ LeetCode
|
|||||||
|404|[Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [C](./src/404.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|
|
|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|
|
|461|[Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C](./src/461.c) |Easy|
|
||||||
|
|476|[Number Complement](https://leetcode.com/problems/number-complement/) | [C](./src/476.c)|Easy|
|
||||||
|509|[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [C](./src/509.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|
|
|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|
|
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c)|Easy|
|
||||||
@ -71,4 +73,4 @@ LeetCode
|
|||||||
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [C](./src/1089.c)|Easy|
|
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [C](./src/1089.c)|Easy|
|
||||||
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c)|Easy|
|
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c)|Easy|
|
||||||
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy|
|
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy|
|
||||||
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c)|Easy|
|
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c)|Easy|
|
17
leetcode/src/121.c
Normal file
17
leetcode/src/121.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
int maxcmp(int a, int b) {
|
||||||
|
return (a >= b)? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* max subarray problem by using Kadane's Algorithm
|
||||||
|
*/
|
||||||
|
int maxProfit(int* prices, int pricesSize){
|
||||||
|
/* maxCur: current maximum
|
||||||
|
* maxSoFar: found maximum for subarray so far
|
||||||
|
*/
|
||||||
|
int maxCur = 0, maxSoFar = 0;
|
||||||
|
for(int i = 1; i < pricesSize; i++) {
|
||||||
|
maxCur = maxcmp(0, maxCur + prices[i] - prices[i - 1]);
|
||||||
|
maxSoFar = maxcmp(maxSoFar, maxCur);
|
||||||
|
}
|
||||||
|
return maxSoFar;
|
||||||
|
}
|
@ -11,3 +11,15 @@ int searchInsert(int* nums, int numsSize, int target){
|
|||||||
}
|
}
|
||||||
return low;
|
return low;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Recursive version */
|
||||||
|
int searchInsert(int* nums, int numsSize, int target){
|
||||||
|
int idx = numsSize - 1;
|
||||||
|
if(numsSize>0){
|
||||||
|
if (target > nums[idx]){
|
||||||
|
return numsSize;
|
||||||
|
}
|
||||||
|
return searchInsert(nums, numsSize - 1, target);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
15
leetcode/src/476.c
Normal file
15
leetcode/src/476.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
int findComplement(int num) {
|
||||||
|
int TotalBits = 0;
|
||||||
|
int temp = num;
|
||||||
|
while(temp) { //To find position of MSB in given num. Since num is represented as a standard size in memory, we cannot rely on size
|
||||||
|
//for that information.
|
||||||
|
TotalBits++; //increment TotalBits till temp becomes 0
|
||||||
|
temp >>= 1; //shift temp right by 1 bit every iteration; temp loses 1 bit to underflow every iteration till it becomes 0
|
||||||
|
}
|
||||||
|
int i, flipNumber = 1; //Eg: 1's complement of 101(binary) can be found as 101^111 (XOR with 111 flips all bits that are 1 to 0 and flips 0 to 1)
|
||||||
|
for(i = 1; i < TotalBits; i++) {
|
||||||
|
flipNumber += UINT32_C(1) << i; //Note the use of unsigned int to facilitate left shift more than 31 times, if needed
|
||||||
|
}
|
||||||
|
num = num^flipNumber;
|
||||||
|
return num;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user