[Offer] [62] [circle] last remaining digital

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.

Cattle brush off questions address network

Ideas analysis

  1. Linked list to store the data, the length of each of the I fetch cycle to achieve:
    • LinkedList all numbers into the list (LinkedList deletions operation is more suitable than ArrayList). Remove assumed that the current node index is removeIndex, the next index node to be deleted are: (removeIndex + m-1)% list.size (), the type of cycle may be achieved by modulo operation symbol
    • Note: no need to use circular list, but will be more trouble.
  2. Mathematical derivation of the law
      n digits of a circle, constantly delete the first m digits, we put the last remaining figures denoted f(n,m).
      a first n digits deleted number is (m-1)% n, we referred to as K, k=(m-1)%n.
      Then the remaining n-1 digits becomes: 0,1,……k-1,k+1,……,n-1We next round of the first number at the top, and that an array of length n-1 is mapped to 0 ~ n-2.
      Original figures: k+1,……, n-1,0, 1,……k-1
      mapping Digital: 0,……,n-k-2, n-k-1, n-k,……n-2
      mapping a digital denoted as x, the original digital referred to as y, then map numbers back to the original digital formula for y=(x+k+1)%n。
      the mapping of numbers, n-1 numbers, and constantly remove the m-th digit, can be defined by know, the last remaining number is f(n-1,m). We put it back into the original digital by a formula can get the last remaining original number is (f(n-1,m)+k+1)%n, and this number is that is beginning we marked in f(n,m), so you can push the recursive formula is as follows:
              f(n,m) =(f(n-1,m)+k+1)%n       
      the k=(m-1)%nsubstitution, simplifying get:
              f(n,m) =(f(n-1,m)+m)%n
              f(1,m) = 0
      the code may be implemented using a recursive formula or recursive method loops. The time complexity is O (n), the spatial complexity is O (1).

Test Case

  1. Test Function: m input is less than n, such as from the first five digits of each circle remove the second and third digit; m inputs greater than or equal to n, such as from the first six digits of each deletion circle 6, 7 digits.
  2. Special input test: circle 0 digits.
  3. Performance Testing: Each delete the first 997 digits from the first 4,000 digits of the circle.

Java code

public class Offer062 {
    public static void main(String[] args) {
        test1();
        test2();
        test3();
        
        
    }

    public static int LastRemaining(int n, int m) {
        return Solution1(n, m);
    }

    
    /*
     * 方法一:采用推导出来的方法
     */
    public static int Solution1(int n, int m) {
        if(n<1 || m<1)
            return -1; //出错
        int last=0;
        for(int i=2;i<=n;i++){
            last=(last+m)% i;  //这里是i不是n!!!
        }
        return last;
    }
     
    /*
     * 方法二:采用链表来存放,每次对长度取余来实现循环
     */
    public static int Solution2(int n, int m) {
        if(n<1 || m<1)
            return -1; //出错
        LinkedList<Integer> list = new LinkedList<Integer>();
        for(int i=0;i<n;i++)
            list.add(i);
        int removeIndex=0;
        while(list.size()>1){
            removeIndex=(removeIndex+m-1)%list.size();
            list.remove(removeIndex);
        }
        return list.getFirst();
    }
    
    private static void test1() {

    }

    private static void test2() {

    }
    private static void test3() {

    }

}

Code link

Offer to prove safety codes -Java

Guess you like

Origin www.cnblogs.com/haoworld/p/offer62-yuan-quan-zhong-zui-hou-sheng-xia-de-shu-z.html