Skip to content

Commit 7e43580

Browse files
authored
Update articles (#5566)
* Update articles * update articles
1 parent ade2cc1 commit 7e43580

4 files changed

Lines changed: 173 additions & 6 deletions

File tree

articles/height-checker.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ class Solution {
9696
}
9797
```
9898

99+
```csharp
100+
public class Solution {
101+
public int HeightChecker(int[] heights) {
102+
int[] expected = new int[heights.Length];
103+
Array.Copy(heights, expected, heights.Length);
104+
Array.Sort(expected);
105+
106+
int res = 0;
107+
for (int i = 0; i < heights.Length; i++) {
108+
if (heights[i] != expected[i]) {
109+
res++;
110+
}
111+
}
112+
113+
return res;
114+
}
115+
}
116+
```
117+
99118
```go
100119
func heightChecker(heights []int) int {
101120
expected := make([]int, len(heights))
@@ -303,6 +322,34 @@ class Solution {
303322
}
304323
```
305324

325+
```csharp
326+
public class Solution {
327+
public int HeightChecker(int[] heights) {
328+
int[] count = new int[101];
329+
foreach (int h in heights) {
330+
count[h]++;
331+
}
332+
333+
List<int> expected = new List<int>();
334+
for (int h = 1; h <= 100; h++) {
335+
int c = count[h];
336+
for (int i = 0; i < c; i++) {
337+
expected.Add(h);
338+
}
339+
}
340+
341+
int res = 0;
342+
for (int i = 0; i < heights.Length; i++) {
343+
if (heights[i] != expected[i]) {
344+
res++;
345+
}
346+
}
347+
348+
return res;
349+
}
350+
}
351+
```
352+
306353
```go
307354
func heightChecker(heights []int) int {
308355
count := make([]int, 101)

articles/maximum-twin-sum-of-a-linked-list.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,43 @@ class Solution {
154154
}
155155
```
156156

157+
```csharp
158+
/**
159+
* Definition for singly-linked list.
160+
* public class ListNode {
161+
* public int val;
162+
* public ListNode next;
163+
* public ListNode(int val=0, ListNode next=null) {
164+
* this.val = val;
165+
* this.next = next;
166+
* }
167+
* }
168+
*/
169+
170+
public class Solution {
171+
public int PairSum(ListNode head) {
172+
List<int> arr = new List<int>();
173+
ListNode cur = head;
174+
175+
while (cur != null) {
176+
arr.Add(cur.val);
177+
cur = cur.next;
178+
}
179+
180+
int i = 0, j = arr.Count - 1;
181+
int res = 0;
182+
183+
while (i < j) {
184+
res = Math.Max(res, arr[i] + arr[j]);
185+
i++;
186+
j--;
187+
}
188+
189+
return res;
190+
}
191+
}
192+
```
193+
157194
```go
158195
/**
159196
* Definition for singly-linked list.
@@ -466,6 +503,50 @@ class Solution {
466503
}
467504
```
468505

506+
```csharp
507+
/**
508+
* Definition for singly-linked list.
509+
* public class ListNode {
510+
* public int val;
511+
* public ListNode next;
512+
* public ListNode(int val=0, ListNode next=null) {
513+
* this.val = val;
514+
* this.next = next;
515+
* }
516+
* }
517+
*/
518+
519+
public class Solution {
520+
public int PairSum(ListNode head) {
521+
ListNode slow = head, fast = head;
522+
523+
while (fast != null && fast.next != null) {
524+
slow = slow.next;
525+
fast = fast.next.next;
526+
}
527+
528+
ListNode prev = null, cur = slow;
529+
while (cur != null) {
530+
ListNode nxt = cur.next;
531+
cur.next = prev;
532+
prev = cur;
533+
cur = nxt;
534+
}
535+
536+
int res = 0;
537+
ListNode first = head, second = prev;
538+
539+
while (second != null) {
540+
res = Math.Max(res, first.val + second.val);
541+
first = first.next;
542+
second = second.next;
543+
}
544+
545+
return res;
546+
}
547+
}
548+
```
549+
469550
```go
470551
/**
471552
* Definition for singly-linked list.
@@ -782,6 +863,45 @@ class Solution {
782863
}
783864
```
784865

866+
```csharp
867+
/**
868+
* Definition for singly-linked list.
869+
* public class ListNode {
870+
* public int val;
871+
* public ListNode next;
872+
* public ListNode(int val=0, ListNode next=null) {
873+
* this.val = val;
874+
* this.next = next;
875+
* }
876+
* }
877+
*/
878+
879+
public class Solution {
880+
public int PairSum(ListNode head) {
881+
ListNode slow = head, fast = head;
882+
ListNode prev = null;
883+
884+
while (fast != null && fast.next != null) {
885+
fast = fast.next.next;
886+
ListNode tmp = slow.next;
887+
slow.next = prev;
888+
prev = slow;
889+
slow = tmp;
890+
}
891+
892+
int res = 0;
893+
894+
while (slow != null) {
895+
res = Math.Max(res, prev.val + slow.val);
896+
prev = prev.next;
897+
slow = slow.next;
898+
}
899+
900+
return res;
901+
}
902+
}
903+
```
904+
785905
```go
786906
/**
787907
* Definition for singly-linked list.

articles/pascals-triangle-ii.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Solution {
103103

104104
```csharp
105105
public class Solution {
106-
public IList<int> GetRow(int rowIndex) {
106+
public List<int> GetRow(int rowIndex) {
107107
if (rowIndex == 0) return new List<int> { 1 };
108108

109109
var curRow = new List<int> { 1 };
@@ -284,7 +284,7 @@ class Solution {
284284

285285
```csharp
286286
public class Solution {
287-
public IList<int> GetRow(int rowIndex) {
287+
public List<int> GetRow(int rowIndex) {
288288
var res = new List<int[]>();
289289
for (int i = 0; i <= rowIndex; i++) {
290290
int[] row = new int[i + 1];
@@ -462,7 +462,7 @@ class Solution {
462462

463463
```csharp
464464
public class Solution {
465-
public IList<int> GetRow(int rowIndex) {
465+
public List<int> GetRow(int rowIndex) {
466466
var res = new List<int> { 1 };
467467
for (int i = 0; i < rowIndex; i++) {
468468
var nextRow = new List<int>(new int[res.Count + 1]);
@@ -627,7 +627,7 @@ class Solution {
627627

628628
```csharp
629629
public class Solution {
630-
public IList<int> GetRow(int rowIndex) {
630+
public List<int> GetRow(int rowIndex) {
631631
var row = new List<int>(new int[rowIndex + 1]);
632632
for (int i = 0; i <= rowIndex; i++) row[i] = 1;
633633
for (int i = 1; i < rowIndex; i++) {
@@ -778,7 +778,7 @@ class Solution {
778778

779779
```csharp
780780
public class Solution {
781-
public IList<int> GetRow(int rowIndex) {
781+
public List<int> GetRow(int rowIndex) {
782782
var row = new List<int> { 1 };
783783
for (int i = 1; i <= rowIndex; i++) {
784784
row.Add((int)((long)row[row.Count - 1] * (rowIndex - i + 1) / i));

articles/products-of-array-discluding-self.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ So we first check how many zeros the array has:
248248
- The index with zero gets the `prod` of all non-zero numbers.
249249
- All other positions get `0`.
250250
- If there are no zeros:
251-
- Set each result value to `prod / nums[i]`.
251+
- Set each result value to `prod // nums[i]`.
252252
5. Return the result array.
253253

254254
::tabs-start

0 commit comments

Comments
 (0)