需要基于输入位数

问题描述:

需要扭转基于输入数 例如整数数组扭转的整数数组:需要基于输入位数

Array be [1,2,3,4,5,6,7,8,9] and value of subset be N = 4
then the array after reversal will be [4, 3, 2, 1, 8, 7, 6, 5, 9].
if N = 3 then output should be [3,2,1,6,5,4,9,8,7]

无法与递归方法认为适当的逻辑的或下面的java代码任何循环概念

+0

你会如何反转整个数组(当N =数组长度时)? – Pshemo

+0

N是索引还是数组中的值? – klutt

+0

@klutt我认为他需要扭转所有的大小正好'4' –

尝试

public static int[] reverse(int[] a, int number) { 
    // negative case 
    if (number < 0) { 
     return a; 
    } 
    // higher count than array length 
    int size = number < a.length ? number : a.length; 
    int[] b = new int[size]; 
    System.arraycopy(a, 0, b, 0, size); 
    for (int i = b.length - 1; i >= 0; i--) { 
     a[b.length - 1 - i] = b[i]; 
    } 
    return a; 
} 

阵列具有与每个索引上述循环代码元素简单高效。

+0

非常感谢。这是我寻找的。 – SurajSr

+0

谢谢,你可以投我的答案 –