首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在java中旋转数组的一部分

在java中旋转数组的一部分
EN

Stack Overflow用户
提问于 2018-07-27 05:10:47
回答 1查看 176关注 0票数 0

我见过很多关于如何旋转数组的例子,比方说:

如果我旋转1次,[0,1,2,3,4]就会变成[4,0,1,2,3]

但是,如果我想要获得初始和最终位置,并且只旋转该部分,该怎么办?假设我有一个数组

代码语言:javascript
运行
复制
[0,1,2,3,4,5]

我想从array[1]旋转到array[4] 2次。结果将是:

代码语言:javascript
运行
复制
[0,3,4,1,2,5]

请注意,array[0] = 0array[4] = 5根本没有改变位置。

我一直在使用和玩弄John Kurlak的杂耍算法(它在Github上是公开的),但我不能让它工作。

有谁能给我指个方向吗?我找不到任何关于如何做到这一点的信息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-27 05:27:07

请使用下面修改后的版本来执行您需要的操作

代码语言:javascript
运行
复制
import java.util.Arrays;

/**
 * This program rotates all of the elements in an array left by a given k
 * value.  It runs in O(n) time and uses O(1) additional space (it operates
 * in-place).
 *
 * @author      John Kurlak <john@kurlak.com>
 * @date        5/30/2013
 */
public class JugglingAlgorithm {
    /**
     * Runs the program with an example array.
     *
     * @param args      The command-line arguments.
     */
    public static void main(String[] args) {
        int[] array = new int[] { 0,1,2,3,4,5 };
        int k = 2;

        System.out.println(Arrays.toString(array));
        System.out.println("rotated to the left " + k + " is:");
        rotateArrayLeft(array, k, 1, 4);
        System.out.println(Arrays.toString(array));
    }

    /**
     * Rotates all of the elements in an array left by the given k value.
     * If k is negative, it rotates the elements in the array right.
     * This method modifies the array in place, so it does not return
     * anything.
     *
     * @param array The array to shift.
     * @param k     The number of indices by which to shift the array.
     */
    public static void rotateArrayLeft(int[] array, int k, int minIndex, int maxIndex) {
        if (array == null) {
            return;
        }

        int n = maxIndex - minIndex + 1;

        // Handle negative k values (rotate to right)
        if (k < 0) {
            k = n - Math.abs(k);
        }

        // Ensure k is in interval [0, n)
        k = ((k % n) + n) % n;

        // Perform juggling algoritm
        for (int i = 0, gcd = gcd(k, n); i < gcd; i++) {
            int temp = array[i+minIndex];
            int j = i;

            while (true) {
                int p = j + k;

                if (p >= n) {
                    p = p - n;
                }

                if (p == i) {
                    break;
                }

                array[j +minIndex] = array[p+minIndex];
                j = p;
            }

            array[j +minIndex] = temp;
        }
    }

    /**
     * Uses Euclid's algorithm to find the greatest common divisor of
     * two numbers.
     *
     * @param a     The first number.
     * @param b     The second number.
     * @returns     The great common divisor of `a` and `b`.
     */
    public static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            return gcd(b, a % b);
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51547548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档