Sword Point offer series---joseph ring problem

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.