자바 언어 구현 그룹 스 택 (작은 사례 추가)
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(); //
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
centos yum 창고 구축yum 창고 소개 yum (모두 Yellow dog Updater, Modified 라 고 함) 은 Fedora 와 RedHat 에 있 는 Shell 전단 패키지 관리자 입 니 다.RPM 패키지 관 리 를 바탕 으로...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.