在第一章的第一部分中,有哪些关键内容或概念需要重点关注吗?
- 内容介绍
- 文章标签
- 相关推荐
本文共计393个文字,预计阅读时间需要2分钟。
javaimport 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)); }
private static void print(boolean isValid, int maxLength1, int maxLength2) { System.out.println(isValid + , + maxLength1 + , + maxLength2); }
private static boolean isValid(String s) { Stack stack=new Stack(); for (char c : s.toCharArray()) { if (c=='(') { stack.push(c); } else if (stack.isEmpty() || stack.pop() !='(') { return false; } } return stack.isEmpty(); }
private static int maxLength1(String s) { int max=0; for (int i=0; i private static int maxLength2(String s) { int max=0, count=0; for (int i=0; i 0) count--; else { max=Math.max(max, count); count=0; } } max=Math.max(max, count); return max; }} 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
本文共计393个文字,预计阅读时间需要2分钟。
javaimport 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)); }
private static void print(boolean isValid, int maxLength1, int maxLength2) { System.out.println(isValid + , + maxLength1 + , + maxLength2); }
private static boolean isValid(String s) { Stack stack=new Stack(); for (char c : s.toCharArray()) { if (c=='(') { stack.push(c); } else if (stack.isEmpty() || stack.pop() !='(') { return false; } } return stack.isEmpty(); }
private static int maxLength1(String s) { int max=0; for (int i=0; i private static int maxLength2(String s) { int max=0, count=0; for (int i=0; i 0) count--; else { max=Math.max(max, count); count=0; } } max=Math.max(max, count); return max; }} 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

