微软面试常用算法总结C#编写Word文件下载.docx
- 文档编号:21629461
- 上传时间:2023-01-31
- 格式:DOCX
- 页数:15
- 大小:18.37KB
微软面试常用算法总结C#编写Word文件下载.docx
《微软面试常用算法总结C#编写Word文件下载.docx》由会员分享,可在线阅读,更多相关《微软面试常用算法总结C#编写Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
i<
str.Length;
i++)
s.Push(charCompare[i]);
q.Enqueue(charCompare[i]);
if(s.Pop()!
=q.Dequeue())
justfy=false;
returnjustfy;
3.两个无序数组的合并(如果是字符串数组的话把int[]改成string[]就可以啦!
publicstaticList<
int>
merge(int[]a,int[]b)
List<
c=newList<
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<
a.Length&
&
b.Length)
if(a[i]<
b[j])
c.Add(a[i]);
c.Add(b[j]);
a.Length)
while(j<
returnc;
4.一个数组的去重(比如122334455变成12345,这个可以用来在两个有序或无序数组合并之后做去重用)
publicstaticList<
distll(int[]ar)
list1=newList<
list1.Add(ar[0]);
ar.Length;
list1.Count())
if(ar[i]==list1[j])
break;
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;
if(ar[i]!
=ar[i-1])
size++;
int[]temp=newint[size];
intk=0;
temp[k++]=ar[0];
if(ar[j-1]!
=ar[j])
temp[k++]=ar[j];
returntemp;
returnnull;
6.字符串比较去重,删除string2中和string1中相同的字符。
(是这样子滴:
string1=abc,string2=safbhcmnads,那么去重之后我要得到的东西是:
string3=sfhmnds)
string>
distill(stringsta,stringstb)
ar=newList<
if(sta==null||sta.Length==0)
for(intl=0;
l<
stb.Length;
l++)
ar.Add(stb[l].ToString());
returnar;
if(stb==null||stb.Length==0)
for(intj=0;
sta.Length)
if(stb[j]==sta[i])
if(i==sta.Length)
ar.Add(stb[j].ToString());
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)
if(sp.Length==0)
returnnewstring[]{strinput};
ArrayListtmp3=newArrayList();
strlen=strinput.Length;
splen=sp.Length;
strlen;
found=strinput.IndexOf(sp,i);
if(found>
=0)
tmp="
tmp=strinput.Substring(i,found-i);
tmp3.Add(tmp);
i=found+splen-1;
stringtmp2="
tmp2=strinput.Substring(i);
if(tmp2!
="
tmp3.Add(tmp2);
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;
if(0<
=(str[j]-'
0'
)&
(str[j]-'
)<
=9)
checked
result=result*10+(str[j]-'
);
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;
this.left.InsertNode(node);
///二叉查找树查询
publicboolSearchKey(intsearchValue)
{if(this.key==searchValue)//searchValue需要查询的值
returntrue;
//是否找到查询的值
if(searchValue>
{if(this.right==null)returnfalse;
returnthis.right.SearchKey(searchValue);
{if(this.left==null)returnfalse;
returnthis.left.SearchKey(searchValue);
//中序遍历
publicvoidMiddleDisplay()
{if(this.left!
=null)
this.left.MiddleDisplay();
Console.WriteLine(this.key);
if(this.right!
this.right.MiddleDisplay();
//前序遍历
publicvoidFrontDisplay()
{Console.WriteLine(this.key);
if(this.left!
this.left.FrontDisplay();
this.right.FrontDisplay();
//后序遍历
publicvoidBehindDisplay()
this.left.BehindDisplay();
this.right.BehindDisplay();
///二叉查找树
publicclassBinarySearchTree
{privateBinarySearchTreeNoderoot;
///生成一个二叉查找树
publicBinarySearchTree()
{root=null;
///<
paramname="
nodeValue"
>
二叉查找树根节点的值<
/param>
publicBinarySearchTree(intnodeValue)
{root=newBinarySearchTreeNode(nodeValue);
///在二叉查找树上插入一个节点
插入节点的值<
publicvoidInsertBinarySearchTreeNode(intnodeValue)
{BinarySearchTreeNodeinsertNode=newBinarySearchTreeNode(nodeValue);
if(root==null)
{root=insertNode;
return;
root.InsertNode(insertNode);
return;
///在二叉查找树上查询一个数
searchValue"
需要查询的值<
returns>
是否找到查询的值<
/returns>
{if(root.key==searchValue)returntrue;
returnroot.SearchKey(searchValue);
///二叉查找树中序遍历
{root.MiddleDisplay();
///二叉查找树前序遍历
{root.FrontDisplay();
///二叉查找树后序遍历
{root.BehindDisplay();
///二叉查找树排序
a"
需要排序的数组<
publicstaticvoidBinarySearchTreeSort(int[]a)
{BinarySearchTreet=newBinarySearchTree();
for(inti=0;
a.Length;
i++)
t.InsertBinarySearchTreeNode(a[i]);
t.MiddleDisplay();
return;
}///二叉查找树查找
进行查找的数组<
searchKey"
需要查找的树<
publicstaticboolBinarySearchTreeSearch(int[]a,intsearchKey)
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;
this.Right.Insert(node);
{if(this.Left==null)
{this.Left=node;
{this.Left.Insert(node);
}}}//递归
publicvoidShow()
{Console.WriteLine(n);
}}
classBinaryTree
{Noderoot;
publicvoidGenerateTree(Nodenode)//高内聚,低耦合
{if(root==null)
{root=node;
}//如果树是空,第一次加节点
root.Insert(node);
publicvoidShowInOrder(Nodenode)//中序遍历(inorder):
左中右。
先(前)序遍历(preorder):
中左右。
后序遍历(postorder):
左右中。
{if(node==null)return;
//递归必须有个终止条件,递归方法中一定要接受参数
ShowInOrder(node.Left);
node.Show();
ShowInOrder(node.Right);
{ShowInOrder(root);
}
classA
{staticvoidMain()
{BinaryTreeb=newBinaryTree();
Nodenode=newNode(5);
b.GenerateTree(node);
node=newNode(13);
node=newNode(6);
node=newNode(26);
node=newNode(7);
b.Show();
}}}结果:
5,6,7,13,26
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 微软 面试 常用 算法 总结 C# 编写
![提示](https://static.bdocx.com/images/bang_tan.gif)