微软面试题深度剖析从实战角度解析常见问题解答思路.docxVIP

微软面试题深度剖析从实战角度解析常见问题解答思路.docx

本文档由用户AI专业辅助创建,并经网站质量审核通过
  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
  4. 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
  5. 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们
  6. 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
  7. 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多

第PAGE页共NUMPAGES页

微软面试题深度剖析从实战角度解析常见问题解答思路

一、编程题(共5题,每题10分)

1.题目:

实现一个函数,输入一个字符串,返回该字符串中所有字符的唯一排列组合。例如,输入abc,返回[abc,acb,bac,bca,cab,cba]。

答案:

cpp

includeiostream

includevector

includestring

includealgorithm

voidpermuteHelper(std::strings,intstart,std::vectorstd::stringresult){

if(start==s.size()-1){

result.push_back(s);

return;

}

for(inti=start;is.size();++i){

std::swap(s[start],s[i]);

permuteHelper(s,start+1,result);

std::swap(s[start],s[i]);//回溯

}

}

std::vectorstd::stringpermute(std::strings){

std::vectorstd::stringresult;

if(s.empty())returnresult;

permuteHelper(s,0,result);

returnresult;

}

intmain(){

std::stringinput=abc;

std::vectorstd::stringpermutations=permute(input);

for(constautoperm:permutations){

std::coutperm;

}

return0;

}

解析:

采用回溯算法,通过交换字符生成所有排列。关键点在于递归时固定一个字符,然后遍历剩余字符进行交换,最后回溯到初始状态。时间复杂度为O(n!),适合小规模输入。

2.题目:

实现一个LRU(LeastRecentlyUsed)缓存,支持get和put操作。LRU缓存限制为固定大小,当缓存满时,淘汰最久未使用的元素。

答案:

cpp

includeunordered_map

includelist

classLRUCache{

public:

LRUCache(intcapacity):capacity_(capacity){}

intget(intkey){

autoit=cache_map.find(key);

if(it==cache_map.end())return-1;

cache_list.splice(cache_list.begin(),cache_list,it-second);

returnit-second-second;

}

voidput(intkey,intvalue){

autoit=cache_map.find(key);

if(it!=cache_map.end()){

it-second-second=value;

cache_list.splice(cache_list.begin(),cache_list,it-second);

}else{

if(cache_map.size()==capacity_){

intold_key=cache_list.back().first;

cache_map.erase(old_key);

cache_list.pop_back();

}

cache_list.push_front({key,value});

cache_map[key]=cache_list.begin();

}

}

private:

intcapacity_;

std::liststd::pairint,intcache_list;//key-valuepair

std::unordered_mapint,std::liststd::pairint,int::iteratorcache_map;

};

intmain(){

LRUCachelru(2);

lru.put(1,1);

lru.put(2,2);

std::coutlru.get(1)std::endl;//1

lru.put(3,3);//evictskey2

std::coutlru.get(2)std::endl;//-1(no

您可能关注的文档

文档评论(0)

137****0700 + 关注
实名认证
文档贡献者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档