做的时候没想到用栈,用了一个比较麻烦的map解法

栈的压入、弹出序列

http://www.nowcoder.com/questionTerminal/d77d11405cc7470d82554cb392585106

import java.util.ArrayList;
import java.util.HashMap;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i = 0; i < pushA.length; i++) map.put(pushA[i], i);
        int top  = -1;
        for(int i = 0; i < popA.length; i++){
            if(map.get(popA[i]) == null || map.get(popA[i]) < top) return false;
            else{
                int tmp = map.get(popA[i]) - 1;
                map.remove(popA[i]);
                while(true){
                    if(tmp < 0 || map.get(pushA[tmp]) != null){
                        top = tmp;
                        break;
                    }else{
                        tmp--;
                    }
                }
            }
        }
        return true;
    }
}
全部评论

相关推荐

每晚夜里独自颤抖:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务