2014-05-10 07:06
原题:
Suppose you have a collection of collection
Eg : CEO-> Vps-> GMs ->..
CEO will contain collection of VP's, VP's will have collection of GM's and so on.
Suppose you need to find a particular GM is the alias is given. Write a linq query to get the employee details if the employee alias is given.
Hint : Use Recursion + Linq
题目:如果CEO手下有很多总监,总监手下有很多总经理,依此类推。那么通过一个名字和一个头衔,要如何找到符合条件的人?用递归和LINQ实现。
解法:这明显是C#问题吧。C#我勉强懂点基本语法,LINQ则完全不会,所以我只能用递归了。
代码:
// http://www.careercup.com/question?id=4840369632051200
// I believe this is not a typical interview question. You must've told them you know LINQ, before you're given this one.
// I've never learnt LINQ before, only basic knowledge of C# is not enough to solve this problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CSharp
{
class Person
{
public string title;
public string alias;
public List<Person> subordinate; public Person(string title, string alias)
{
this.title = title;
this.alias = alias;
subordinate = new List<Person>();
}
} class Solution
{
Person search(string title, string alias, Person source) {
if (source.title == title) {
return source.alias == alias ? source : null;
} Person result;
foreach(Person sub_source in source.subordinate)
{
result = search(title, alias, sub_source);
if (result != null)
{
return result;
}
} return null;
}
}
}