Java Integer Array Permutation nextPermutation(int v)

Here you can find the source of nextPermutation(int v)

Description

Permutates the bits with value 1 in bitstring v, return next permutation

License

Open Source License

Parameter

Parameter Description
v a parameter

Declaration

public static int nextPermutation(int v) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*ww  w . jav  a  2 s . co  m*/
     * Permutates the bits with value 1 in bitstring <code>v</code>, return next permutation
     *
     * @param v
     * @return
     */
    public static int nextPermutation(int v) {
        if (v == 0)
            return 0;
        else {
            int t = (v | (v - 1)) + 1;
            return t | ((((t & -t) / (v & -v)) >> 1) - 1);
        }
    }
}

Related

  1. nextPermutation(int[] arr)
  2. nextPermutation(int[] is)
  3. nextPermutation(int[] next)
  4. nextPermutation(int[] nums)