Skip to content

Commit

Permalink
feat: add csharp solutions to lcof problems (doocs#826)
Browse files Browse the repository at this point in the history
* 《剑指 Offer(第 2 版)》添加 C# 题解
  • Loading branch information
xiongbinzou authored Jul 21, 2022
1 parent c793e10 commit 8dcceb9
Show file tree
Hide file tree
Showing 142 changed files with 3,515 additions and 5 deletions.
31 changes: 31 additions & 0 deletions basic/sorting/BubbleSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,37 @@ public class Program
}
```

### **Python3**

```python
def bubbleSort(arr):
n = len(arr)
# Iterate over all array elements
for i in range(n):
# Last i elements are already in place
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]


# 改进版本
def bubbleSort(arr):
n = len(arr)
for i in range(n - 1):
has_change = False
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
has_change = True
if not has_change:
break


arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print(arr)
```

<!-- tabs:end -->

## 算法分析
Expand Down
16 changes: 16 additions & 0 deletions basic/sorting/InsertionSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ public class Program
}
```

### **Python3**

```python
def insertion_sort(array):
for i in range(len(array)):
cur_index = i
while array[cur_index - 1] > array[cur_index] and cur_index - 1 >= 0:
array[cur_index], array[cur_index - 1] = array[cur_index - 1], array[cur_index]
cur_index -= 1
return array

array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
print(insertion_sort(array))

```

<!-- tabs:end -->

## 算法分析
Expand Down
18 changes: 18 additions & 0 deletions basic/sorting/SelectionSort/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ public class Program

```

### **Python3**

```python
def selection_sort(arr):
n = len(arr)
for i in range(n-1):
min_index = i
for j in range(i+1, n):
if arr[j] < arr[min_index]:
min_index = j
arr[min_index], arr[i] = arr[i], arr[min_index]

arr = [26, 11, 99, 33, 69, 77, 55, 56, 67]
selection_sort(arr)
print(arr)

```

<!-- tabs:end -->

## 算法分析
Expand Down
21 changes: 21 additions & 0 deletions lcof/面试题03. 数组中重复的数字/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ impl Solution {
}
```

### **C#**

```cs
public class Solution {
public int FindRepeatNumber(int[] nums) {
int temp;
for (int i = 0; i < nums.Length; i++) {
while (i != nums[i]) {
if (nums[i] == nums[nums[i]]) {
return nums[i];
}
temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
}
}
return -1;
}
}
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions lcof/面试题03. 数组中重复的数字/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Solution {
public int FindRepeatNumber(int[] nums) {
int temp;
for (int i = 0; i < nums.Length; i++) {
while (i != nums[i]) {
if (nums[i] == nums[nums[i]]) {
return nums[i];
}
temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
}
}
return -1;
}
}
23 changes: 23 additions & 0 deletions lcof/面试题04. 二维数组中的查找/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,29 @@ impl Solution {
}
```

### **C#**

```cs
public class Solution {
public bool FindNumberIn2DArray(int[][] matrix, int target) {
if (matrix.Length == 0 || matrix[0].Length == 0) {
return false;
}
int i = 0, j = matrix[0].Length - 1;
while (i < matrix.Length && j >= 0) {
if (target == matrix[i][j]) {
return true;
} else if (target > matrix[i][j]) {
i += 1;
} else {
j -= 1;
}
}
return false;
}
}
```

### **...**

```
Expand Down
18 changes: 18 additions & 0 deletions lcof/面试题04. 二维数组中的查找/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class Solution {
public bool FindNumberIn2DArray(int[][] matrix, int target) {
if (matrix.Length == 0 || matrix[0].Length == 0) {
return false;
}
int i = 0, j = matrix[0].Length - 1;
while (i < matrix.Length && j >= 0) {
if (target == matrix[i][j]) {
return true;
} else if (target > matrix[i][j]) {
i += 1;
} else {
j -= 1;
}
}
return false;
}
}
30 changes: 30 additions & 0 deletions lcof/面试题05. 替换空格/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,36 @@ impl Solution {
}
```

### **C#**

使用 `Replace()`

```cs
public class Solution {
public string ReplaceSpace(string s) {
return s.Replace(" ", "%20");
}
}
```

遍历添加:

```cs
public class Solution {
public string ReplaceSpace(string s) {
StringBuilder res = new StringBuilder();
foreach (var c in s) {
if (c == ' ') {
res.Append("%20");
} else {
res.Append(c);
}
}
return res.ToString();
}
}
```

### **...**

```
Expand Down
5 changes: 5 additions & 0 deletions lcof/面试题05. 替换空格/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class Solution {
public string ReplaceSpace(string s) {
return s.Replace(" ", "%20");
}
}
24 changes: 24 additions & 0 deletions lcof/面试题06. 从尾到头打印链表/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,30 @@ impl Solution {
}
```

### **C#**

```cs
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public int[] ReversePrint(ListNode head) {
List<int> ans = new List<int>();
while (head != null) {
ans.Add(head.val);
head = head.next;
}
ans.Reverse();
return ans.ToArray();
}
}
```

### **...**

```
Expand Down
19 changes: 19 additions & 0 deletions lcof/面试题06. 从尾到头打印链表/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public int[] ReversePrint(ListNode head) {
List<int> ans = new List<int>();
while (head != null) {
ans.Add(head.val);
head = head.next;
}
ans.Reverse();
return ans.ToArray();
}
}
26 changes: 26 additions & 0 deletions lcof/面试题07. 重建二叉树/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,32 @@ impl Solution {
}
```

### **C#**

```cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode BuildTree(int[] preorder, int[] inorder) {
if (preorder.Length == 0) {
return null;
}
TreeNode root = new TreeNode(preorder[0]);
int idx = Array.IndexOf(inorder, root.val);
root.left = BuildTree(preorder[1..(index+1)], inorder[0..idx]);
root.right = BuildTree(preorder[(index+1)..], inorder[(idx+1)..]);
return root;
}
}
```

### **...**

```
Expand Down
21 changes: 21 additions & 0 deletions lcof/面试题07. 重建二叉树/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode BuildTree(int[] preorder, int[] inorder) {
if (preorder.Length == 0) {
return null;
}
TreeNode root = new TreeNode(preorder[0]);
int idx = Array.IndexOf(inorder, root.val);
root.left = BuildTree(preorder[1..(index+1)], inorder[0..idx]);
root.right = BuildTree(preorder[(index+1)..], inorder[(idx+1)..]);
return root;
}
}
35 changes: 34 additions & 1 deletion lcof/面试题09. 用两个栈实现队列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,43 @@ impl CQueue {
*/
```

### **...**
### **C##**

```cs
public class CQueue {
private Stack<int> stack1;
private Stack<int> stack2;

public CQueue() {
stack1 = new Stack<int>();
stack2 = new Stack<int>();
}

public void AppendTail(int value) {
stack1.Push(value);
}

public int DeleteHead() {
if (stack2.Count == 0) {
while (stack1.Count != 0) {
stack2.Push(stack1.Pop());
}
}
return stack2.Count == 0 ? -1 : stack2.Pop();
}
}

/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.AppendTail(value);
* int param_2 = obj.DeleteHead();
*/
```

### **...**

```
```
<!-- tabs:end -->
Loading

0 comments on commit 8dcceb9

Please sign in to comment.