【发布时间】:2016-09-18 02:16:13
【问题描述】:
以下是我对这个问题的分析:括号匹配的条件有四种:{{()}}、{}[]<>、<{}[]>、{<>[]}<>
因此,如果我只考虑这 4 种匹配形式,可能会很复杂。所以我试图找出括号何时不匹配。如果我让{ 和} 成为一对,我会发现一个括号是否在奇数位置,那么他的对必须在偶数位置,反之亦然。以{<>[]}<> 为例,{ 位于第 1 位,为奇数位,} 位于第 6 位,为偶数位。因此我用数字来标记它们'()'--1,9; '[]'--2,8; '<>' --3,7; '{}' --4,6,所以如果两个数字加起来等于10,那么这两个数字代表一对。然后我用这些数字来表示括号结构。我拉出奇数位置的括号和偶数位置的括号(用数字表示它们),然后我将奇数位置和偶数位置的每个项目相加,看看是否有一个匹配项加起来是 10,如果没有,我说这是一场比赛。我的代码如下:
/** Matching Brackets
* Tony
*/
import java.util.*;
public class Solution19 {
public static String process(String n) {
/** build a condition combination: */
String newString = "";
for (int i = 0; i < n.length(); i++) {
if (n.charAt(i) == '(' || n.charAt(i) == ')' || n.charAt(i) == '[' || n.charAt(i) == ']'
|| n.charAt(i) == '<' || n.charAt(i) == '>' || n.charAt(i) == '{' || n.charAt(i) == '}') {
newString += n.charAt(i);
}
}
return newString;
}
public static String numForm(String s) {
String newone = "";
for (int i = 0; i < s.length(); i++) {
switch(s.charAt(i)) {
case '(': newone += "1 ";break;
case ')': newone += "9 ";break;
case '[': newone += "2 ";break;
case ']': newone += "8 ";break;
case '<': newone += "3 ";break;
case '>': newone += "7 ";break;
case '{': newone += "4 ";break;
case '}': newone += "6 ";break;
}
}
return newone;
}
public static int[] intArray(String m) {
String[] stringArray = m.split(" ");
int[] intArr = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
intArr[i] = Integer.parseInt(stringArray[i]);
}
return intArr;
}
public static void printArray (int[] array) {
for (int n : array) {
System.out.print(n + " ");
}
}
public static int[] oddPosition (int[] array) {
int [] oddNumbers = new int[array.length / 2];
int j = 0;
for (int i = 0; i < array.length; i++) {
if ((i + 1) % 2 != 0) {
oddNumbers[j] = array[i];
j ++;
}
}
return oddNumbers;
}
public static int[] evenPosition (int[] array) {
int [] evenNumbers = new int[array.length / 2];
int j = 0;
for (int i = 0; i < array.length; i++) {
if ((i + 1) % 2 == 0) {
evenNumbers[j] = array[i];
j ++;
}
}
return evenNumbers;
}
public static boolean addsUpten (int [] array) {
boolean conditionSum = false;
boolean conditionSingle = false;
for (int i = 0; i < array.length; i++) {
int d = 0;
while (i + d < array.length) {
if (array[i] + array[i+d] == 10) {
conditionSingle = true;
}
conditionSum = (conditionSum || conditionSingle);
d ++;
}
}
return conditionSum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
String voider = sc.nextLine();
for (int i = 0; i < times; i ++) {
String formula = sc.nextLine();
String processed = process(formula);
String numFormed = numForm(processed);
// System.out.println(numFormed);
int[] numArray = intArray(numFormed);
if (numArray.length % 2 != 0) {
System.out.print("0 ");
}
else {
int[] oddNumbers = oddPosition(numArray);
int[] evenNumbers = evenPosition(numArray);
if (addsUpten(oddNumbers) || addsUpten(evenNumbers) == true) {
System.out.print("0 ");
}
else {
System.out.print("1 ");
}
}
}
}
}
正如我所料,它应该可以工作,并且在我输入时它确实可以工作:
4
(a+[b*c]-{d/3})
(a + [b * c) - 17]
(((a * x) + [b] * y) + c
auf(zlo)men [gy<psy>] four{s}
它给了我输出:1 0 0 1(1 表示匹配,0 表示不匹配)。但是,当我输入像[^]<t>(z){<[^]<w>[{f}c]y[-][v]{<y>g<+( )>(c){w{a{t}}}}>((a)w)} 这样更长的内容时,它是匹配的,但它给了我0。我想知道我确定它是否匹配的方式是对还是错?我错过了什么?抱歉,代码很长,只是想知道("I find out if one bracket is on the odd position then his pair must be in a even position, vice versa.") 这种描述括号匹配的方式是对还是错?谢谢!
【问题讨论】:
-
您的偶数/奇数假设显然是错误的。以
(a)为例。 -
"(a)" 将首先转换为 "()"; '(' 是第 1 个,')' 是第 2 个。所以一个是奇数另一个是偶数。怎么了?
-
在 Java 循环中避免使用
String +=- 使用 StringBuilder。我认为你的条件不仅过于复杂,而且只是必要的,而不是充分的。 -
@greybeard 谢谢。
标签: java algorithm math brackets