1, Title Description:
0,1,, n-1 which are arranged in a circle numbers n, starting from the number 0, delete the m-th digit each time from inside the circle. Find the circle where the last remaining digit.
For example, 0,1,2,3,4 five numbers in a circle, each deletion of three numbers starting from the number 0, the first four digits of the deleted sequence is 2,0,4,1, so the final the remaining number is 3.
2. Example:
Example 1:
Input: n = 5, m = 3
Output: 3
Example 2:
Input: n = 10, m = 17
Output: 2
restrictions:
. 1 <= n-<^ = 10. 5
. 1 <= m <= 10. 6 ^
3, solving ideas
(1) 0, ..., n-1 number is stored in the list set
(2) loops through the list the beginIndex (initial value 0) from the beginning to find the m-th element in the list of index value index
(3) deleting the m-th element list.remove (index)
(4) traversing the starting index list updated and recorded beginIndex variable
(5) repeating (2), (3), (4 ) step, list until only one element in
4, code implementation
Classes in java.util * Import;. class Solution { public int lastRemaining (n-int, int m) { IF (n-<m. 1 || <. 1) { return -1; } IF (n-==. 1) { return 0; } // store the original number the ArrayList <Integer> = new new List the ArrayList <> (); for (int I = 0; I <n-; I ++) { List.add (I); } int the beginIndex = 0; the while (n->. 1 ) { int size = list.size (); // Get deleted numbered index int index = (m% size the beginIndex + -. 1)% size; index = index == -1 ? size -1 : index; list.remove (index); the index number of the start time // beginIndex = index == size - 1 ? 0 : index; --n; } return list.get(0); } }