mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge pull request #2330 from eeee0717/master
Update 028.实现strStr,添加C#版本
This commit is contained in:
@ -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;
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -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;
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -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());
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -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;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
@ -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--;
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
|
@ -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;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
<p align="center">
|
||||
|
Reference in New Issue
Block a user