Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1031 from zhangjiongwx/master
Browse files Browse the repository at this point in the history
添加 0344.反转字符串、0541.反转字符串II、0015.三数之和、0018.四数之和 C#版本
  • Loading branch information
youngyangyang04 authored Jan 25, 2022
2 parents 6010030 + e453737 commit 294e4ce
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 3 deletions.
59 changes: 59 additions & 0 deletions problems/0015.三数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,5 +545,64 @@ int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes
}
```
C#:
```csharp
public class Solution
{
public IList<IList<int>> ThreeSum(int[] nums)
{
var result = new List<IList<int>>();
Array.Sort(nums);
for (int i = 0; i < nums.Length - 2; i++)
{
int n1 = nums[i];
if (n1 > 0)
break;
if (i > 0 && n1 == nums[i - 1])
continue;
int left = i + 1;
int right = nums.Length - 1;
while (left < right)
{
int n2 = nums[left];
int n3 = nums[right];
int sum = n1 + n2 + n3;
if (sum > 0)
{
right--;
}
else if (sum < 0)
{
left++;
}
else
{
result.Add(new List<int> { n1, n2, n3 });
while (left < right && nums[left] == n2)
{
left++;
}
while (left < right && nums[right] == n3)
{
right--;
}
}
}
}
return result;
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
62 changes: 62 additions & 0 deletions problems/0018.四数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,67 @@ func fourSum(_ nums: [Int], _ target: Int) -> [[Int]] {
}
```

C#:
```csharp
public class Solution
{
public IList<IList<int>> FourSum(int[] nums, int target)
{
var result = new List<IList<int>>();

Array.Sort(nums);

for (int i = 0; i < nums.Length - 3; i++)
{
int n1 = nums[i];
if (i > 0 && n1 == nums[i - 1])
continue;

for (int j = i + 1; j < nums.Length - 2; j++)
{
int n2 = nums[j];
if (j > i + 1 && n2 == nums[j - 1])
continue;

int left = j + 1;
int right = nums.Length - 1;

while (left < right)
{
int n3 = nums[left];
int n4 = nums[right];
int sum = n1 + n2 + n3 + n4;

if (sum > target)
{
right--;
}
else if (sum < target)
{
left++;
}
else
{
result.Add(new List<int> { n1, n2, n3, n4 });

while (left < right && nums[left] == n3)
{
left++;
}

while (left < right && nums[right] == n4)
{
right--;
}
}
}
}
}

return result;
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
13 changes: 13 additions & 0 deletions problems/0344.反转字符串.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ void reverseString(char* s, int sSize){
}
```
C#:
```csharp
public class Solution
{
public void ReverseString(char[] s)
{
for (int i = 0, j = s.Length - 1; i < j; i++, j--)
{
(s[i], s[j]) = (s[j], s[i]);
}
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
18 changes: 15 additions & 3 deletions problems/0541.反转字符串II.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,21 @@ func reverseStr(_ s: String, _ k: Int) -> String {
}
```




C#:
```csharp
public class Solution
{
public string ReverseStr(string s, int k)
{
Span<char> span = s.ToCharArray().AsSpan();
for (int i = 0; i < span.Length; i += 2 * k)
{
span[i + k < span.Length ? i..(i + k) : i..].Reverse();
}
return span.ToString();
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 comments on commit 294e4ce

Please sign in to comment.