Skip to content

Commit 8dcceb9

Browse files
authored
feat: add csharp solutions to lcof problems (doocs#826)
* 《剑指 Offer(第 2 版)》添加 C# 题解
1 parent c793e10 commit 8dcceb9

File tree

142 files changed

+3515
-5
lines changed

Some content is hidden

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

142 files changed

+3515
-5
lines changed

basic/sorting/BubbleSort/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,37 @@ public class Program
238238
}
239239
```
240240

241+
### **Python3**
242+
243+
```python
244+
def bubbleSort(arr):
245+
n = len(arr)
246+
# Iterate over all array elements
247+
for i in range(n):
248+
# Last i elements are already in place
249+
for j in range(n - i - 1):
250+
if arr[j] > arr[j + 1]:
251+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
252+
253+
254+
# 改进版本
255+
def bubbleSort(arr):
256+
n = len(arr)
257+
for i in range(n - 1):
258+
has_change = False
259+
for j in range(n - i - 1):
260+
if arr[j] > arr[j + 1]:
261+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
262+
has_change = True
263+
if not has_change:
264+
break
265+
266+
267+
arr = [64, 34, 25, 12, 22, 11, 90]
268+
bubbleSort(arr)
269+
print(arr)
270+
```
271+
241272
<!-- tabs:end -->
242273

243274
## 算法分析

basic/sorting/InsertionSort/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ public class Program
197197
}
198198
```
199199

200+
### **Python3**
201+
202+
```python
203+
def insertion_sort(array):
204+
for i in range(len(array)):
205+
cur_index = i
206+
while array[cur_index - 1] > array[cur_index] and cur_index - 1 >= 0:
207+
array[cur_index], array[cur_index - 1] = array[cur_index - 1], array[cur_index]
208+
cur_index -= 1
209+
return array
210+
211+
array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
212+
print(insertion_sort(array))
213+
214+
```
215+
200216
<!-- tabs:end -->
201217

202218
## 算法分析

basic/sorting/SelectionSort/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ public class Program
205205

206206
```
207207

208+
### **Python3**
209+
210+
```python
211+
def selection_sort(arr):
212+
n = len(arr)
213+
for i in range(n-1):
214+
min_index = i
215+
for j in range(i+1, n):
216+
if arr[j] < arr[min_index]:
217+
min_index = j
218+
arr[min_index], arr[i] = arr[i], arr[min_index]
219+
220+
arr = [26, 11, 99, 33, 69, 77, 55, 56, 67]
221+
selection_sort(arr)
222+
print(arr)
223+
224+
```
225+
208226
<!-- tabs:end -->
209227

210228
## 算法分析

lcof/面试题03. 数组中重复的数字/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,27 @@ impl Solution {
189189
}
190190
```
191191

192+
### **C#**
193+
194+
```cs
195+
public class Solution {
196+
public int FindRepeatNumber(int[] nums) {
197+
int temp;
198+
for (int i = 0; i < nums.Length; i++) {
199+
while (i != nums[i]) {
200+
if (nums[i] == nums[nums[i]]) {
201+
return nums[i];
202+
}
203+
temp = nums[i];
204+
nums[i] = nums[temp];
205+
nums[temp] = temp;
206+
}
207+
}
208+
return -1;
209+
}
210+
}
211+
```
212+
192213
### **...**
193214

194215
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int FindRepeatNumber(int[] nums) {
3+
int temp;
4+
for (int i = 0; i < nums.Length; i++) {
5+
while (i != nums[i]) {
6+
if (nums[i] == nums[nums[i]]) {
7+
return nums[i];
8+
}
9+
temp = nums[i];
10+
nums[i] = nums[temp];
11+
nums[temp] = temp;
12+
}
13+
}
14+
return -1;
15+
}
16+
}

lcof/面试题04. 二维数组中的查找/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,29 @@ impl Solution {
208208
}
209209
```
210210

211+
### **C#**
212+
213+
```cs
214+
public class Solution {
215+
public bool FindNumberIn2DArray(int[][] matrix, int target) {
216+
if (matrix.Length == 0 || matrix[0].Length == 0) {
217+
return false;
218+
}
219+
int i = 0, j = matrix[0].Length - 1;
220+
while (i < matrix.Length && j >= 0) {
221+
if (target == matrix[i][j]) {
222+
return true;
223+
} else if (target > matrix[i][j]) {
224+
i += 1;
225+
} else {
226+
j -= 1;
227+
}
228+
}
229+
return false;
230+
}
231+
}
232+
```
233+
211234
### **...**
212235

213236
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public bool FindNumberIn2DArray(int[][] matrix, int target) {
3+
if (matrix.Length == 0 || matrix[0].Length == 0) {
4+
return false;
5+
}
6+
int i = 0, j = matrix[0].Length - 1;
7+
while (i < matrix.Length && j >= 0) {
8+
if (target == matrix[i][j]) {
9+
return true;
10+
} else if (target > matrix[i][j]) {
11+
i += 1;
12+
} else {
13+
j -= 1;
14+
}
15+
}
16+
return false;
17+
}
18+
}

lcof/面试题05. 替换空格/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,36 @@ impl Solution {
183183
}
184184
```
185185

186+
### **C#**
187+
188+
使用 `Replace()`
189+
190+
```cs
191+
public class Solution {
192+
public string ReplaceSpace(string s) {
193+
return s.Replace(" ", "%20");
194+
}
195+
}
196+
```
197+
198+
遍历添加:
199+
200+
```cs
201+
public class Solution {
202+
public string ReplaceSpace(string s) {
203+
StringBuilder res = new StringBuilder();
204+
foreach (var c in s) {
205+
if (c == ' ') {
206+
res.Append("%20");
207+
} else {
208+
res.Append(c);
209+
}
210+
}
211+
return res.ToString();
212+
}
213+
}
214+
```
215+
186216
### **...**
187217

188218
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Solution {
2+
public string ReplaceSpace(string s) {
3+
return s.Replace(" ", "%20");
4+
}
5+
}

lcof/面试题06. 从尾到头打印链表/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,30 @@ impl Solution {
297297
}
298298
```
299299

300+
### **C#**
301+
302+
```cs
303+
/**
304+
* Definition for singly-linked list.
305+
* public class ListNode {
306+
* public int val;
307+
* public ListNode next;
308+
* public ListNode(int x) { val = x; }
309+
* }
310+
*/
311+
public class Solution {
312+
public int[] ReversePrint(ListNode head) {
313+
List<int> ans = new List<int>();
314+
while (head != null) {
315+
ans.Add(head.val);
316+
head = head.next;
317+
}
318+
ans.Reverse();
319+
return ans.ToArray();
320+
}
321+
}
322+
```
323+
300324
### **...**
301325

302326
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
public class Solution {
10+
public int[] ReversePrint(ListNode head) {
11+
List<int> ans = new List<int>();
12+
while (head != null) {
13+
ans.Add(head.val);
14+
head = head.next;
15+
}
16+
ans.Reverse();
17+
return ans.ToArray();
18+
}
19+
}

lcof/面试题07. 重建二叉树/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,32 @@ impl Solution {
298298
}
299299
```
300300

301+
### **C#**
302+
303+
```cs
304+
/**
305+
* Definition for a binary tree node.
306+
* public class TreeNode {
307+
* public int val;
308+
* public TreeNode left;
309+
* public TreeNode right;
310+
* public TreeNode(int x) { val = x; }
311+
* }
312+
*/
313+
public class Solution {
314+
public TreeNode BuildTree(int[] preorder, int[] inorder) {
315+
if (preorder.Length == 0) {
316+
return null;
317+
}
318+
TreeNode root = new TreeNode(preorder[0]);
319+
int idx = Array.IndexOf(inorder, root.val);
320+
root.left = BuildTree(preorder[1..(index+1)], inorder[0..idx]);
321+
root.right = BuildTree(preorder[(index+1)..], inorder[(idx+1)..]);
322+
return root;
323+
}
324+
}
325+
```
326+
301327
### **...**
302328

303329
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
public class Solution {
11+
public TreeNode BuildTree(int[] preorder, int[] inorder) {
12+
if (preorder.Length == 0) {
13+
return null;
14+
}
15+
TreeNode root = new TreeNode(preorder[0]);
16+
int idx = Array.IndexOf(inorder, root.val);
17+
root.left = BuildTree(preorder[1..(index+1)], inorder[0..idx]);
18+
root.right = BuildTree(preorder[(index+1)..], inorder[(idx+1)..]);
19+
return root;
20+
}
21+
}

lcof/面试题09. 用两个栈实现队列/README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,43 @@ impl CQueue {
288288
*/
289289
```
290290

291-
### **...**
291+
### **C##**
292+
293+
```cs
294+
public class CQueue {
295+
private Stack<int> stack1;
296+
private Stack<int> stack2;
297+
298+
public CQueue() {
299+
stack1 = new Stack<int>();
300+
stack2 = new Stack<int>();
301+
}
302+
303+
public void AppendTail(int value) {
304+
stack1.Push(value);
305+
}
306+
307+
public int DeleteHead() {
308+
if (stack2.Count == 0) {
309+
while (stack1.Count != 0) {
310+
stack2.Push(stack1.Pop());
311+
}
312+
}
313+
return stack2.Count == 0 ? -1 : stack2.Pop();
314+
}
315+
}
292316

317+
/**
318+
* Your CQueue object will be instantiated and called as such:
319+
* CQueue obj = new CQueue();
320+
* obj.AppendTail(value);
321+
* int param_2 = obj.DeleteHead();
322+
*/
293323
```
294324

325+
### **...**
326+
295327
```
296328
329+
```
297330
<!-- tabs:end -->

0 commit comments

Comments
 (0)