简体   繁体   English

以奇数索引反转队列中的数字

[英]Reverse numbers in a queue at odd-indexes

This is aa review question(not asking for HW answers!!!) 这是一个复习问题(不要求硬件答案!!!)

Write a method reverseHalf that reverses the order of half of the elements of a Queue of integers. 编写一个reverseHalf方法,该方法可以反转整数队列中一半元素的顺序。 Your method should reverse the order of all the elements in odd-numbered positions (position 1, 3, 5, etc.) assuming that the first value in the queue has position 0. For example, if the queue originally stores this sequence of numbers when the method is called: 您的方法应该反转奇数位置(位置1、3、5等)中所有元素的顺序,假设队列中的第一个值的位置为0。例如,如果队列最初存储此数字序列调用该方法时:

index: 0 1 2 3 4 5 6 7 front [1, 8, 7, 2, 9, 18, 12, 0] back - it should store the following values after the method finishes executing: 索引:0 1 2 3 4 5 6 7前[1、8、7、2、9、18、12、0]后-在方法完成执行后,它应该存储以下值:

index: 0 1 2 3 4 5 6 7 front [1, 0, 7, 18, 9, 2, 12, 8] back Notice that numbers in even positions (positions 0, 2, 4, 6) have not moved. 下标:0 1 2 3 4 5 6 7前[1,0,7,18,9,2,12,8]后请注意,偶数位置(位置0、2、4、6)的数字没有移动。 That sub-sequence of numbers is still: (1, 7, 9, 12). 数字的子序列仍然是:(1、7、9、12)。 But notice that the numbers in odd positions (positions 1, 3, 5, 7) are now in reverse order relative to the original. 但是请注意,相对于原始位置,奇数位置(位置1、3、5、7)的数字现在相反。 In other words, the original sub-sequence: (8, 2, 18, 0) - has become: (0, 18, 2, 8). 换句话说,原始子序列:(8,2,18,0)-已变为:(0,18,2,8)。 You may use a single stack as auxiliary storage. 您可以将单个堆栈用作辅助存储。

I have gotten it to work with an even number of elements in my queue, but am lost when it comes to an odd number....do I have to re-structure my entire program? 我已经使它可以处理队列中偶数个元素,但是当涉及到奇数时迷失了……我必须重新构造整个程序吗? Or is this one of those quick-fixes?? 还是这是快速解决方案之一?

My code: 我的代码:

public static void reverseHalf(Queue<Integer> q){
    int size = q.size();
    Stack<Integer> s = new Stack<Integer>();


    for(int i = 0; i < size; i++){
        int n = q.remove();
        s.push(n);
        q.add(n);
    }
    for(int i = 0; i < size; i++){
        int n = q.remove();
        if(i % 2 == 0){
            q.add(n);
        }
    }

    for(int i = 0; i < size; i++){
        int n = s.pop();
        if(i % 2 == 0)
        q.add(n); 
    }

    for(int i = 0; i < size; i++){
        int n = q.remove();
        s.push(n);
        q.add(n);
    }

    for(int i = (size / 2); i < size; i++){
        q.remove();
    }

    for(int i = 0; i < (size / 2); i++){
        s.pop();
    }

    for(int i = 0; i < size / 2; i++){
        q.add(s.pop());
    }

    s.clear();

    for(int i = 0; i < size; i++){
        int n = q.remove();
        s.push(n);
        q.add(n);
    }

    for(int i = 0; i < (size  / 2); i++){
        int n = s.pop();
        int m = q.remove();
        q.add(n);
        q.add(m);
    }

    for(int i = 0; i < (size / 2); i++){
        q.remove();
    }


}

First of all you need to replace all of the 首先,您需要更换所有

i % 2 == 0

with

i % 2 != 0

The above is the definition of an odd number. 以上是奇数的定义。 The second trick lies in all of the lines containing 第二招在于所有包含

for(int i = 0; i < (size  / 2); i++)

you should be able to figure this one out easily. 您应该能够轻松地弄清楚这一点。

Initialize i to 1 and j to the last odd index. 将i初始化为1,将j初始化为最后一个奇数索引。 Swap the elements at these index. 在这些索引处交换元素。 Increment i by 2 and decrement j by 2 until j Less than i. 将i递增2,并将j递减2直到j小于i。 I think this should would work. 我认为这应该可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM