mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #685 from KingArthur0205/remote
添加 0134.加油站.md C语言版本、添加 0134.加油站.md C语言版本注释、以及 添加 0452.用最少数量的箭引爆气球.md C语言版本
This commit is contained in:
@ -283,6 +283,35 @@ var canCompleteCircuit = function(gas, cost) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
C:
|
||||||
|
```c
|
||||||
|
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
|
||||||
|
int curSum = 0;
|
||||||
|
int i;
|
||||||
|
int min = INT_MAX;
|
||||||
|
//遍历整个数组。计算出每站的用油差。并将其与最小累加量比较
|
||||||
|
for(i = 0; i < gasSize; i++) {
|
||||||
|
int diff = gas[i] - cost[i];
|
||||||
|
curSum += diff;
|
||||||
|
if(curSum < min)
|
||||||
|
min = curSum;
|
||||||
|
}
|
||||||
|
//若汽油总数为负数,代表无法跑完一环。返回-1
|
||||||
|
if(curSum < 0)
|
||||||
|
return -1;
|
||||||
|
//若min大于等于0,说明每一天加油量比用油量多。因此从0出发即可
|
||||||
|
if(min >= 0)
|
||||||
|
return 0;
|
||||||
|
//若累加最小值为负,则找到一个非零元素(加油量大于出油量)出发。返回坐标
|
||||||
|
for(i = gasSize - 1; i >= 0; i--) {
|
||||||
|
min+=(gas[i]-cost[i]);
|
||||||
|
if(min >= 0)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
//逻辑上不会返回这个0
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
@ -218,6 +218,30 @@ var findMinArrowShots = function(points) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
C:
|
||||||
|
```c
|
||||||
|
int cmp(const void *a,const void *b)
|
||||||
|
{
|
||||||
|
return ((*((int**)a))[0] > (*((int**)b))[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int findMinArrowShots(int** points, int pointsSize, int* pointsColSize){
|
||||||
|
//将points数组作升序排序
|
||||||
|
qsort(points, pointsSize, sizeof(points[0]),cmp);
|
||||||
|
|
||||||
|
int arrowNum = 1;
|
||||||
|
int i = 1;
|
||||||
|
for(i = 1; i < pointsSize; i++) {
|
||||||
|
//若前一个气球与当前气球不重叠,证明需要增加箭的数量
|
||||||
|
if(points[i][0] > points[i-1][1])
|
||||||
|
arrowNum++;
|
||||||
|
else
|
||||||
|
//若前一个气球与当前气球重叠,判断并更新最小的x_end
|
||||||
|
points[i][1] = points[i][1] > points[i-1][1] ? points[i-1][1] : points[i][1];
|
||||||
|
}
|
||||||
|
return arrowNum;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user