0

I want to rotate k top element of array for example:
k = 2
array before rotate --> 3 , 4 , 2
array after rotate ---> 3 , 2 , 4

 int[] temp = new int[k];

            for (int i = 0; i < k; i++) {
                if(this.stack[i]!= null){
                temp[i] = this.pop();
                }    else
                    i--;
            }
            for (int i = 0 ; i <k ; i++) {
                this.push(temp[i]);
            }

i want to implement it by stack
can any one help me to optimize it ?

3
Contributors
3
Replies
36
Views
2 Years
Discussion Span
Last Post by stultuske
0

Is there any particular reason you are not using the Java Stack collection? See Java Stac for some help.

I don't see the value in building your own stack collection, unless it's a homework exercise, in which case you need to struggle with it :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.