fix(csharp): reformat csharp codes and docs (#652)

* fix(csharp): reformat the C# codes and docs

* Update time_complexity.md

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
hpstory 2023-07-24 19:50:00 +08:00 committed by GitHub
parent 1777a16865
commit 978d3c2ed7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 47 additions and 77 deletions

View File

@ -45,7 +45,7 @@ public class list {
/* 通过索引遍历列表 */ /* 通过索引遍历列表 */
int count = 0; int count = 0;
for (int i = 0; i < list.Count(); i++) { for (int i = 0; i < list.Count; i++) {
count++; count++;
} }

View File

@ -87,7 +87,7 @@ class MyList {
/* 列表扩容 */ /* 列表扩容 */
public void extendCapacity() { public void extendCapacity() {
// 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组 // 新建一个长度为 numsCapacity * extendRatio 的数组,并将原数组拷贝到新数组
System.Array.Resize(ref nums, numsCapacity * extendRatio); Array.Resize(ref nums, numsCapacity * extendRatio);
// 更新列表容量 // 更新列表容量
numsCapacity = nums.Length; numsCapacity = nums.Length;
} }

View File

@ -87,9 +87,7 @@ public class time_complexity {
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) { if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1] // 交换 nums[j] 与 nums[j + 1]
int tmp = nums[j]; (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // 元素交换包含 3 个单元操作 count += 3; // 元素交换包含 3 个单元操作
} }
} }

View File

@ -17,7 +17,7 @@ public class two_sum {
return new int[] { i, j }; return new int[] { i, j };
} }
} }
return new int[0]; return Array.Empty<int>();
} }
/* 方法二:辅助哈希表 */ /* 方法二:辅助哈希表 */
@ -32,7 +32,7 @@ public class two_sum {
} }
dic.Add(nums[i], i); dic.Add(nums[i], i);
} }
return new int[0]; return Array.Empty<int>();
} }
[Test] [Test]

View File

@ -29,11 +29,11 @@ public class queue {
Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue)); Console.WriteLine("出队元素 pop = " + pop + ",出队后 queue = " + string.Join(",", queue));
/* 获取队列的长度 */ /* 获取队列的长度 */
int size = queue.Count(); int size = queue.Count;
Console.WriteLine("队列长度 size = " + size); Console.WriteLine("队列长度 size = " + size);
/* 判断队列是否为空 */ /* 判断队列是否为空 */
bool isEmpty = queue.Count() == 0; bool isEmpty = queue.Count == 0;
Console.WriteLine("队列是否为空 = " + isEmpty); Console.WriteLine("队列是否为空 = " + isEmpty);
} }
} }

View File

@ -30,11 +30,11 @@ public class stack {
Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack)); Console.WriteLine("出栈元素 pop = " + pop + ",出栈后 stack = " + string.Join(",", stack));
/* 获取栈的长度 */ /* 获取栈的长度 */
int size = stack.Count(); int size = stack.Count;
Console.WriteLine("栈的长度 size = " + size); Console.WriteLine("栈的长度 size = " + size);
/* 判断是否为空 */ /* 判断是否为空 */
bool isEmpty = stack.Count() == 0; bool isEmpty = stack.Count == 0;
Console.WriteLine("栈是否为空 = " + isEmpty); Console.WriteLine("栈是否为空 = " + isEmpty);
} }
} }

View File

@ -111,8 +111,7 @@
```csharp title="" ```csharp title=""
/* 链表节点类 */ /* 链表节点类 */
class ListNode class ListNode {
{
int val; // 节点值 int val; // 节点值
ListNode next; // 指向下一节点的引用 ListNode next; // 指向下一节点的引用
ListNode(int x) => val = x; //构造函数 ListNode(int x) => val = x; //构造函数

View File

@ -539,15 +539,13 @@
```csharp title="list.cs" ```csharp title="list.cs"
/* 通过索引遍历列表 */ /* 通过索引遍历列表 */
int count = 0; int count = 0;
for (int i = 0; i < list.Count(); i++) for (int i = 0; i < list.Count; i++) {
{
count++; count++;
} }
/* 直接遍历列表元素 */ /* 直接遍历列表元素 */
count = 0; count = 0;
foreach (int n in list) foreach (int n in list) {
{
count++; count++;
} }
``` ```

View File

@ -200,22 +200,19 @@
```csharp title="" ```csharp title=""
/* 类 */ /* 类 */
class Node class Node {
{
int val; int val;
Node next; Node next;
Node(int x) { val = x; } Node(int x) { val = x; }
} }
/* 函数 */ /* 函数 */
int function() int function() {
{
// do something... // do something...
return 0; return 0;
} }
int algorithm(int n) // 输入数据 int algorithm(int n) { // 输入数据
{
const int a = 0; // 暂存数据(常量) const int a = 0; // 暂存数据(常量)
int b = 0; // 暂存数据(变量) int b = 0; // 暂存数据(变量)
Node node = new Node(0); // 暂存数据(对象) Node node = new Node(0); // 暂存数据(对象)
@ -376,12 +373,10 @@
=== "C#" === "C#"
```csharp title="" ```csharp title=""
void algorithm(int n) void algorithm(int n) {
{
int a = 0; // O(1) int a = 0; // O(1)
int[] b = new int[10000]; // O(1) int[] b = new int[10000]; // O(1)
if (n > 10) if (n > 10) {
{
int[] nums = new int[n]; // O(n) int[] nums = new int[n]; // O(n)
} }
} }
@ -564,22 +559,18 @@
=== "C#" === "C#"
```csharp title="" ```csharp title=""
int function() int function() {
{
// do something // do something
return 0; return 0;
} }
/* 循环 O(1) */ /* 循环 O(1) */
void loop(int n) void loop(int n) {
{ for (int i = 0; i < n; i++) {
for (int i = 0; i < n; i++)
{
function(); function();
} }
} }
/* 递归 O(n) */ /* 递归 O(n) */
int recur(int n) int recur(int n) {
{
if (n == 1) return 1; if (n == 1) return 1;
return recur(n - 1); return recur(n - 1);
} }

View File

@ -62,12 +62,12 @@ $$
```go title="" ```go title=""
// 在某运行平台下 // 在某运行平台下
func algorithm(n int) { func algorithm(n int) {
a := 2 // 1 ns a := 2 // 1 ns
a = a + 1 // 1 ns a = a + 1 // 1 ns
a = a * 2 // 10 ns a = a * 2 // 10 ns
// 循环 n 次 // 循环 n 次
for i := 0; i < n; i++ { // 1 ns for i := 0; i < n; i++ { // 1 ns
fmt.Println(a) // 5 ns fmt.Println(a) // 5 ns
} }
} }
``` ```
@ -121,15 +121,13 @@ $$
```csharp title="" ```csharp title=""
// 在某运行平台下 // 在某运行平台下
void algorithm(int n) void algorithm(int n) {
{
int a = 2; // 1 ns int a = 2; // 1 ns
a = a + 1; // 1 ns a = a + 1; // 1 ns
a = a * 2; // 10 ns a = a * 2; // 10 ns
// 循环 n 次 // 循环 n 次
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++) { // 1 ns 每轮都要执行 i++
{ // 1 ns ,每轮都要执行 i++ Console.WriteLine(0); // 5 ns
Console.WriteLine(0); // 5 ns
} }
} }
``` ```
@ -329,23 +327,18 @@ $$
```csharp title="" ```csharp title=""
// 算法 A 时间复杂度:常数阶 // 算法 A 时间复杂度:常数阶
void algorithm_A(int n) void algorithm_A(int n) {
{
Console.WriteLine(0); Console.WriteLine(0);
} }
// 算法 B 时间复杂度:线性阶 // 算法 B 时间复杂度:线性阶
void algorithm_B(int n) void algorithm_B(int n) {
{ for (int i = 0; i < n; i++) {
for (int i = 0; i < n; i++)
{
Console.WriteLine(0); Console.WriteLine(0);
} }
} }
// 算法 C 时间复杂度:常数阶 // 算法 C 时间复杂度:常数阶
void algorithm_C(int n) void algorithm_C(int n) {
{ for (int i = 0; i < 1000000; i++) {
for (int i = 0; i < 1000000; i++)
{
Console.WriteLine(0); Console.WriteLine(0);
} }
} }
@ -518,14 +511,12 @@ $$
=== "C#" === "C#"
```csharp title="" ```csharp title=""
void algorithm(int n) void algorithm(int n) {
{
int a = 1; // +1 int a = 1; // +1
a = a + 1; // +1 a = a + 1; // +1
a = a * 2; // +1 a = a * 2; // +1
// 循环 n 次 // 循环 n 次
for (int i = 0; i < n; i++) // +1每轮都执行 i ++ for (int i = 0; i < n; i++) { // +1每轮都执行 i ++
{
Console.WriteLine(0); // +1 Console.WriteLine(0); // +1
} }
} }
@ -744,20 +735,16 @@ $$
=== "C#" === "C#"
```csharp title="" ```csharp title=""
void algorithm(int n) void algorithm(int n) {
{
int a = 1; // +0技巧 1 int a = 1; // +0技巧 1
a = a + n; // +0技巧 1 a = a + n; // +0技巧 1
// +n技巧 2 // +n技巧 2
for (int i = 0; i < 5 * n + 1; i++) for (int i = 0; i < 5 * n + 1; i++) {
{
Console.WriteLine(0); Console.WriteLine(0);
} }
// +n*n技巧 3 // +n*n技巧 3
for (int i = 0; i < 2 * n; i++) for (int i = 0; i < 2 * n; i++) {
{ for (int j = 0; j < n + 1; j++) {
for (int j = 0; j < n + 1; j++)
{
Console.WriteLine(0); Console.WriteLine(0);
} }
} }

View File

@ -368,18 +368,15 @@
```csharp title="hash_map.cs" ```csharp title="hash_map.cs"
/* 遍历哈希表 */ /* 遍历哈希表 */
// 遍历键值对 Key->Value // 遍历键值对 Key->Value
foreach (var kv in map) foreach (var kv in map) {
{
Console.WriteLine(kv.Key + " -> " + kv.Value); Console.WriteLine(kv.Key + " -> " + kv.Value);
} }
// 单独遍历键 key // 单独遍历键 key
foreach (int key in map.Keys) foreach (int key in map.Keys) {
{
Console.WriteLine(key); Console.WriteLine(key);
} }
// 单独遍历值 value // 单独遍历值 value
foreach (String val in map.Values) foreach (String val in map.Values) {
{
Console.WriteLine(val); Console.WriteLine(val);
} }
``` ```

View File

@ -212,10 +212,10 @@
int pop = queue.Dequeue(); int pop = queue.Dequeue();
/* 获取队列的长度 */ /* 获取队列的长度 */
int size = queue.Count(); int size = queue.Count;
/* 判断队列是否为空 */ /* 判断队列是否为空 */
bool isEmpty = queue.Count() == 0; bool isEmpty = queue.Count == 0;
``` ```
=== "Swift" === "Swift"

View File

@ -211,10 +211,10 @@
int pop = stack.Pop(); int pop = stack.Pop();
/* 获取栈的长度 */ /* 获取栈的长度 */
int size = stack.Count(); int size = stack.Count;
/* 判断是否为空 */ /* 判断是否为空 */
bool isEmpty = stack.Count()==0; bool isEmpty = stack.Count == 0;
``` ```
=== "Swift" === "Swift"