Valid Parentheses

xiaoxiao2021-02-28  35



Before showing mine codes,i would like you to know:

Object to Character can be finished through impulsive way,like '(Character)map.get(index)'.

 

Here is mine codes:

 

import java.util.HashMap;

import java.util.Vector;

 

public class ValidParentheses {

 

public static char[][] standard = { {'}','{'},

{']','['},

{')','('} };

public HashMap<Integer,Character> mape = new HashMap();

public HashMap<Integer,Character> map = new HashMap();

public void checkParent(HashMap map,int i) {

int origin = i;

int correspond = i;

char c = (Character)map.get(origin);

while ( correspond >= 0 ) {

if (map.containsKey(--correspond)) {

for ( int j = 0; j < standard.length; j++ ) {

if (c == standard[j][0]) {

if ((Character)map.get(correspond) == standard[j][1]) {

map.remove(origin);

map.remove(correspond);

--correspond;

}

else {

mape.put(correspond, (Character)map.get(i));

mape.put(origin, c);

map.remove(origin);

map.remove(correspond);

--correspond;

}

break;

}

}

break;

}

}

}

public boolean checkItInsideFirst(char c) {

switch (c) {

case '}':

case ']':

case ')':return true;

default : return false;

}

}

public boolean checkIt(String x) {

boolean flag = true;

if ( x.isEmpty() ) {

return true;

}

else if (x.length() % 2 != 0 || checkItInsideFirst(x.charAt(0)) ) {

flag = false;

}

return flag;

}

public boolean isValid(String x) {

boolean flagsub = false;

Vector vector = new Vector();

int lenx =x.length();

if ( !checkIt(x) ) {

return false;

}

for ( int i = 0; i < lenx; i++ ) {

switch( (Character)x.charAt(i) ) {

case '{':

case '[':

case '(': flagsub = true;

break;

case '}':

case ']':

case ')':   flagsub = false;

break;

default: break;

}

map.put(i,x.charAt(i));

if ( !flagsub ) {

checkParent(map,i);

}

}

return mape.size() > 0 || map.size() != 0?false:true;

}

}

 

 

转载请注明原文地址: https://www.6miu.com/read-2622668.html

最新回复(0)