others

  • next_permutation
    static boolean next_permutation(int[]as) {
    	int n = as.length;
    	for (int i = n - 1; i > 0; i--) {
    		if (as[i - 1] < as[i]) {
    			int j = n;
    			while (as[i - 1] >= as[--j]);
    			swap(as, i - 1, j);
    			reverse(as, i, n);
    			return true;
    		}
    	}
    	return false;
    }
    static void swap(int[] is, int i, int j) {
    	int t = is[i];
    	is[i] = is[j];
    	is[j] = t;
    }
    static void reverse(int[] is, int s, int t) {
    	while (s < --t) swap(is, s++, t);
    }
最終更新:2014年04月10日 23:21