mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
@ -464,6 +464,27 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```csharp
|
||||||
|
// 版本二
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int Jump(int[] nums)
|
||||||
|
{
|
||||||
|
int cur = 0, next = 0, step = 0;
|
||||||
|
for (int i = 0; i < nums.Length - 1; i++)
|
||||||
|
{
|
||||||
|
next = Math.Max(next, i + nums[i]);
|
||||||
|
if (i == cur)
|
||||||
|
{
|
||||||
|
cur = next;
|
||||||
|
step++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return step;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -258,6 +258,23 @@ object Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public bool CanJump(int[] nums)
|
||||||
|
{
|
||||||
|
int cover = 0;
|
||||||
|
if (nums.Length == 1) return true;
|
||||||
|
for (int i = 0; i <= cover; i++)
|
||||||
|
{
|
||||||
|
cover = Math.Max(i + nums[i], cover);
|
||||||
|
if (cover >= nums.Length - 1) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -406,6 +406,21 @@ object Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int MaxProfit(int[] prices)
|
||||||
|
{
|
||||||
|
int res = 0;
|
||||||
|
for (int i = 0; i < prices.Length - 1; i++)
|
||||||
|
{
|
||||||
|
res += Math.Max(0, prices[i + 1] - prices[i]);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -630,6 +630,29 @@ object Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```csharp
|
||||||
|
// 贪心算法,方法二
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int CanCompleteCircuit(int[] gas, int[] cost)
|
||||||
|
{
|
||||||
|
int curSum = 0, totalSum = 0, start = 0;
|
||||||
|
for (int i = 0; i < gas.Length; i++)
|
||||||
|
{
|
||||||
|
curSum += gas[i] - cost[i];
|
||||||
|
totalSum += gas[i] - cost[i];
|
||||||
|
if (curSum < 0)
|
||||||
|
{
|
||||||
|
start = i + 1;
|
||||||
|
curSum = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (totalSum < 0) return -1;
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
@ -370,6 +370,35 @@ object Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int Candy(int[] ratings)
|
||||||
|
{
|
||||||
|
int[] candies = new int[ratings.Length];
|
||||||
|
for (int i = 0; i < candies.Length; i++)
|
||||||
|
{
|
||||||
|
candies[i] = 1;
|
||||||
|
}
|
||||||
|
for (int i = 1; i < ratings.Length; i++)
|
||||||
|
{
|
||||||
|
if (ratings[i] > ratings[i - 1])
|
||||||
|
{
|
||||||
|
candies[i] = candies[i - 1] + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = ratings.Length - 2; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (ratings[i] > ratings[i + 1])
|
||||||
|
{
|
||||||
|
candies[i] = Math.Max(candies[i], candies[i + 1] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return candies.Sum();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
@ -397,6 +397,46 @@ object Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
### C#
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public bool LemonadeChange(int[] bills)
|
||||||
|
{
|
||||||
|
int five = 0, ten = 0, twenty = 0;
|
||||||
|
foreach (var bill in bills)
|
||||||
|
{
|
||||||
|
if (bill == 5) five++;
|
||||||
|
if (bill == 10)
|
||||||
|
{
|
||||||
|
if (five == 0) return false;
|
||||||
|
five--;
|
||||||
|
ten++;
|
||||||
|
}
|
||||||
|
if (bill == 20)
|
||||||
|
{
|
||||||
|
if (ten > 0 && five > 0)
|
||||||
|
{
|
||||||
|
ten--;
|
||||||
|
five--;
|
||||||
|
twenty++;
|
||||||
|
}
|
||||||
|
else if (five >= 3)
|
||||||
|
{
|
||||||
|
five -= 3;
|
||||||
|
twenty++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
@ -322,6 +322,29 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### C#
|
||||||
|
```csharp
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public int LargestSumAfterKNegations(int[] nums, int k)
|
||||||
|
{
|
||||||
|
int res = 0;
|
||||||
|
Array.Sort(nums, (a, b) => Math.Abs(b) - Math.Abs(a));
|
||||||
|
for (int i = 0; i < nums.Length; i++)
|
||||||
|
{
|
||||||
|
if (nums[i] < 0 && k > 0)
|
||||||
|
{
|
||||||
|
nums[i] *= -1;
|
||||||
|
k--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (k % 2 == 1) nums[nums.Length - 1] *= -1;
|
||||||
|
foreach (var item in nums) res += item;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
|
Reference in New Issue
Block a user