Lettcode_252_Implement Stack using Queues

본 고 는 학습 중의 총 결 입 니 다.전재 하 는 것 을 환영 하지만 출처 를 밝 혀 주 십시오.http://blog.csdn.net/pistolove/article/details/48598773
Implement the following operations of a stack using queues.
  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

  • Notes:
  • You must use only standard operations of a queue -- which means only  push to backpeek/pop from frontsize , and  is empty  operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

  • 생각:
    (1)문 제 는 대기 열 을 활용 하여 스 택 을 실현 하 는 것 으로 주로 push(),pop(),top(),empty()방법의 실현 을 포함한다.
    (2)이 문제 의 사고방식 은'창 고 를 활용 하여 대열 을 실현 하 는 것'과 유사 하 다.우선 스 택 과 대기 열의 특성 을 알 아야 합 니 다.여기 서 다시 한 번 복습 합 니 다.스 택:먼저 나 가면 스 택 꼭대기 에 요 소 를 추가 하고 삭제 할 수 있 습 니 다.대기 열:먼저 나 가면 팀 끝 에 요 소 를 추가 하고 팀 머리 에서 요 소 를 제거 할 수 있 습 니 다.대기 열 로 스 택 을 실현 하려 면 먼저 넣 은 요 소 를 스 택 에서 먼저 나 오 는 방법 을 고려 해 야 합 니 다.두 개의 대기 열 로 이 루어 져 야 합 니 다.한 대기 열 은 작업 을 수행 한 후에 비어 있 고 다른 대기 열 은 요 소 를 저장 합 니 다.요소 가 추가 되 었 을 때 특정한 대기 열 이 비어 있 지 않 으 면 요 소 를 이 대기 열 에 추가 합 니 다.스 택 에 요소 가 있 을 때 빈 대기 열 list 에 서 는 팀 의 꼬리 요 소 를 제외 한 나머지 요 소 를 모두 다른 빈 대기 열 list 0 에 추가 해 야 합 니 다.그러면 list 대기 열 에 있 는 팀 의 꼬리 요소(스 택 상단 요소 에 대응)는 list 0 에 가입 하지 않 고 list 를 빈 대기 열 로 설정 하면 스 택 작업 이 완 료 됩 니 다.나머지 조작 은 비교적 간단 하 니 코드 를 참조 하 시기 바 랍 니 다.
    (3)자세 한 내용 은 아래 코드 를 보십시오.본문 이 너 에 게 도움 이 되 기 를 바란다.
    알고리즘 코드 는 다음 과 같 습 니 다.
    package leetcode;
    
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * 
     * @author liqqc
     *
     */
    public class Implement_Stack_using_Queues {
    
    	// Push element x onto stack.
    
    	private List<Integer> list = new LinkedList<Integer>();
    	private List<Integer> list0 = new LinkedList<Integer>();
    
    	public void push(int x) {
    		if (list.size() == 0 && list0.size() == 0) {
    			list.add(x);
    			return;
    		}
    
    		if (list.size() != 0) {
    			list.add(x);
    			return;
    		}
    
    		if (list0.size() != 0) {
    			list0.add(x);
    		}
    
    	}
    
    	// Removes the element on top of the stack.
    	public void pop() {
    
    		if (list.size() != 0 && list0.size() == 0) {
    			for (int i = 0; i < list.size() - 1; i++) {
    				list0.add(list.get(i));
    			}
    			list.clear();
    			return;
    		}
    
    		if (list.size() == 0 && list0.size() != 0) {
    			for (int i = 0; i < list0.size() - 1; i++) {
    				list.add(list0.get(i));
    			}
    			list0.clear();
    		}
    
    	}
    
    	// Get the top element.
    	public int top() {
    		if (list.size() != 0 && list0.size() == 0) {
    			return list.get(list.size() - 1);
    		}
    
    		if (list.size() == 0 && list0.size() != 0) {
    			return list0.get(list0.size() - 1);
    		}
    
    		return -1;
    	}
    
    	// Return whether the stack is empty.
    	public boolean empty() {
    
    		return list0.size() == 0 && list.size() == 0;
    	}
    }
    

    좋은 웹페이지 즐겨찾기