mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Add some solution leetcode
This commit is contained in:
parent
f9139347fe
commit
031ae5f1d2
@ -63,4 +63,7 @@ LeetCode
|
||||
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [C](./src/938.c)|Easy|
|
||||
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [C](./src/965.c)|Easy|
|
||||
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C](./src/977.c)|Easy|
|
||||
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.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|
|
||||
|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|
|
||||
|
20
leetcode/src/1089.c
Normal file
20
leetcode/src/1089.c
Normal file
@ -0,0 +1,20 @@
|
||||
void duplicateZeros(int* arr, int arrSize){
|
||||
int i, start = 0;
|
||||
int *tmp = malloc(arrSize * sizeof(int));
|
||||
/* Copy arr into tmp arr */
|
||||
for(i = 0; i < arrSize; i++) {
|
||||
tmp[i] = arr[i];
|
||||
}
|
||||
i = 0;
|
||||
for(start = 0; start < arrSize; start++) {
|
||||
arr[start] = tmp[i];
|
||||
if(tmp[i] == 0) {
|
||||
start++;
|
||||
if (start < arrSize)
|
||||
arr[start] = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
16
leetcode/src/1184.c
Normal file
16
leetcode/src/1184.c
Normal file
@ -0,0 +1,16 @@
|
||||
int distanceBetweenBusStops(int* distance, int distanceSize, int start, int destination){
|
||||
|
||||
int sum1 = 0, sum2 = 0;
|
||||
if (start > destination) {
|
||||
int tmp = start;
|
||||
start = destination;
|
||||
destination = tmp;
|
||||
}
|
||||
for (auto i = 0; i < distanceSize; ++i) {
|
||||
if (i >= start && i < destination)
|
||||
sum1 += distance[i];
|
||||
else
|
||||
sum2 += distance[i];
|
||||
}
|
||||
return sum1 < sum2 ? sum1 : sum2;
|
||||
}
|
25
leetcode/src/1207.c
Normal file
25
leetcode/src/1207.c
Normal file
@ -0,0 +1,25 @@
|
||||
#define MAP_SIZE 2048
|
||||
|
||||
int cmpvalue(const void *a, const void *b) {
|
||||
return *(int *)b - *(int *)a;
|
||||
}
|
||||
bool uniqueOccurrences(int* arr, int arrSize){
|
||||
int *map = calloc(MAP_SIZE, sizeof(int));
|
||||
int i;
|
||||
for(i = 0; i < arrSize; i++) {
|
||||
if (arr[i] < 0)
|
||||
map[arr[i] + MAP_SIZE/2] += 1;
|
||||
else
|
||||
map[arr[i]] += 1;
|
||||
}
|
||||
/* number of occurrences is sorted by decreasing order
|
||||
Ex: 3 2 1 0 0 0 0 */
|
||||
qsort(map, MAP_SIZE, sizeof(int), cmpvalue);
|
||||
i = 0;
|
||||
while(map[i]) {
|
||||
if(map[i] == map[i+1])
|
||||
return 0;
|
||||
i++;
|
||||
}
|
||||
return 1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user