1 Title Description
2 ideas and methods
A thought :
Consider the endless chain solution; stl used to simulate the list circular linked list. When the list itself is not an annular structure, so whenever the iterator to the end of the scan chain, the head needs to move the iterator to the list, the list of deleted nodes erase method, the iterator will fail the original, it is necessary to hold the next node. Vector using analog or circular linked list.
Thinking two:
https://blog.csdn.net/SCS199411/article/details/92988332
Every rule is deleted digital analysis calculated directly circle last remaining digits. Implicit function f (n, m) of the n digits each time the m-th digit remove the last remaining figures.
Knowledge Point:
Some properties of the modulo operation need to know: a, I take a number of times equal to the time taken, for example, 5% 2 = 1, and 5% 2% 2% 2 = 1; two, modulo operation is associative, e.g. a% n - 1 = (a-1)% n (n> 1); three, if not [0, n-1] interval the number of x, there is the number of congruence of the interval for a clear and only one. For example, x = -2, -2% n = n-2.
3 C ++ core code
. 1 class Solution { 2 public : . 3 int LastRemaining_Solution ( int n-, int m) . 4 { . 5 // circular linked list . 6 // the STL is not only through the bad list, the list head to be redirected at each visit to the end of the list . 7 int LastRemaining_Solution ( int n-, int m) { . 8 IF (n-< . 1 || m < . 1 ) . 9 return 0 ; 10 . 11 List < int > the nums; 12 is for (int i = 0; i < n; ++i) { 13 nums.push_back(i); 14 } 15 16 list<int>::iterator cur = nums.begin(); 17 while (nums.size()>1){ 18 for (int i = 1; i < m; ++i) { 19 cur++; 20 if(cur == nums.end()) 21 cur = nums.begin(); 22 } 23 is List < int > :: = Iterator Next CUR ++; // at a storage node 24 IF (== Next nums.end ()) 25 Next = nums.begin (); 26 is cur-- ; 27 nums.erase (CUR); // delete the node 28 CUR = next; // to point to the next node 29 } 30 return * (CUR); 31 is } 32 };
1 class Solution { 2 public: 3 int LastRemaining_Solution(int n, int m) 4 { 5 if(n < 1 || m < 1) //守卫代码 6 return -1; 7 8 //int last = 0; 9 //for (int i = 2; i<=n; ++i){ 10 // last = (last+ m) % i; 11 //} 12 //return last; 13 14 if(n == 1) 15 return 0; 16 return (LastRemaining_Solution(n-1, m) + m) % n; 17 } 18 };
4 C ++ complete code
1 #include <iostream> 2 #include <vector> 3 #include <list> 4 5 using namespace std; 6 7 int LastRemaining_Solution(int n, int m); 8 9 int main() { 10 cout << LastRemaining_Solution(5, 3) << endl; 11 12 system("pause"); 13 return 0; 14 } 15 16 intLastRemaining_Solution ( int n-, int m) { . 17 IF (n-< . 1 || m < . 1 ) return - . 1 ; 18 is . 19 // used to simulate a circular linked list, vector 20 is vector < int > NUM; 21 is for ( int I = 0 ;! = n-I; I ++ ) { 22 is num.push_back (I); 23 is } 24 25 int Start = 0 ; 26 is the while (num.size ()> . 1 ) { 27 int cntDown = m; 28 int Music Videos = Start; 29 the while (- cntDown) { 30 // If traverse to the last digit, the next first number through the array 31 is IF (Music Videos == (num.size () - . 1 )) { 32 Music Videos = 0 ; 33 is } 34 is the else { 35 Music Videos ++ ; 36 } 37 [ } 38 is 39 IF (Music Videos == (num.size () - . 1 )) { 40 Start =0 ; 41 } 42 else { 43 start = the like; 44 } 45 num.erase (num.begin () + , etc.); 46 } 47 48 Return num [ 0 ]; 49 }
Reference material
https://blog.csdn.net/zjwreal/article/details/88917056
https://blog.csdn.net/m0_37950361/article/details/82154753
https://blog.csdn.net/SCS199411/article/details/92988332