Skip to content

Commit 1187671

Browse files
committed
array to treenode extension method
1 parent 98df603 commit 1187671

1 file changed

Lines changed: 46 additions & 38 deletions

File tree

Algorithms/Utils/Helper.cs

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ public static class Helper
77
{
88
public static ListNode<int> ToListNode(this List<int> lst)
99
{
10-
if(lst.Count>0 && lst.Where(x=>x>=10).Count()==0)
10+
if (lst.Count > 0 && lst.Where(x => x >= 10).Count() == 0)
1111
{
1212
ListNode<int> head = new ListNode<int>(lst[0]);
1313
ListNode<int> current = head;
14-
for (var i=1;i<lst.Count;i++)
14+
for (var i = 1; i < lst.Count; i++)
1515
{
1616
current.Next = new ListNode<int>(lst[i]);
1717
current = current.Next;
@@ -22,12 +22,20 @@ public static ListNode<int> ToListNode(this List<int> lst)
2222
}
2323
public static TreeNode ToTreeNode(this int?[] arr)
2424
{
25-
throw new NotImplementedException("TODO");
25+
return ToTreeNode(arr, 0, arr.Length);
26+
}
27+
private static TreeNode ToTreeNode(int?[] arr, int i, int n)
28+
{
29+
if (i >= n || arr[i] == null) return null;
30+
TreeNode tn = new TreeNode(arr[i].Value);
31+
tn.Left = ToTreeNode(arr, 2 * i + 1, n);
32+
tn.Right = ToTreeNode(arr, 2 * i + 2, n);
33+
return tn;
2634
}
2735
public static List<int> ToList(this ListNode<int> node)
2836
{
2937
var lst = new List<int>();
30-
while(node != null)
38+
while (node != null)
3139
{
3240
lst.Add(node.Val);
3341
node = node.Next;
@@ -36,33 +44,33 @@ public static List<int> ToList(this ListNode<int> node)
3644
}
3745
public static void BubbleSort(int[] nums)
3846
{
39-
for(int i = 0; i < nums.Length - 1; i++)
47+
for (int i = 0; i < nums.Length - 1; i++)
4048
{
41-
for(int j=0; j< nums.Length - 1 - i; j++)
49+
for (int j = 0; j < nums.Length - 1 - i; j++)
4250
{
43-
if(nums[j] > nums[j+1])
51+
if (nums[j] > nums[j + 1])
4452
{
4553
int tmp = nums[j];
46-
nums[j] = nums[j+1];
47-
nums[j+1] = tmp;
54+
nums[j] = nums[j + 1];
55+
nums[j + 1] = tmp;
4856
}
4957
}
5058
}
5159
}
5260
public static void BubbleSort2(int[] nums)
5361
{
5462
int i = nums.Length - 1;
55-
while(i > 0)
63+
while (i > 0)
5664
{
5765
int pos = 0;
58-
for(int j = 0; j < i; j++)
66+
for (int j = 0; j < i; j++)
5967
{
60-
if(nums[j] > nums[j+1])
68+
if (nums[j] > nums[j + 1])
6169
{
6270
pos = j;//记录交换的位置
6371
int tmp = nums[j];
64-
nums[j] = nums[j+1];
65-
nums[j+1] = tmp;
72+
nums[j] = nums[j + 1];
73+
nums[j + 1] = tmp;
6674
}
6775
}
6876
i = pos;
@@ -73,25 +81,25 @@ public static void BubbleSort3(int[] nums)
7381
int low = 0;
7482
int high = nums.Length - 1;
7583
int tmp, j;
76-
while(low < high)
84+
while (low < high)
7785
{
78-
for(j=low; j<high;j++)
86+
for (j = low; j < high; j++)
7987
{
80-
if(nums[j] > nums[j+1])
88+
if (nums[j] > nums[j + 1])
8189
{
8290
tmp = nums[j];
83-
nums[j] = nums[j+1];
84-
nums[j+1] = tmp;
91+
nums[j] = nums[j + 1];
92+
nums[j + 1] = tmp;
8593
}
8694
}
8795
high--;
88-
for(j=high;j>low;j--)
96+
for (j = high; j > low; j--)
8997
{
90-
if(nums[j] < nums[j-1])
98+
if (nums[j] < nums[j - 1])
9199
{
92100
tmp = nums[j];
93-
nums[j] = nums[j-1];
94-
nums[j-1] = tmp;
101+
nums[j] = nums[j - 1];
102+
nums[j - 1] = tmp;
95103
}
96104
}
97105
low++;
@@ -101,68 +109,68 @@ public static void Sort(int[] arr)
101109
{
102110
SortMerge(arr, 0, arr.Length - 1);
103111
}
104-
static void MainMerge(int[] numbers, int left, int mid, int right)
112+
static void MainMerge(int[] numbers, int left, int mid, int right)
105113
{
106114
int[] temp = new int[numbers.Length];
107115
int i, eol, num, pos;
108-
116+
109117
eol = (mid - 1);
110118
pos = left;
111119
num = (right - left + 1);
112-
120+
113121
while ((left <= eol) && (mid <= right))
114122
{
115123
if (numbers[left] <= numbers[mid])
116124
temp[pos++] = numbers[left++];
117125
else
118126
temp[pos++] = numbers[mid++];
119127
}
120-
128+
121129
while (left <= eol)
122130
temp[pos++] = numbers[left++];
123-
131+
124132
while (mid <= right)
125133
temp[pos++] = numbers[mid++];
126-
134+
127135
for (i = 0; i < num; i++)
128136
{
129137
numbers[right] = temp[right];
130138
right--;
131139
}
132140
}
133-
141+
134142
static void SortMerge(int[] numbers, int left, int right)
135143
{
136144
int mid;
137-
145+
138146
if (right > left)
139147
{
140148
mid = (right + left) / 2;
141149
SortMerge(numbers, left, mid);
142150
SortMerge(numbers, (mid + 1), right);
143-
151+
144152
MainMerge(numbers, left, (mid + 1), right);
145153
}
146154
}
147155

148156
public static void QuickSort(int[] nums)
149157
{
150-
QuickSort(nums, 0 , nums.Length - 1);
158+
QuickSort(nums, 0, nums.Length - 1);
151159
}
152160
private static void QuickSort(int[] nums, int low, int high)
153161
{
154-
int privotLoc = Partition(nums,low,high);
155-
QuickSort(nums,low,privotLoc - 1);
162+
int privotLoc = Partition(nums, low, high);
163+
QuickSort(nums, low, privotLoc - 1);
156164
QuickSort(nums, privotLoc + 1, high);
157165
}
158166
private static int Partition(int[] a, int low, int high)
159167
{
160168
int privotKey = a[low];
161-
while(low < high)
169+
while (low < high)
162170
{
163-
while(low < high && a[high] >= privotKey){ high--; }
171+
while (low < high && a[high] >= privotKey) { high--; }
164172
Swap(a[low], a[high]);
165-
while(low < high && a[low] <= privotKey){ low++; }
173+
while (low < high && a[low] <= privotKey) { low++; }
166174
Swap(a[low], a[high]);
167175
}
168176
return low;

0 commit comments

Comments
 (0)