集册 Java实例教程 使用经典的三个反转旋转数组

使用经典的三个反转旋转数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

525
使用经典的三个反转旋转数组


//package com.nowjava;


public class Main {/*来自 n o w    j a v a  . c o m*/

    /**

     * Rotates array using classic three reversals

     * @param list

     * @param k

     */

    public static void rotateArrayReversal(Character[] list, int k) {

        int n = k % list.length;

        reverseArrayFromToPosition(list, 0, list.length - 1);

        reverseArrayFromToPosition(list, 0, n - 1);

        reverseArrayFromToPosition(list, n, list.length - 1);

    }


    public static void reverseArrayFromToPosition(Character[] list,

            int fromPos, int toPos) {

        //System.out.println("from Pos: " + fromPos + " to Pos" + toPos);

        int numIterations = (int) Math.ceil((toPos - fromPos) / 2.0);

        for (int x = fromPos; x < fromPos + 
展开阅读全文