"title" 0, 1, ... N in a circle, starting from 0, each time the number of M is deleted, the last number of circles is calculated.
* "idea" 1 simulates a circle with an array. When the number to the last number is index==n, so that index==0 began to traverse;
* When encountering a number that has been deleted Nums[index]==-1, skip to continue;
* When you go to the specified number of M, delete it, nums[index]=-1;
1 PackageCom.exe10.offer;2 3 /**4 * "title" 0, 1, ... N in a circle, starting from 0, each time the number of M is deleted, the last number of circles is calculated. 5 * "idea" 1 simulates a circle with an array. When the number to the last number is index==n, so that index==0 began to traverse;6 * When encountering a number that has been deleted Nums[index]==-1, skip to continue;7 * When you go to the specified number of M, delete it, nums[index]=-1;8 * @authorWGS9 *Ten */ One Public classlastnumberincircle { A - Public intLastremainincircle (intNintm) { - if(m<1 | | n<1)return-1; the int[] nums=New int[n]; - intIndexinnums=-1; - intcount=0;//Step-Count device - intLength=N; + while(length>0){ -indexinnums++; + //1 A if(indexinnums==N) atIndexinnums=0; - //2 - if(Nums[indexinnums]==-1) - Continue; -count++;//pedometer + +; - //3 in if(count==L) { -Nums[indexinnums]=-1; toCount=0; +length--; - } the } * returnindexinnums; $ Panax Notoginseng } - the + Public Static voidMain (string[] args) { A intL =NewLastnumberincircle (). Lastremainincircle (5, 3); the System.out.println (l); + } - $}
Sword Point offer series---joseph ring problem