当前位置 : 主页 > 编程语言 > c++ >

Chapter1-1

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt import java.util.Stack;public class Main{ public static void main(String[] args){ String str1 = "((())())"; print(isValid(str1),maxLength1(str1),maxLength2(str1)); String str2 = "(())(()(())"; print(isValid(str2),maxLength1(st
gistfile1.txt
import java.util.Stack;

public class Main{
    public static void main(String[] args){
        String str1 = "((())())";
        print(isValid(str1),maxLength1(str1),maxLength2(str1));

        String str2 = "(())(()(())";
        print(isValid(str2),maxLength1(str2),maxLength2(str2));

        String str3 = "()(()()(";
        print(isValid(str3),maxLength1(str3),maxLength2(str3));

    }
    public static void print(boolean b,int maxLength1,int maxLength2){
        System.out.println(b+"\t"+maxLength1+"\t"+maxLength2);
    }
    public static boolean isValid(String str){
        if(str==null||str.equals(""))
            return false;
        char[] chars=str.toCharArray();
        int status=0;
        for(int i=0;i
 
   stack=new Stack
  
   (); int start=-1,maxLength=0; for(int i=0;i
   
    =0&&chars[pre]=='(') dp[i]=dp[i-1]+2+(pre>0 ? dp[pre-1]:0); } maxLength=Math.max(maxLength,dp[i]); } return maxLength; } }
   
  
 
网友评论