Arrays 我想使用临时数组进行数组旋转,下面是它的代码?有没有更好的方法使用临时数组来实现这一点?
在这里,我在一些临时数组的帮助下旋转数组。Arrays 我想使用临时数组进行数组旋转,下面是它的代码?有没有更好的方法使用临时数组来实现这一点?,arrays,data-structures,Arrays,Data Structures,在这里,我在一些临时数组的帮助下旋转数组。 有没有其他更好的方法来改进下面的数组旋转代码?我不熟悉数据结构,我如何找到这样的代码的时间复杂性 class Arrayrotation { public int[] inserttemp(int b[], int move, int len) { int c[] = new int[move]; for (int i = 0; i < move; i++) {
有没有其他更好的方法来改进下面的数组旋转代码?我不熟悉数据结构,我如何找到这样的代码的时间复杂性
class Arrayrotation {
public int[] inserttemp(int b[], int move, int len) {
int c[] = new int[move];
for (int i = 0; i < move; i++) {
c[i] = b[i];
//System.out.println(c[i]);
}
return c;
}
public int[] rotate(int b[], int move, int len, int d[]) {
int orglen = move;
int[] a = new int[len - orglen];
int k = 0;
for (int i = 0; i < b.length; i++) {
if (move < b.length) {
b[i] = b[move++];
} else if (move >= b.length) {
b[i] = d[k++];
}
}
System.out.println(Arrays.toString(b));
return b;
}
public static void main(String args[]) {
Arrayrotation a = new Arrayrotation();
int[] b = { 1,2,3,4,5,6,7,8,9};
int rotate = 3;
int[] d;
int[] f;
d = a.inserttemp(b, rotate, b.length);
a.rotate(b, rotate, b.length, d);
}
}
类排列{
公共int[]inserttemp(int b[],int move,int len){
int c[]=新int[move];
for(int i=0;i=b.length){
b[i]=d[k++];
}
}
System.out.println(Arrays.toString(b));
返回b;
}
公共静态void main(字符串参数[]){
排列a=新排列();
int[]b={1,2,3,4,5,6,7,8,9};
int=3;
int[]d;
int[]f;
d=a.插入温度(b.旋转,b.长度);
a、 旋转(b,旋转,b.长度,d);
}
}
类旋转光{
公共静态void main(字符串[]args){
//初始化数组
int[]arr=新的int[]{1,2,3,4,5};
//n确定阵列应旋转的次数。
int n=3;
//显示原始数组
System.out.println(“原始数组:”);
对于(int i=0;i0;j--){
//将数组的元素移位1
arr[j]=arr[j-1];
}
//数组的最后一个元素将添加到数组的开头。
arr[0]=最后一次;
}
System.out.println();
//显示旋转后生成的阵列
System.out.println(“右旋转后的数组:”);
对于(int i=0;i
- 原始阵列: 123445
- 右旋转后的阵列: 34512
class RotateRight {
public static void main(String[] args) {
//Initialize array
int [] arr = new int [] {1, 2, 3, 4, 5};
//n determine the number of times an array should be rotated.
int n = 3;
//Displays original array
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//Rotate the given array by n times toward right
for(int i = 0; i < n; i++){
int j, last;
//Stores the last element of array
last = arr[arr.length-1];
for(j = arr.length-1; j > 0; j--){
//Shift element of array by one
arr[j] = arr[j-1];
}
//Last element of array will be added to the start of array.
arr[0] = last;
}
System.out.println();
//Displays resulting array after rotation
System.out.println("Array after right rotation: ");
for(int i = 0; i< arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}