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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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
```
Lines changed: 16 additions & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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
```
Lines changed: 18 additions & 0 deletions
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

Lines changed: 30 additions & 0 deletions
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
```
Lines changed: 5 additions & 0 deletions
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

Lines changed: 24 additions & 0 deletions
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
```

0 commit comments

Comments
 (0)