M位置的圆移位阵列的最快算法是什么?
例如,[3 4 5 2 3 1 4]班次M = 2个位置应该是[1 4 3 4 5 2 3].
非常感谢.
我编写了一个程序来查找给定项目列表的所有可能的排列.这恰恰意味着我的程序打印r = 0到n的所有可能的P(n,r)值
以下是代码:
package com.algorithm;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Permutations<T> {
public static void main(String args[]) {
Permutations<Integer> obj = new Permutations<Integer>();
Collection<Integer> input = new ArrayList<Integer>();
input.add(1);
input.add(2);
input.add(3);
Collection<List<Integer>> output = obj.permute(input);
int k = 0;
Set<List<Integer>> pnr = null;
for (int i = 0; i <= input.size(); i++) {
pnr = new HashSet<List<Integer>>();
for(List<Integer> integers : output){
pnr.add(integers.subList(i, integers.size()));
}
k = input.size()- i;
System.out.println("P("+input.size()+","+k+") :"+ …Run Code Online (Sandbox Code Playgroud) 因此,目标是正确地旋转数组中的元素a.举个例子; 如果a==2,然后 array = {0,1,2,3,4}会成为array = {3,4,0,1,2}
这就是我所拥有的:
for (int x = 0; x <= array.length-1; x++){
array[x+a] = array[x];
}
Run Code Online (Sandbox Code Playgroud)
但是,这无法解释何时[x+a]大于数组的长度.我读到我应该存储在不同阵列中更大的那些但是看到a变量我不确定这是最好的解决方案.提前致谢.