2 int Arrays 1 Stack,检查是否可以使用堆栈操作进行排列?

时间:2015-03-18 22:55:31

标签: java arrays algorithm stack permutation

我的问题在于排列类,我有两个int数组,例如 在排列类的主要部分。 我必须检查堆栈提供的操作是否可以进行这种排列... push,pop,isEmpty,top ....

我被卡住了,方法switchValues()需要通过我找不到解决方案来纠正...请帮帮忙。 THX

public class Permutation {

private Stack _permStack;
private int [] _beforePerm;
private int [] _afterPerm;
private int _beforeIndex =0;
private int _afterIndex = 0;

public Permutation (int [] beforePerm, int [] afterPerm){
    _beforePerm = beforePerm;
    _afterPerm = afterPerm;
    _permStack = new Stack (10);
}
public boolean arrayPreCheck(){
    boolean reversedArray= false;
    for (int i=0; i<_beforePerm.length-1;i++){
        for (int j = _afterPerm.length-1; j >0;j--){
            if (_beforePerm[i] ==_afterPerm[j]){
                reversedArray = true;

            }else{
                return reversedArray = false; 
            }
        }
    }
    return reversedArray;
}

public boolean isPermutation(){

    if(_permStack.isEmpty()&&_beforeIndex ==_beforePerm.length&&_afterIndex ==_afterPerm.length){
        System.out.println("Permutation is true");
        return true;
    }else{

        return false;
    }
}



public void switchValues(){

    _permStack.push(_beforePerm[_beforeIndex]);

    while(!isPermutation()&& _beforeIndex <_beforePerm.length){
        if(_permStack.top() == _afterPerm[_afterIndex]){
            _permStack.pop();
            _beforeIndex++;
            _afterIndex++;
            if(_beforeIndex < _beforePerm.length){
                _permStack.push(_beforePerm[_beforeIndex]);
                }
        }else{

            _beforeIndex++;
            if(_beforeIndex < _beforePerm.length){
            _permStack.push(_beforePerm[_beforeIndex]);
            }
        }
    }

    if(!isPermutation()){
        System.out.println("Permutation false");
    }

 }


public static void main (String []args){
    int [] before = new int[] {1,2,3,4,5};
    int [] after = new int []{1,3,5,4,2};
    Permutation myPerm = new Permutation (before,after);
    myPerm.switchValues();

}
}

class Stack {

private int [] _values;
private int _position;


public Stack (int size){
    _values = new int [size];
    _position = 0;

}


public boolean isEmpty(){
    if (_position == 0){
        return true;
    }else{
        return false;
    }
}


public int top(){

    if(isEmpty()){
        return -1;
    }else{
        return _values[_position-1];
    }
}



public boolean push(int value){

    if (_position< _values.length){
        _values [_position] = value;
        _position++;
        System.out.println("push:"+value);

    return true;
}else{
    return false;
    }
}


public int pop(){

    if (isEmpty()){
         return -1;
     }else{
         _position--;
         System.out.println("pop: "+ _values[_position]);
         return _values[_position];
     }
}


public void printStack(){
    int j = 0;
    for (int i=0;i<_values.length;i++){
        System.out.println(_values[i]+" at the spot: "+ j);
        j++;
    }
}

0 个答案:

没有答案