Skip to content

Commit 4b0226c

Browse files
Merge pull request youngyangyang04#2389 from eeee0717/master
Update.买卖股票的最佳时机2,添加C#
2 parents 6f46b26 + 499d2af commit 4b0226c

7 files changed

+168
-0
lines changed

problems/0045.跳跃游戏II.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,27 @@ impl Solution {
464464
}
465465
}
466466
```
467+
### C#
468+
```csharp
469+
// 版本二
470+
public class Solution
471+
{
472+
public int Jump(int[] nums)
473+
{
474+
int cur = 0, next = 0, step = 0;
475+
for (int i = 0; i < nums.Length - 1; i++)
476+
{
477+
next = Math.Max(next, i + nums[i]);
478+
if (i == cur)
479+
{
480+
cur = next;
481+
step++;
482+
}
483+
}
484+
return step;
485+
}
486+
}
487+
```
467488

468489
<p align="center">
469490
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0055.跳跃游戏.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,23 @@ object Solution {
258258
}
259259
}
260260
```
261+
### C#
262+
```csharp
263+
public class Solution
264+
{
265+
public bool CanJump(int[] nums)
266+
{
267+
int cover = 0;
268+
if (nums.Length == 1) return true;
269+
for (int i = 0; i <= cover; i++)
270+
{
271+
cover = Math.Max(i + nums[i], cover);
272+
if (cover >= nums.Length - 1) return true;
273+
}
274+
return false;
275+
}
276+
}
277+
```
261278

262279
<p align="center">
263280
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0122.买卖股票的最佳时机II.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,21 @@ object Solution {
406406
}
407407
}
408408
```
409+
### C#
410+
```csharp
411+
public class Solution
412+
{
413+
public int MaxProfit(int[] prices)
414+
{
415+
int res = 0;
416+
for (int i = 0; i < prices.Length - 1; i++)
417+
{
418+
res += Math.Max(0, prices[i + 1] - prices[i]);
419+
}
420+
return res;
421+
}
422+
}
423+
```
409424

410425
<p align="center">
411426
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0134.加油站.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,29 @@ object Solution {
630630
}
631631
}
632632
```
633+
### C#
634+
```csharp
635+
// 贪心算法,方法二
636+
public class Solution
637+
{
638+
public int CanCompleteCircuit(int[] gas, int[] cost)
639+
{
640+
int curSum = 0, totalSum = 0, start = 0;
641+
for (int i = 0; i < gas.Length; i++)
642+
{
643+
curSum += gas[i] - cost[i];
644+
totalSum += gas[i] - cost[i];
645+
if (curSum < 0)
646+
{
647+
start = i + 1;
648+
curSum = 0;
649+
}
650+
}
651+
if (totalSum < 0) return -1;
652+
return start;
653+
}
654+
}
655+
```
633656

634657
<p align="center">
635658
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

problems/0135.分发糖果.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,35 @@ object Solution {
370370
}
371371
}
372372
```
373+
### C#
374+
```csharp
375+
public class Solution
376+
{
377+
public int Candy(int[] ratings)
378+
{
379+
int[] candies = new int[ratings.Length];
380+
for (int i = 0; i < candies.Length; i++)
381+
{
382+
candies[i] = 1;
383+
}
384+
for (int i = 1; i < ratings.Length; i++)
385+
{
386+
if (ratings[i] > ratings[i - 1])
387+
{
388+
candies[i] = candies[i - 1] + 1;
389+
}
390+
}
391+
for (int i = ratings.Length - 2; i >= 0; i--)
392+
{
393+
if (ratings[i] > ratings[i + 1])
394+
{
395+
candies[i] = Math.Max(candies[i], candies[i + 1] + 1);
396+
}
397+
}
398+
return candies.Sum();
399+
}
400+
}
401+
```
373402

374403

375404
<p align="center">

problems/0860.柠檬水找零.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,46 @@ object Solution {
397397
}
398398
}
399399
```
400+
### C#
401+
```csharp
402+
public class Solution
403+
{
404+
public bool LemonadeChange(int[] bills)
405+
{
406+
int five = 0, ten = 0, twenty = 0;
407+
foreach (var bill in bills)
408+
{
409+
if (bill == 5) five++;
410+
if (bill == 10)
411+
{
412+
if (five == 0) return false;
413+
five--;
414+
ten++;
415+
}
416+
if (bill == 20)
417+
{
418+
if (ten > 0 && five > 0)
419+
{
420+
ten--;
421+
five--;
422+
twenty++;
423+
}
424+
else if (five >= 3)
425+
{
426+
five -= 3;
427+
twenty++;
428+
}
429+
else
430+
{
431+
return false;
432+
}
433+
434+
}
435+
}
436+
return true;
437+
}
438+
}
439+
```
400440

401441

402442
<p align="center">

problems/1005.K次取反后最大化的数组和.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,29 @@ object Solution {
322322
}
323323
```
324324

325+
### C#
326+
```csharp
327+
public class Solution
328+
{
329+
public int LargestSumAfterKNegations(int[] nums, int k)
330+
{
331+
int res = 0;
332+
Array.Sort(nums, (a, b) => Math.Abs(b) - Math.Abs(a));
333+
for (int i = 0; i < nums.Length; i++)
334+
{
335+
if (nums[i] < 0 && k > 0)
336+
{
337+
nums[i] *= -1;
338+
k--;
339+
}
340+
}
341+
if (k % 2 == 1) nums[nums.Length - 1] *= -1;
342+
foreach (var item in nums) res += item;
343+
return res;
344+
}
345+
}
346+
```
347+
325348

326349

327350
<p align="center">

0 commit comments

Comments
 (0)