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;
for (int i = 0; i < list.Count(); i++) {
for (int i = 0; i < list.Count; i++) {
count++;
}

View File

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

View File

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

View File

@ -17,7 +17,7 @@ public class two_sum {
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);
}
return new int[0];
return Array.Empty<int>();
}
[Test]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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