Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6f46b26

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents e5c4e9e + 1fcebdf commit 6f46b26

File tree

50 files changed

+1525
-231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1525
-231
lines changed

‎problems/0017.电话号码的字母组合.md‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class Solution {
260260

261261
}
262262

263-
//每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuild
263+
//每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuilder
264264
StringBuilder temp = new StringBuilder();
265265

266266
//比如digits如果为"23",num 为0,则str表示2对应的 abc
@@ -732,6 +732,38 @@ def backtracking(result, letter_map, digits, path, index)
732732
end
733733
end
734734
```
735+
### C#
736+
```C#
737+
public class Solution
738+
{
739+
public IList<string> res = new List<string>();
740+
public string s;
741+
public string[] letterMap = new string[10] { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
742+
public IList<string> LetterCombinations(string digits)
743+
{
744+
if (digits.Length == 0)
745+
return res;
746+
BackTracking(digits, 0);
747+
return res;
748+
}
749+
public void BackTracking(string digits, int index)
750+
{
751+
if (index == digits.Length)
752+
{
753+
res.Add(s);
754+
return;
755+
}
756+
int digit = digits[index] - '0';
757+
string letters = letterMap[digit];
758+
for (int i = 0; i < letters.Length; i++)
759+
{
760+
s += letters[i];
761+
BackTracking(digits, index + 1);
762+
s = s.Substring(0, s.Length - 1);
763+
}
764+
}
765+
}
766+
```
735767

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

‎problems/0019.删除链表的倒数第N个节点.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public ListNode removeNthFromEnd(ListNode head, int n){
106106
ListNode slowIndex = dummyNode;
107107
108108
// 只要快慢指针相差 n 个结点即可
109-
for (int i = 0; i <= n ; i++){
109+
for (int i = 0; i < n ; i++){
110110
fastIndex = fastIndex.next;
111111
}
112112

‎problems/0020.有效的括号.md‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,23 +218,41 @@ class Solution:
218218
### Go:
219219

220220
```Go
221+
// 思路: 使用栈来进行括号的匹配
222+
// 时间复杂度 O(n)
223+
// 空间复杂度 O(n)
221224
func isValid(s string) bool {
222-
hash := map[byte]byte{')':'(', ']':'[', '}':'{'}
223-
stack := make([]byte, 0)
224-
if s == "" {
225-
return true
226-
}
227-
228-
for i := 0; i < len(s); i++ {
229-
if s[i] == '(' || s[i] == '[' || s[i] == '{' {
230-
stack = append(stack, s[i])
231-
} else if len(stack) > 0 && stack[len(stack)-1] == hash[s[i]] {
232-
stack = stack[:len(stack)-1]
233-
} else {
234-
return false
235-
}
236-
}
237-
return len(stack) == 0
225+
// 使用切片模拟栈的行为
226+
stack := make([]rune, 0)
227+
228+
// m 用于记录某个右括号对应的左括号
229+
m := make(map[rune]rune)
230+
m[')'] = '('
231+
m[']'] = '['
232+
m['}'] = '{'
233+
234+
// 遍历字符串中的 rune
235+
for _, c := range s {
236+
// 左括号直接入栈
237+
if c == '(' || c == '[' || c == '{' {
238+
stack = append(stack, c)
239+
} else {
240+
// 如果是右括号,先判断栈内是否还有元素
241+
if len(stack) == 0 {
242+
return false
243+
}
244+
// 再判断栈顶元素是否能够匹配
245+
peek := stack[len(stack)-1]
246+
if peek != m[c] {
247+
return false
248+
}
249+
// 模拟栈顶弹出
250+
stack = stack[:len(stack)-1]
251+
}
252+
}
253+
254+
// 若栈中不再包含元素,则能完全匹配
255+
return len(stack) == 0
238256
}
239257
```
240258

‎problems/0024.两两交换链表中的节点.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public:
6363

6464
cur = cur->next->next; // cur移动两位,准备下一轮交换
6565
}
66-
return dummyHead->next;
66+
ListNode* result = dummyHead->next;
67+
delete dummyHead;
68+
return result;
6769
}
6870
};
6971
```

‎problems/0027.移除元素.md‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,24 @@ class Solution:
256256
### Go:
257257

258258
```go
259+
// 快慢指针法
260+
// 时间复杂度 O(n)
261+
// 空间复杂度 O(1)
259262
func removeElement(nums []int, val int) int {
260-
length:=len(nums)
261-
res:=0
262-
for i:=0;i<length;i++{
263-
if nums[i]!=val {
264-
nums[res]=nums[i]
265-
res++
266-
}
267-
}
268-
nums=nums[:res]
269-
return res
263+
// 初始化慢指针 slow
264+
slow := 0
265+
// 通过 for 循环移动快指针 fast
266+
// 当 fast 指向的元素等于 val 时,跳过
267+
// 否则,将该元素写入 slow 指向的位置,并将 slow 后移一位
268+
for fast := 0; fast < len(nums); fast++ {
269+
if nums[fast] == val {
270+
continue
271+
}
272+
nums[slow] = nums[fast]
273+
slow++
274+
}
275+
276+
return slow
270277
}
271278
```
272279
```go

‎problems/0037.解数独.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,59 @@ object Solution {
756756
}
757757
}
758758
```
759+
### C#
760+
```csharp
761+
public class Solution
762+
{
763+
public void SolveSudoku(char[][] board)
764+
{
765+
BackTracking(board);
766+
}
767+
public bool BackTracking(char[][] board)
768+
{
769+
for (int i = 0; i < board.Length; i++)
770+
{
771+
for (int j = 0; j < board[0].Length; j++)
772+
{
773+
if (board[i][j] != '.') continue;
774+
for (char k = '1'; k <= '9'; k++)
775+
{
776+
if (IsValid(board, i, j, k))
777+
{
778+
board[i][j] = k;
779+
if (BackTracking(board)) return true;
780+
board[i][j] = '.';
781+
}
782+
}
783+
return false;
784+
}
785+
786+
}
787+
return true;
788+
}
789+
public bool IsValid(char[][] board, int row, int col, char val)
790+
{
791+
for (int i = 0; i < 9; i++)
792+
{
793+
if (board[i][col] == val) return false;
794+
}
795+
for (int i = 0; i < 9; i++)
796+
{
797+
if (board[row][i] == val) return false;
798+
}
799+
int startRow = (row / 3) * 3;
800+
int startCol = (col / 3) * 3;
801+
for (int i = startRow; i < startRow + 3; i++)
802+
{
803+
for (int j = startCol; j < startCol + 3; j++)
804+
{
805+
if (board[i][j] == val) return false;
806+
}
807+
}
808+
return true;
809+
}
810+
}
811+
```
759812

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

‎problems/0039.组合总和.md‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,66 @@ object Solution {
598598
}
599599
}
600600
```
601+
### C#
602+
```csharp
603+
public class Solution
604+
{
605+
public IList<IList<int>> res = new List<IList<int>>();
606+
public IList<int> path = new List<int>();
607+
public IList<IList<int>> CombinationSum(int[] candidates, int target)
608+
{
609+
BackTracking(candidates, target, 0, 0);
610+
return res;
611+
}
612+
public void BackTracking(int[] candidates, int target, int start, int sum)
613+
{
614+
if (sum > target) return;
615+
if (sum == target)
616+
{
617+
res.Add(new List<int>(path));
618+
return;
619+
}
620+
for (int i = start; i < candidates.Length; i++)
621+
{
622+
sum += candidates[i];
623+
path.Add(candidates[i]);
624+
BackTracking(candidates, target, i, sum);
625+
sum -= candidates[i];
626+
path.RemoveAt(path.Count - 1);
627+
}
628+
}
629+
}
630+
631+
// 剪枝优化
632+
public class Solution
633+
{
634+
public IList<IList<int>> res = new List<IList<int>>();
635+
public IList<int> path = new List<int>();
636+
public IList<IList<int>> CombinationSum(int[] candidates, int target)
637+
{
638+
Array.Sort(candidates);
639+
BackTracking(candidates, target, 0, 0);
640+
return res;
641+
}
642+
public void BackTracking(int[] candidates, int target, int start, int sum)
643+
{
644+
if (sum > target) return;
645+
if (sum == target)
646+
{
647+
res.Add(new List<int>(path));
648+
return;
649+
}
650+
for (int i = start; i < candidates.Length && sum + candidates[i] <= target; i++)
651+
{
652+
sum += candidates[i];
653+
path.Add(candidates[i]);
654+
BackTracking(candidates, target, i, sum);
655+
sum -= candidates[i];
656+
path.RemoveAt(path.Count - 1);
657+
}
658+
}
659+
}
660+
```
601661

602662

603663
<p align="center">

‎problems/0040.组合总和II.md‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,39 @@ object Solution {
773773
}
774774
}
775775
```
776-
776+
### C#
777+
```csharp
778+
public class Solution
779+
{
780+
public List<IList<int>> res = new List<IList<int>>();
781+
public List<int> path = new List<int>();
782+
public IList<IList<int>> CombinationSum2(int[] candidates, int target)
783+
{
784+
785+
Array.Sort(candidates);
786+
BackTracking(candidates, target, 0, 0);
787+
return res;
788+
}
789+
public void BackTracking(int[] candidates, int target, int start, int sum)
790+
{
791+
if (sum > target) return;
792+
if (sum == target)
793+
{
794+
res.Add(new List<int>(path));
795+
return;
796+
}
797+
for (int i = start; i < candidates.Length && sum + candidates[i] <= target; i++)
798+
{
799+
if (i > start && candidates[i] == candidates[i - 1]) continue;
800+
sum += candidates[i];
801+
path.Add(candidates[i]);
802+
BackTracking(candidates, target, i + 1, sum);
803+
sum -= candidates[i];
804+
path.RemoveAt(path.Count - 1);
805+
}
806+
}
807+
}
808+
```
777809
<p align="center">
778810
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
779811
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

‎problems/0046.全排列.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,37 @@ object Solution {
486486
}
487487
}
488488
```
489+
### C#
490+
```csharp
491+
public class Solution
492+
{
493+
public IList<IList<int>> res = new List<IList<int>>();
494+
public IList<int> path = new List<int>();
495+
public IList<IList<int>> Permute(int[] nums)
496+
{
497+
var used = new bool[nums.Length];
498+
BackTracking(nums, used);
499+
return res;
500+
}
501+
public void BackTracking(int[] nums, bool[] used)
502+
{
503+
if (path.Count == nums.Length)
504+
{
505+
res.Add(new List<int>(path));
506+
return;
507+
}
508+
for (int i = 0; i < nums.Length; i++)
509+
{
510+
if (used[i]) continue;
511+
used[i] = true;
512+
path.Add(nums[i]);
513+
BackTracking(nums, used);
514+
used[i] = false;
515+
path.RemoveAt(path.Count - 1);
516+
}
517+
}
518+
}
519+
```
489520

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /