자바 언어 구현 그룹 스 택 (작은 사례 추가)

public class Array<E> {
	int size;  //        
	E[] data;  //      
	
	/**
	 *        ,        
	 * @param capacity     
	 */
	public Array(int capacity) {
//		data=new E[capacity];      new    
		data=(E[])new Object[capacity];  //   new  object         
		size=0;
	}
	
	/**
	 *        ,        10
	 */
	public Array() {
		data=(E[])new Object[10];
		size=0;
	}
	
	/**
	 *          
	 * @return size       
	 */
	public int getSize() {
		return size;
	}
	
	/**
	 *        
	 * @return capacity     
	 */
	public int getCapacity() {
		return data.length;
	}
	
	/**
	 *         
	 * @return   
	 */
	public boolean isEmpty() {
		return size==0;
	}
	
	/**
	 *      index ,      
	 * @param index         
	 * @param e    
	 */
	public void add(int index,E e) {
		if (size==getCapacity())
//			throw new IllegalArgumentException("      ");
			resize(2*data.length);  //              ,    
		if (index<0||index>size)  //index    ,            , index=size ,       
			throw new IllegalArgumentException("index  ");
		for (int i = data.length-1; i >=index; i--) {
			data[i+1]=data[i];
		}
		data[size]=e;
		size++;
	}

	/**
	 *               
	 * @param e
	 */
	public void addLast(E e) {
//		if (size==getCapacity()) 
//			throw new IllegalArgumentException("      ");
//		data[size]=e;
//		size++;   //data[size++]=e;
		add(size, e);
	}
	
	/**
	 *           
	 * @param e
	 */
	public void addFirst(E e) {
		add(0, e);
	}
	
	/**
	 *   index    
	 * @param index
	 * @return
	 */
	public E get(int index) {
		if (index<0||index>size)  //index    ,            , index=size ,       
			throw new IllegalArgumentException("index  ");
		return data[index];
	}
	
	/**
	 *         
	 */
	public E getLast() {
		return get(data.length);
	}
	
	/**
	 *   index    
	 * @param index
	 * @return
	 */
	public void set(int index,E e) {
		if (index<0||index>size)  //index    ,            , index=size ,       
			throw new IllegalArgumentException("index  ");
		data[index]=e;
	}
	
	/**
	 *         e
	 * @param e
	 * @return
	 */
	public boolean contains(E e) {
		for (int i = 0; i < size; i++) {
			if (data[i]==e) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 *        e      
	 * @param e
	 * @return           ,     -1
	 */
	public int find(E e) {
		for (int i = 0; i < size; i++) {
			if (data[i]==e) {
				return i;
			}
		}
		return -1;
	}
	
	/**
	 *   index    
	 * @param index
	 * @return       
	 */
	public E remove(int index) {
		if (index<0||index>=size)  //index    ,          
			throw new IllegalArgumentException("index  ");
		E res=data[index];
		for (int i = index+1; i < data.length; i++) {
			data[i-1]=data[i];
		}
		size--;
		data[size]=null;  //          ,             GC  。    !=    
//		if (size==data.length/2) {
//			resize(data.length/2);  //        ,       
//		}
		if (size==data.length/4) {  //     ,      1/4 ,       ,         ,  O(1)    
		resize(data.length/2);  //        ,       
	}
		return res;
	}
	
	/**
	 *         
	 */
	public E removeLast() {
		return remove(data.length);
	}
	/**
	 *        e        
	 * @param e
	 * @return     
	 */
	public boolean removeElement(E e) {
		int res = find(e);
		if (res!=-1) {
			remove(res);
			return true;
		}
		return false;
	}

	/**
	 *     (   private       )
	 * @param newCapacity
	 */
	private void resize(int newCapacity) {
		E[] newdata=(E[])new Object[newCapacity];
		for (int i = 0; i < data.length; i++) {
			newdata[i]=data[i];
		}
		data=newdata;
	}
}

public class Stack<E> {
	Array<E> array;
	
	public Stack(int capacity) {
		array=new Array<E>(capacity);
	}
	
	void push(E e) {
		array.addLast(e);
	}
	
	E pop() {
		return array.removeLast();
	}
	
	E peek() {
		return array.getLast();
	}
	
	boolean isEmpty() {
		return array.isEmpty();
	}
}

작은 사례: 유효한 괄호: '(', ')', '{', '}', '[', ']' 만 포함 하 는 문자열 을 지정 하여 문자열 이 유효한 지 판단 합 니 다.유효한 문자열 은 만족 해 야 합 니 다. 왼쪽 괄호 는 같은 유형의 오른쪽 괄호 로 닫 아야 합 니 다.왼쪽 괄호 는 반드시 정확 한 순서 로 닫 아야 한다.빈 문자열 은 유효한 문자열 로 여 겨 질 수 있 습 니 다.
public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack();
        for (int i = 0; i < s.length(); i++) {
        	char c=s.charAt(i);
			if (c=='('||c=='['||c=='{') {
				stack.push(c);
			}else {
				if (stack.isEmpty()==true) {
					return false;
				}
				char topchar = stack.pop();
				if (c==')'&&topchar!='(') {
					return false;
				}
				if (c==']'&&topchar!='[') {
					return false;
				}
				if (c=='}'&&topchar!='{') {
					return false;
				}
			}
		}
        return stack.isEmpty(); //        
    }
}

좋은 웹페이지 즐겨찾기