旋转数组并查找最大元素的索引

时间:2018-03-01 20:05:26

标签: java arrays

我正在开发一个程序,我需要找到一个数组的索引位置,该数组在对该数组进行左旋转后保持该数组中的最大值。

例如:

如果数组是{5,7,1,8,2}。此数组最大值为8.如果我将数组旋转2次,则结果为1,8,2,5,7。那么max元素8的索引位置是1。

如果我将数组{5,7,1,8,2}旋转6次,则数组变为7,1,8,2,5。现在8的指数是2。

我的程序将数组和旋转次数作为输入,并返回一个数组,该数组包含旋转数组时max元素的索引位置。

int[] rotate(int[] array, int[] rotate) {
    int[] result = new int[rotate.length];
    int index = 0;
    int large = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] > large) {
            large = array[i];
            index = i;
        }
    }
    for (int i = 0; i < rotate.length; i++) {
        int r = rotate[i];
        if (index - r < 0) {
            if (r > array.length) {
                r = r % array.length;
                result[i] = index - r;
            } else {
                result[i] = index - (index - r);
            }
        } else {
            result[i] = index - r;
        }
    }
    return result;
}

该程序在某些情况下有效但未能找到其他我无法找到的测试用例。

你可以帮助我在这段代码中犯错吗?

3 个答案:

答案 0 :(得分:1)

您可以使用以下功能。看到它正常工作here

int[] rotate(int[] array, int[] rotate) 
{
    int[] result = new int[rotate.length];
    int index = 0;
    int large = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] > large) {
            large = array[i];
            index = i;
        }
    }
    int len = array.length;
    for (int i = 0; i < rotate.length; i++) {
         int r = (index - (rotate[i]%len));
         result[i] = (r>=0) ? r : (len+r); 
    }
    return result;
}

以下是完整的代码:

class Test
{
    static int[] rotate(int[] array, int[] rotate) 
    {
        int[] result = new int[rotate.length];
        int index = 0;
        int large = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > large) {
                large = array[i];
                index = i;
            }
        }
        int len = array.length;
        for (int i = 0; i < rotate.length; i++) {
             int r = (index - (rotate[i]%len));
             result[i] = (r>=0) ? r : (len+r); 
        }
        return result;
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        int nums[] = {5,7,1,8,2};
        int r[] = {2, 6, 5, 1, 2, 3, 4, 5, 0};

        int res[] = rotate(nums, r);

        for(int i=0; i<res.length; i++)
        {
            System.out.println(r[i] + " = "+ res[i]);
        }
    }
}

<强>输出

2 = 1
6 = 2
5 = 3
1 = 2
2 = 1
3 = 0
4 = 4
5 = 3
0 = 3

答案 1 :(得分:0)

  1. 获取最大数字的索引,代码可以正常工作

    int index = 0;
    int large = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] > large) {
            large = array[i];
            index = i;
        }
    }
    
  2. 旋转数组

    //Rotate the array until the maximum value is the first index
    for (int rotations = 0; rotations < index; rotations++) {
        int temp = rotate[0]; //Store the first value of the array in a temporary value
        for (int i = 1; i < rotate.length; i++) {
            rotate[i - 1] == rotate[i]; //move each value one index to the left, replacing the old value at i with the one next to it
        }
        rotate[rotate.length - 1] = temp; //replace the last index of the array with the stored temp value ("Rotating it left")
    }
    return rotate; //return the new array which is now rotated
    
  3. 另请注意,在java中,您不需要返回数组值,只需将其传递给方法,更改值,原始数组将与您在此方法中修改的数组相同。这方面的一个例子是......

    Arrays.sort(array);
    System.out.println(array);
    //or
    Collections.shuffle(Arrays.asList(array));
    System.out.println(array);
    

答案 2 :(得分:0)

您可以使用流来查找索引,然后按每次轮换偏移它:

int[] rotate(int[] array, int[] rotate) {
    int len = array.length;

    // find the index of the greatest element
    int index = IntStream.range(0, len)
            .reduce(Integer.MIN_VALUE, (a, b) -> array[a] > array[b] ? a : b);

    // offset the index by each rotation
    return Arrays.stream(rotate)
            .map(r -> (index + len + (r % len)) % len)
            .toArray();
}