面试题

今天去面试考官给了下面几个题,随便做了下
有没有更好的算法来实现这些东西呢?有兴趣的朋友可以探讨下
  1         //1.实现  string.reverse 方法
  2         private string Reverse(string str)
  3         {
  4             string result = string.Empty;
  5             if (!string.IsNullOrEmpty(str))
  6             {
  7                 for (int i = str.Length - 1; i >= 0; i--)
  8                 {
  9                     result += str[i];
 10                 }
 11             }
 12             return result;
 13         }
 14 
 15         //2.实现  string.Split方法
 16         private string[] Split(string str, char separate)
 17         {
 18             string[] result;
 19             List<string> outPutList = new List<string>();
 20             string temp = string.Empty;
 21             for (int i = 0; i < str.Length; i++)
 22             {
 23                 if (str[i] != separate)
 24                 {
 25                     temp += str[i];
 26                 }
 27                 else
 28                 {
 29                     if (temp != string.Empty)
 30                     {
 31                         outPutList.Add(temp);
 32                         temp = string.Empty;
 33                     }
 34                 }
 35             }
 36             if (temp != string.Empty)
 37                 outPutList.Add(temp);
 38             result = outPutList.ToArray();
 39             return result;
 40         }
 41 
 42         //3.实现 string.SubString 方法
 43         private string SubString(int startPiont, int lenght)
 44         {
 45             string inputString = "abcdefghijklmn";
 46             string result = string.Empty;
 47 
 48             for (int i = startPiont; i < inputString.Length; i++)
 49             {
 50                 if (i - startPiont >= lenght) break;
 51                 else result += inputString[i];
 52             }
 53             return result;
 54         }
 55 
 56         //4.实现 string.Contains 方法
 57         private int Contains(string str1, string str2)
 58         {
 59             int result = -1;
 60             if (string.IsNullOrEmpty(str2) || string.IsNullOrEmpty(str1))
 61             {
 62                 result = -1;
 63             }
 64             else
 65             {
 66                 for (int i = 0; i <= str1.Length - str2.Length; i++)
 67                 {
 68                     if (str1[i] == str2[0])
 69                     {
 70                         int j = 1;
 71                         for (j = 1; j < str2.Length && j + i < str1.Length; j++)
 72                         {
 73                             if (str2[j] != str1[i + j]) break;
 74                         }
 75                         if (j == str2.Length)
 76                             return i;
 77                     }
 78                 }
 79             }
 80             return result;
 81 
 82         }
 83 
 84         class Node
 85         {
 86             public int Data { get; set; }
 87             public Node Next { get; set; }
 88         }
 89 
 90         //5.单链表排序返回头结点
 91         private Node Sort(Node list)
 92         {
 93             Node head = new Node();
 94             Node temp1 = new Node();
 95             Node temp2 = new Node();
 96             int re = 0;
 97             temp1 = list;
 98             while (temp1.Next != null)
 99             {
100                 temp2 = temp1;
101                 while (temp2.Next != null)
102                 {
103                     temp2 = temp2.Next;
104                     if (temp1.Data > temp2.Data)
105                     {
106                         int t = temp1.Data;
107                         temp1.Data = temp2.Data;
108                         temp2.Data = t;
109                     }
110                     if (re == 0)
111                     {
112                         head = temp1; re++;
113                     }
114                 }
115                 temp1 = temp1.Next;
116             }
117             return head;
118         }
119 
120         //6.翻转单链表
121         private Node Reverse(Node list)
122         {
123             Node temp = list;
124             Node upNode = null;
125             Node next = null;
126             while (temp.Next != null)
127             {
128                 next = temp.Next;
129                 temp.Next = upNode;
130                 upNode = temp;
131                 temp = next;
132             }
133             temp.Next = upNode;
134             return temp;
135         }
136 
137         //7 实现双链表合并有序链表
138         Node Merge(Node list1, Node list2)
139         { 
140             Node head =new Node();
141             Node temp1, temp2;
142             Node temp3 = null;
143             temp1=list1 ;
144             temp2 = list2; 
145             while (temp1 != null && temp2 != null)
146             {
147                 Node N = new Node();
148                 if (temp1.Data > temp2.Data)
149                 {
150                     N = temp1;
151                     temp1 = temp1.Next;
152                 }
153                 else
154                 {
155                     N = temp2;
156                     temp2 = temp2.Next;
157                 }
158                 //if 
159                 if (temp3 == null)
160                 {
161                     temp3 = N;
162                     head = N;
163                 }
164                 else
165                 {
166                     temp3.Next = N;
167                     temp3 = temp3.Next;
168                 }    
169             }
170             if(temp2!=null )             
171             {
172                 temp3.Next = temp2;
173             }
174             if (temp1 != null)
175             {
176                 temp3.Next = temp1;
177             } 
178             return head; 
179         }
180 
181         private void NodeTest()
182         {
183             string beforeSort = "排序前:";
184             string afterSort = "排序后:";
185             string afterReverse = "翻转后:";
186             string AfterMerge = "合并后:";
187             Random r = new Random();
188             Node head1 = new Node() { Data = r.Next(0, 100) };
189             Node temp = new Node();
190             temp = head1;
191             for (int i = 1; i < 10; i++)
192             {
193                 Node n = new Node();
194                 n.Data = r.Next(0, 100);
195                 temp.Next = n;
196                 temp = n;
197             }
198             OutPutNode(beforeSort, head1);
199             head1 = Sort(head1);
200             OutPutNode(afterSort, head1);
201             head1 = Reverse(head1);
202             OutPutNode(afterReverse, head1);
203             Node head2 = new Node() { Data = r.Next(0, 100) };
204             temp = head2;
205             for (int i = 1; i < 5; i++)
206             {
207                 Node n = new Node();
208                 n.Data = r.Next(0, 100);
209                 temp.Next = n;
210                 temp = n;
211             }
212             OutPutNode(beforeSort, head2);
213             head2 = Sort(head2);
214             OutPutNode(afterSort, head2);
215             head2 = Reverse(head2);
216             OutPutNode(afterReverse, head2);
217             OutPutNode(AfterMerge, Merge(head1, head2));
218         }
219 
220         private void OutPutNode(string headStr, Node temp)
221         {
222             string printstr = string.Empty;
223             printstr += headStr;
224             while (temp.Next != null)
225             {
226                 printstr += temp.Data.ToString() + " ";
227                 temp = temp.Next;
228             }
229 
230             printstr += temp.Data.ToString();
231             textBox1.Text += printstr + "
";
232             //MessageBox.Show(printstr); 
233 
234         }
235 
236         private void Form1_Load(object sender, EventArgs e)
237         {
238             NodeTest();
239         }
View Code

  

 1  //双链表合并另一种思路
 2         Node Merge2(Node list1, Node list2)
 3         {
 4             Node head = new Node();
 5             Node temp = new Node();
 6             Node node2 = new Node();
 7             Node current = new Node();
 8             if (list1.Data > list2.Data)
 9             {
10                 current = list1;
11                 node2 = list2;
12             }
13             else
14             {
15                 current = list2;
16                 node2 = list1;
17             }
18             head = current; 
19             while (current.Next != null)
20             {
21                 if (current.Next.Data < node2.Data)
22                 { 
23                     temp  = current.Next;
24                     current.Next = node2;
25                     node2 = temp;
26                 }
27                 else
28                 { 
29                     current = current.Next;
30                 }
31             }
32             current.Next = node2;
33             return head;
34         }
View Code
原文地址:https://www.cnblogs.com/bingxueme/p/3157428.html