微软面试常用算法总结C#编写.docx
- 文档编号:8493227
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:15
- 大小:18.37KB
微软面试常用算法总结C#编写.docx
《微软面试常用算法总结C#编写.docx》由会员分享,可在线阅读,更多相关《微软面试常用算法总结C#编写.docx(15页珍藏版)》请在冰豆网上搜索。
微软面试常用算法总结C#编写
全是用C#写的算法,并且都在VS上运行通过啦,希望能给大家一点参考~!
1.冒泡算法(这个太简单啦,不会考的,不过我们可以把这个当作Sort()工具来用哦,下边你就知道啦!
)
publicstaticint[]bubble(int[]a)
{
inttemp=0;
for(inti=a.Length-1;i>0;i--)
for(intj=1;j<=i;j++)
{
if(a[j-1]>a[j])
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
returna;
}
2.判断回文(比如给你一个字符串”madam”,这就是一个回文啦!
)
publicstaticboolCheckPalindrome(stringstr)
{
char[]charCompare=str.ToCharArray();
Stack
Queue
booljustfy=true;
for(inti=0;i { s.Push(charCompare[i]); q.Enqueue(charCompare[i]); } for(inti=0;i { if(s.Pop()! =q.Dequeue()) { justfy=false; } } returnjustfy; } 3.两个无序数组的合并(如果是字符串数组的话把int[]改成string[]就可以啦! ) publicstaticList { List inti=0; intj=0; if(a==null||a.Length==0) { returnb.ToList } if(b==null||b.Length==0) { returna.ToList } Array.Sort(a);//如果数组是有序的话这句就不用啦! Array.Sort(b);//如果数组是有序的话这句就不用啦! if(a[0]>b[0]) { c.Add(b[0]); j++; } else { c.Add(a[0]); i++; } while(i { if(a[i] { c.Add(a[i]); i++; } else { c.Add(b[j]); j++; } } while(i { c.Add(a[i]); i++; } while(j { c.Add(b[j]); j++; } returnc; } 4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用) publicstaticList { List list1.Add(ar[0]); for(inti=0;i { intj=0; while(j { if(ar[i]==list1[j]) { break; } else { j++; } } if(j==list1.Count()) { list1.Add(ar[i]); } } returnlist1; } 5.一个无序数组先排序再去重。 (比如: 输入: 2143517645,得到1234567) publicstaticint[]distill(int[]ar) { if(ar.Length>0) { Array.Sort(ar); intsize=1; for(inti=1;i { if(ar[i]! =ar[i-1]) { size++; } } int[]temp=newint[size]; intk=0; temp[k++]=ar[0]; for(intj=1;j { if(ar[j-1]! =ar[j]) { temp[k++]=ar[j]; } } returntemp; } else returnnull; } 6.字符串比较去重,删除string2中和string1中相同的字符。 (是这样子滴: string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是: string3=sfhmnds) publicstaticList { List if(sta==null||sta.Length==0) { for(intl=0;l { ar.Add(stb[l].ToString()); } returnar; } if(stb==null||stb.Length==0) returnar; else { for(intj=0;j { inti=0; while(i { if(stb[j]==sta[i]) { break; } else { i++; } } if(i==sta.Length) { ar.Add(stb[j].ToString()); } } returnar; } } 6.不让你直接用库函数split来实现字符串的分割(比如: 用”sp”来分割”goodspmorning”,之后会得到”goodmorning”) publicstaticstring[]mysplit(stringstrinput,stringsp) { stringtmp=""; intstrlen=0,splen=0; intfound=0; string[]rt=null; if(strinput==null||sp==null||strinput.Length==0) returnnull; if(sp.Length==0) returnnewstring[]{strinput}; ArrayListtmp3=newArrayList(); strlen=strinput.Length; splen=sp.Length; for(inti=0;i { found=strinput.IndexOf(sp,i); if(found>=0) { tmp=""; tmp=strinput.Substring(i,found-i); tmp3.Add(tmp); i=found+splen-1; } else { stringtmp2=""; tmp2=strinput.Substring(i); if(tmp2! ="") tmp3.Add(tmp2); break; } } tmp3.TrimToSize(); rt=(string[])tmp3.ToArray(typeof(String)); tmp3.Clear(); returnrt; } 7.把一个由数字组成的字符串变成整数(像这样string=”123”变成int型的123(一百二十三)) publicstaticinttrans(stringstr) { if(str==null||str.Length==0) { return-1; } intresult=0; for(intj=0;j { if(0<=(str[j]-'0')&&(str[j]-'0')<=9) { checked { result=result*10+(str[j]-'0'); } } else { return-1; } } returnresult; } 8.二叉树的东西(这个写起来还蛮费劲的,应该不会考到吧~呵呵~~) 二叉查找树 //二叉查找树节点Binarysearchtreenode publicclassBinarySearchTreeNode {publicintkey;//二叉查找树节点的值 publicBinarySearchTreeNodeleft;//二叉查找树节点的左子节点 publicBinarySearchTreeNoderight;//二叉查找树节点的右子节点 ///二叉查找树节点构造函数 publicBinarySearchTreeNode(intnodeValue) {key=nodeValue;//nodeValue节点的值 left=null;right=null; } ///插入节点 publicvoidInsertNode(BinarySearchTreeNodenode) {if(node.key>this.key) {if(this.right==null) {this.right=node;//node插入的节点 return; } else this.right.InsertNode(node); } else {if(this.left==null) {this.left=node;return;} else this.left.InsertNode(node); } } ///二叉查找树查询 publicboolSearchKey(intsearchValue) {if(this.key==searchValue)//searchValue需要查询的值 returntrue;//是否找到查询的值 if(searchValue>this.key) {if(this.right==null)returnfalse; else returnthis.right.SearchKey(searchValue); } else {if(this.left==null)returnfalse; else returnthis.left.SearchKey(searchValue); } } //中序遍历 publicvoidMiddleDisplay() {if(this.left! =null) this.left.MiddleDisplay(); Console.WriteLine(this.key); if(this.right! =null) this.right.MiddleDisplay(); } //前序遍历 publicvoidFrontDisplay() {Console.WriteLine(this.key); if(this.left! =null) this.left.FrontDisplay(); if(this.right! =null) this.right.FrontDisplay(); } //后序遍历 publicvoidBehindDisplay() {if(this.left! =null) this.left.BehindDisplay(); if(this.right! =null) this.right.BehindDisplay(); Console.WriteLine(this.key); } } ///二叉查找树 publicclassBinarySearchTree {privateBinarySearchTreeNoderoot; ///生成一个二叉查找树 publicBinarySearchTree() {root=null;} ///生成一个二叉查找树 /// publicBinarySearchTree(intnodeValue) {root=newBinarySearchTreeNode(nodeValue);} ///在二叉查找树上插入一个节点 /// publicvoidInsertBinarySearchTreeNode(intnodeValue) {BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue); if(root==null) {root=insertNode; return; } else root.InsertNode(insertNode); return; } ///在二叉查找树上查询一个数 /// /// publicboolSearchKey(intsearchValue) {if(root.key==searchValue)returntrue; else returnroot.SearchKey(searchValue); } ///二叉查找树中序遍历 publicvoidMiddleDisplay() {root.MiddleDisplay();return;} ///二叉查找树前序遍历 publicvoidFrontDisplay() {root.FrontDisplay();return;} ///二叉查找树后序遍历 publicvoidBehindDisplay() {root.BehindDisplay();return;} ///二叉查找树排序 /// publicstaticvoidBinarySearchTreeSort(int[]a) {BinarySearchTreet=newBinarySearchTree(); for(inti=0;i t.InsertBinarySearchTreeNode(a[i]); t.MiddleDisplay();return; }///二叉查找树查找 /// /// publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey) {BinarySearchTreet=newBinarySearchTree(); for(inti=0;i t.InsertBinarySearchTreeNode(a[i]); returnt.SearchKey(searchKey); } } namespace二叉树 {classNode {intn; publicNode(intx) {n=x;} publicNodeLeft; publicNodeRight; publicvoidInsert(Nodenode) {if(node.n>this.n) {if(this.Right==null) this.Right=node; else this.Right.Insert(node);} else {if(this.Left==null) {this.Left=node;} else {this.Left.Insert(node);}}}//递归 publicvoidShow() {Console.WriteLine(n);}} classBinaryTree {Noderoot; publicvoidGenerateTree(Nodenode)//高内聚,低耦合 {if(root==null) {root=node;return;}//如果树是空,第一次加节点 root.Insert(node); } publicvoidShowInOrder(Nodenode)//中序遍历(inorder): 左中右。 先(前)序遍历(preorder): 中左右。 后序遍历(postorder): 左右中。 {if(node==null)return;//递归必须有个终止条件,递归方法中一定要接受参数 ShowInOrder(node.Left); node.Show(); ShowInOrder(node.Right); } publicvoidShow() {ShowInOrder(root);} } classA {staticvoidMain() {BinaryTreeb=newBinaryTree(); Nodenode=newNode(5); b.GenerateTree(node); node=newNode(13); b.GenerateTree(node); node=newNode(6); b.GenerateTree(node); node=newNode(26); b.GenerateTree(node); node=newNode(7); b.GenerateTree(node); b.Show();}}}结果: 5,6,7,13,26
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 微软 面试 常用 算法 总结 C# 编写