杭州网站设计手机,做地方生活网站,移动网站建设报价表,深圳微商城网站设计电话这道题在算法课上的一个小故事上有一个类似的#xff0c;就是一个军官打了败仗#xff0c;带着他的几个兵逃到一个山洞#xff0c;他们不想当俘虏想自杀#xff0c;但是军官不想自杀但是又不好意思走#xff0c;于是军官想了个办法#xff0c;他们几个人围成一个圈#… 
这道题在算法课上的一个小故事上有一个类似的就是一个军官打了败仗带着他的几个兵逃到一个山洞他们不想当俘虏想自杀但是军官不想自杀但是又不好意思走于是军官想了个办法他们几个人围成一个圈每次枪毙第5个然后从下一个往下数5个最后一个人自杀。只要军官站在第20个的位置上他就可以留到最后然后自己一个人走。 
一开始想用循环链表这样就可以按照题目的定义进行循环直到最后剩1个但是用循环链表还得自己写结构体定义最后就用了LinkedListindex表示从哪个位置开始算delete表示要删除的位置最后两个样例过了其他示例超时了。 
class Solution {public int lastRemaining(int n, int m) {LinkedListInteger num  new LinkedList();for(int i 0;in;i){num.add(i);}int index  0;while(num.size() ! 1){int delete  index  m-1;int size  num.size();delete  delete % size;num.remove(delete);indexdelete;}return num.peek();}
} 然后自己又想了一会没思路就直接看题解了题解这个递归都让我看了将近20分钟才看懂但是看懂了就觉得好简单没看懂就一直理解不了。 
定义一个递归函数f(int n, int m),他的返回值是一个int表示最后留下的是最后留下的元素的序号对于一个长度为n的序列我们第一次先删除m%n个元素然后递归的求解出剩下的n-1个元素最后会剩下的那个元素的序号记为xint x  fn-1, m; 
也就是说当我们删除n个元素中第m%n个元素后剩下的n-1个元素如果从第1个开始算最后会剩下第x个元素但是我们不是从第1个开始算的我们是从第m%n个元素开始算的所以最后剩下的是第m%nx个元素以防越界最后再%n也就是第m%nx%n个元素递归必须有终止条件这道题的终止条件就是当n等于1的时候返回第0个元素。 
class Solution {public int lastRemaining(int n, int m) {return f(n, m);}public int f(int n, int m){if(n  1){return 0;}int x  f(n-1, m);return (m%n  x) % n;}
} 文章转载自: http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.rhdln.cn.gov.cn.rhdln.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.clpdm.cn.gov.cn.clpdm.cn http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn http://www.morning.ghfmd.cn.gov.cn.ghfmd.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.bkylg.cn.gov.cn.bkylg.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.xtkw.cn.gov.cn.xtkw.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.knswz.cn.gov.cn.knswz.cn http://www.morning.nfgbf.cn.gov.cn.nfgbf.cn http://www.morning.wpmlp.cn.gov.cn.wpmlp.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.uytae.cn.gov.cn.uytae.cn http://www.morning.qgzmz.cn.gov.cn.qgzmz.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.ntgsg.cn.gov.cn.ntgsg.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.sqqpb.cn.gov.cn.sqqpb.cn http://www.morning.klrpm.cn.gov.cn.klrpm.cn http://www.morning.dkqr.cn.gov.cn.dkqr.cn http://www.morning.jcyyh.cn.gov.cn.jcyyh.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.rgmls.cn.gov.cn.rgmls.cn http://www.morning.yhsrp.cn.gov.cn.yhsrp.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.wjlrw.cn.gov.cn.wjlrw.cn http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.xwgbr.cn.gov.cn.xwgbr.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.qxltp.cn.gov.cn.qxltp.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.qqnjr.cn.gov.cn.qqnjr.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn