From 99b8b5744ef3aadb97da6ea9049ed2c1f53dbdc5 Mon Sep 17 00:00:00 2001 From: "Farmer.Chillax" <48387781+farmerchillax@users.noreply.github.com> Date: 2023年10月31日 23:02:44 +0800 Subject: [PATCH 1/7] =?UTF-8?q?Update=200242.=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15345円274円202円344円275円215円350円257円215円.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git "a/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" "b/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" index f47d8b05b6..6eed90a73a 100644 --- "a/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" +++ "b/problems/0242.346円234円211円346円225円210円347円232円204円345円255円227円346円257円215円345円274円202円344円275円215円350円257円215円.md" @@ -181,6 +181,31 @@ func isAnagram(s string, t string) bool { } ``` +Go 写法二(只对字符串遍历一次) +```go +func isAnagram(s string, t string) bool { + if len(s) != len(t) { + return false + } + records := [26]int{} + for index := 0; index < len(s); index++ { + if s[index] == t[index] { + continue + } + sCharIndex := s[index] - 'a' + records[sCharIndex]++ + tCharIndex := t[index] - 'a' + records[tCharIndex]-- + } + for _, record := range records { + if record != 0 { + return false + } + } + return true +} +``` + ### JavaScript: ```js From 559434172cacdda8f1cb324bd161eab21bf58bfe Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Mon, 6 Nov 2023 10:10:36 +0800 Subject: [PATCH 2/7] =?UTF-8?q?Update=20028.=E5=AE=9E=E7=8E=B0strStr?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0028.345円256円236円347円216円260円strStr.md" | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git "a/problems/0028.345円256円236円347円216円260円strStr.md" "b/problems/0028.345円256円236円347円216円260円strStr.md" index 629ff014dd..bf4ad600c3 100644 --- "a/problems/0028.345円256円236円347円216円260円strStr.md" +++ "b/problems/0028.345円256円236円347円216円260円strStr.md" @@ -1358,6 +1358,72 @@ impl Solution { } ``` +>前缀表统一不减一 +```C# +public int StrStr(string haystack, string needle) +{ + if (string.IsNullOrEmpty(needle)) + return 0; + + if (needle.Length> haystack.Length || string.IsNullOrEmpty(haystack)) + return -1; + + return KMP(haystack, needle); +} + +public int KMP(string haystack, string needle) +{ + int[] next = GetNext(needle); + int i = 0, j = 0; + while (i < haystack.Length) + { + if (haystack[i] == needle[j]) + { + i++; + j++; + } + if (j == needle.Length) + return i-j; + else if (i < haystack.Length && haystack[i] != needle[j]) + if (j != 0) + { + j = next[j - 1]; + } + else + { + i++; + } + } + return -1; +} + +public int[] GetNext(string needle) +{ + int[] next = new int[needle.Length]; + next[0] = 0; + int i = 1, j = 0; + while (i < needle.Length) + { + if (needle[i] == needle[j]) + { + next[i++] = ++j; + } + else + { + if (j == 0) + { + next[i++] = 0; + } + else + { + j = next[j - 1]; + } + } + } + return next; +} +``` +

From 6316fff080e11124c6d56d45f931207cb0bfd9d4 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:13:15 +0800 Subject: [PATCH 3/7] =?UTF-8?q?Update=20707.=E8=AE=BE=E8=AE=A1=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...76350円256円241円351円223円276円350円241円250円.md" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git "a/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" "b/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" index c27f0107fb..a08227d9fc 100644 --- "a/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" +++ "b/problems/0707.350円256円276円350円256円241円351円223円276円350円241円250円.md" @@ -1485,6 +1485,77 @@ impl MyLinkedList { } ``` +### C# +```C# +class ListNode +{ + public int val; + public ListNode next; + public ListNode(int val) { this.val = val; } +} +public class MyLinkedList +{ + ListNode dummyHead; + int count; + + public MyLinkedList() + { + dummyHead = new ListNode(0); + count = 0; + } + + public int Get(int index) + { + if (index < 0 || count <= index) return -1; + ListNode current = dummyHead; + for (int i = 0; i <= index; i++) + { + current = current.next; + } + return current.val; + } + + public void AddAtHead(int val) + { + AddAtIndex(0, val); + } + + public void AddAtTail(int val) + { + AddAtIndex(count, val); + } + + public void AddAtIndex(int index, int val) + { + if (index> count) return; + index = Math.Max(0, index); + count++; + ListNode tmp1 = dummyHead; + for (int i = 0; i < index; i++) + { + tmp1 = tmp1.next; + } + ListNode tmp2 = new ListNode(val); + tmp2.next = tmp1.next; + tmp1.next = tmp2; + } + + public void DeleteAtIndex(int index) + { + + if (index>= count || index < 0) return; + var tmp1 = dummyHead; + for (int i = 0; i < index; i++) + { + tmp1 = tmp1.next; + } + tmp1.next = tmp1.next.next; + count--; + + } +} +``` +

From 87a6e78e8106d769576ddc48770deb245f830fd0 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:15:40 +0800 Subject: [PATCH 4/7] =?UTF-8?q?Update=200024.=E4=BA=A4=E6=8D=A2=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E8=8A=82=E7=82=B9=EF=BC=8C=E6=B7=BB=E5=8A=A0C#?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55347円232円204円350円212円202円347円202円271円.md" | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git "a/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" "b/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" index 36f9b0cc9a..57034f4795 100644 --- "a/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" +++ "b/problems/0024.344円270円244円344円270円244円344円272円244円346円215円242円351円223円276円350円241円250円344円270円255円347円232円204円350円212円202円347円202円271円.md" @@ -459,6 +459,40 @@ impl Solution { } ``` +### C# +```C# +// 虚拟头结点 +public ListNode SwapPairs(ListNode head) +{ + var dummyHead = new ListNode(); + dummyHead.next = head; + ListNode cur = dummyHead; + while (cur.next != null && cur.next.next != null) + { + ListNode tmp1 = cur.next; + ListNode tmp2 = cur.next.next.next; + + cur.next = cur.next.next; + cur.next.next = tmp1; + cur.next.next.next = tmp2; + + cur = cur.next.next; + } + return dummyHead.next; +} +``` +``` C# +// 递归 +public ListNode SwapPairs(ListNode head) +{ + if (head == null || head.next == null) return head; + var cur = head.next; + head.next = SwapPairs(head.next.next); + cur.next = head; + return cur; +} +``` +

From 451c045a6e293bd038815400de660d3d6d091873 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:17:40 +0800 Subject: [PATCH 5/7] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=9802.07?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...223276円350円241円250円347円233円270円344円272円244円.md" | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git "a/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" "b/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" index 5de9da5c06..adeaa413aa 100644 --- "a/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" +++ "b/problems/351円235円242円350円257円225円351円242円23002円.07.351円223円276円350円241円250円347円233円270円344円272円244円.md" @@ -502,6 +502,20 @@ object Solution { } } ``` +### C# +```C# +public ListNode GetIntersectionNode(ListNode headA, ListNode headB) +{ + if (headA == null || headB == null) return null; + ListNode cur1 = headA, cur2 = headB; + while (cur1 != cur2) + { + cur1 = cur1 == null ? headB : cur1.next; + cur2 = cur2 == null ? headA : cur2.next; + } + return cur1; +} +```

From 4500cbd9bd1d603c99482fdccbfe1deff7417c15 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:20:04 +0800 Subject: [PATCH 6/7] =?UTF-8?q?Update=200151.=E7=BF=BB=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95=E8=AF=8D=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#=20LINQ=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...262351円207円214円347円232円204円345円215円225円350円257円215円.md" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" "b/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" index 111c07e4f5..d15bb5f321 100644 --- "a/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" +++ "b/problems/0151.347円277円273円350円275円254円345円255円227円347円254円246円344円270円262円351円207円214円347円232円204円345円215円225円350円257円215円.md" @@ -972,6 +972,13 @@ char * reverseWords(char * s){ } ``` +### C# +```C# LINQ高级方法 +public string ReverseWords(string s) { + return string.Join(' ', s.Trim().Split(' ',StringSplitOptions.RemoveEmptyEntries).Reverse()); +} +``` +

From 69cc985a7c45fb41a901c26b2868d481f8b3e206 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:40:37 +0800 Subject: [PATCH 7/7] =?UTF-8?q?Update=200459.=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20345円255円227円347円254円246円344円270円262円.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git "a/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" "b/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" index 177c3878ba..3245d94897 100644 --- "a/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" +++ "b/problems/0459.351円207円215円345円244円215円347円232円204円345円255円220円345円255円227円347円254円246円344円270円262円.md" @@ -681,6 +681,32 @@ impl Solution { } } ``` +### C# +```C# +// 前缀表不减一 +public bool RepeatedSubstringPattern(string s) +{ + if (s.Length == 0) + return false; + int[] next = GetNext(s); + int len = s.Length; + if (next[len - 1] != 0 && len % (len - next[len - 1]) == 0) return true; + return false; +} +public int[] GetNext(string s) +{ + int[] next = Enumerable.Repeat(0, s.Length).ToArray(); + for (int i = 1, j = 0; i < s.Length; i++) + { + while (j> 0 && s[i] != s[j]) + j = next[j - 1]; + if (s[i] == s[j]) + j++; + next[i] = j; + } + return next; +} +```

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