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则完全不会,所以我只能用递归了。
代码:
1 // http://www.careercup.com/question?id=4840369632051200 2 // I believe this is not a typical interview question. You must've told them you know LINQ, before you're given this one. 3 // I've never learnt LINQ before, only basic knowledge of C# is not enough to solve this problem. 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace CSharp 11 { 12 class Person 13 { 14 public string title; 15 public string alias; 16 public List<Person> subordinate; 17 18 public Person(string title, string alias) 19 { 20 this.title = title; 21 this.alias = alias; 22 subordinate = new List<Person>(); 23 } 24 } 25 26 class Solution 27 { 28 Person search(string title, string alias, Person source) { 29 if (source.title == title) { 30 return source.alias == alias ? source : null; 31 } 32 33 Person result; 34 foreach(Person sub_source in source.subordinate) 35 { 36 result = search(title, alias, sub_source); 37 if (result != null) 38 { 39 return result; 40 } 41 } 42 43 return null; 44 } 45 } 46 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)