1、微软面试常用算法总结C#编写全是用C#写的算法,并且都在VS上运行通过啦,希望能给大家一点参考!1.冒泡算法(这个太简单啦,不会考的,不过我们可以把这个当作Sort()工具来用哦,下边你就知道啦!)public static int bubble(int a) int temp = 0; for(int i=a.Length -1;i0;i-) for (int j = 1; j aj) temp = aj - 1; aj - 1 = aj; aj=temp; return a; 2.判断回文(比如给你一个字符串”madam”,这就是一个回文啦!)public static bool Chec
2、kPalindrome(string str) char charCompare = str.ToCharArray(); Stack s = new Stack(); Queue q = new Queue(); bool justfy = true; for (int i = 0; i str.Length; i+) s.Push(charComparei); q.Enqueue(charComparei); for (int i = 0; i str.Length; i+) if (s.Pop() != q.Dequeue() justfy = false; return justfy;
3、 3.两个无序数组的合并(如果是字符串数组的话把int改成string就可以啦!) public static List merge(int a, int b) List c = new List(); int i = 0; int j = 0; if (a=null|a.Length = 0) return b.ToList(); if (b=null|b.Length = 0) return a.ToList(); Array.Sort(a); /如果数组是有序的话这句就不用啦! Array.Sort(b); / 如果数组是有序的话这句就不用啦! if (a0 b0) c.Add(b0);
4、 j+; else c.Add(a0); i+; while (i a.Length & j b.Length) if (ai bj) c.Add(ai); i+; else c.Add(bj); j+; while (i a.Length) c.Add(ai); i+; while (j b.Length) c.Add(bj); j+; return c; 4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用) public static List distll(int ar) List list1 = new List(); list1
5、.Add(ar0); for (int i = 0; i ar.Length; i+) int j = 0; while (j 0) Array.Sort(ar); int size = 1; for (int i = 1; i ar.Length; i+) if (ari != ari - 1) size+; int temp = new intsize; int k = 0; tempk+ = ar0; for (int j = 1; j ar.Length; j+) if (arj - 1 != arj) tempk+ = arj; return temp; else return nu
6、ll; 6.字符串比较去重,删除string2中和string1中相同的字符。(是这样子滴:string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:string3=sfhmnds)public static List distill(string sta, string stb) List ar = new List(); if (sta = null | sta.Length = 0) for (int l = 0; l stb.Length; l+) ar.Add(stbl.ToString(); return ar; if (stb = null |
7、 stb.Length = 0) return ar; else for (int j = 0; j stb.Length; j+) int i = 0; while (i sta.Length) if (stbj = stai) break; else i+; if (i = sta.Length) ar.Add(stbj.ToString(); return ar; 6.不让你直接用库函数split来实现字符串的分割(比如:用”sp”来分割”goodspmorning”,之后会得到”good morning”) public static string mysplit(string str
8、input, string sp) string tmp = ; int strlen = 0, splen = 0; int found = 0; string rt = null; if (strinput = null | sp = null | strinput.Length = 0) return null; if (sp.Length = 0) return new string strinput ; ArrayList tmp3 = new ArrayList(); strlen = strinput.Length; splen = sp.Length; for (int i =
9、 0; i = 0) tmp = ; tmp = strinput.Substring(i, found - i); tmp3.Add(tmp); i = found + splen - 1; else string tmp2 = ; tmp2 = strinput.Substring(i); if (tmp2 != ) tmp3.Add(tmp2); break; tmp3.TrimToSize(); rt = (string)tmp3.ToArray(typeof(String); tmp3.Clear(); return rt; 7.把一个由数字组成的字符串变成整数(像这样string=
10、”123”变成int型的123(一百二十三) public static int trans(string str) if (str = null | str.Length = 0) return -1; int result = 0; for (int j = 0; j str.Length; j+) if (0 = (strj - 0) & (strj - 0) this.key) if(this.right = null) this.right = node;/node插入的节点 return; else this.right.InsertNode(node); else if(this
11、.left = null) this.left = node; return; else this.left.InsertNode(node); / 二叉查找树查询 public bool SearchKey(int searchValue) if(this.key = searchValue)/searchValue需要查询的值 return true;/ 是否找到查询的值 if(searchValue this.key) if(this.right = null) return false; else return this.right.SearchKey(searchValue); el
12、se if(this.left = null) return false; else return this.left.SearchKey(searchValue); / 中序遍历 public void MiddleDisplay() if(this.left != null) this.left.MiddleDisplay(); Console.WriteLine(this.key); if(this.right != null) this.right.MiddleDisplay(); / 前序遍历 public void FrontDisplay() Console.WriteLine(
13、this.key); if(this.left != null) this.left.FrontDisplay(); if(this.right != null) this.right.FrontDisplay(); / 后序遍历 public void BehindDisplay() if(this.left != null) this.left.BehindDisplay(); if(this.right != null) this.right.BehindDisplay(); Console.WriteLine(this.key); / 二叉查找树 public class Binary
14、SearchTree private BinarySearchTreeNode root; / 生成一个二叉查找树 public BinarySearchTree() root = null; / 生成一个二叉查找树 / 二叉查找树根节点的值 public BinarySearchTree(int nodeValue) root = new BinarySearchTreeNode(nodeValue); / 在二叉查找树上插入一个节点 / 插入节点的值 public void InsertBinarySearchTreeNode(int nodeValue) BinarySearchTree
15、Node insertNode = new BinarySearchTreeNode(nodeValue); if(root = null) root = insertNode; return; else root.InsertNode(insertNode); return; / 在二叉查找树上查询一个数 / 需要查询的值 / 是否找到查询的值 public bool SearchKey(int searchValue) if(root.key = searchValue) return true; else return root.SearchKey(searchValue); / 二叉查
16、找树中序遍历 public void MiddleDisplay() root.MiddleDisplay(); return; / 二叉查找树前序遍历 public void FrontDisplay() root.FrontDisplay(); return; / 二叉查找树后序遍历 public void BehindDisplay() root.BehindDisplay(); return; / 二叉查找树排序 / 需要排序的数组 public static void BinarySearchTreeSort(int a) BinarySearchTree t = new Binar
17、ySearchTree(); for(int i = 0; i a.Length; i +) t.InsertBinarySearchTreeNode(ai); t.MiddleDisplay();return; / 二叉查找树查找 / 进行查找的数组 / 需要查找的树 public static bool BinarySearchTreeSearch(int a, int searchKey) BinarySearchTree t = new BinarySearchTree(); for(int i = 0; i this.n) if(this.Right = null) this.Rig
18、ht = node; else this.Right.Insert(node); else if(this.Left = null) this.Left = node; else this.Left.Insert(node); /递归 public void Show() Console.WriteLine(n); class BinaryTree Node root; public void GenerateTree(Node node) /高内聚,低耦合 if(root = null) root = node; return; /如果树是空,第一次加节点 root.Insert(node)
19、; public void ShowInOrder(Node node) /中序遍历(in order):左中右。先(前)序遍历(pre order):中左右。后序遍历(post order):左右中。 if(node = null) return;/递归必须有个终止条件,递归方法中一定要接受参数 ShowInOrder(node.Left); node.Show(); ShowInOrder(node.Right); public void Show() ShowInOrder(root); class A static void Main() BinaryTree b = new BinaryTree(); Node node = new Node(5); b.GenerateTree(node); node = new Node(13); b.GenerateTree(node); node = new Node(6); b.GenerateTree(node); node = new Node(26); b.GenerateTree(node); node = new Node(7); b.GenerateTree(node); b.Show(); 结果:5,6,7,13,26
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1