日撸 Java 三百行学习笔记day15

第 15 天: 栈的应用(括号匹配)

在数据处理中,常常要判断数据中的括号,应用栈来处理及其方便。
当用户输入一个字符串时,遇到左括号时,将其入栈,如’(’ ‘[”’{,而遇到右括号时,如’)”}”]’时,与栈顶的括号与当前匹配,如果匹配成功,则将栈中的括号出栈,表示当前括号成对。

 如匹配失败,又分为两种情况,栈已经空和栈不空,若栈已空,则现在右括号比左括号多()},栈不空则很明显,当前括号与栈中括号不匹配(}.

 当所有的匹配完成,也有两种状态,栈空或不空,栈空,当前字符串中所有字符匹配成功,栈不空,则还有左括号待匹配,说明左括号多。

public static boolean bracketMatching(String paraString) {
			// Step 1. Initialize the stack through pushing a '#' at the bottom.
			CharStack tempStack = new CharStack();
			tempStack.push('#');
			char tempChar, tempPopedChar;

			// Step 2. Process the string. For a string, length() is a method
			// instead of a member variable.
			for (int i = 0; i < paraString.length(); i++) {
				tempChar = paraString.charAt(i);

				switch (tempChar) {
				case '(':
				case '[':
				case '{':
					tempStack.push(tempChar);
					break;
				case ')':
					tempPopedChar = tempStack.pop();
					if (tempPopedChar != '(') {
						return false;
					} // Of if
					break;
				case ']':
					tempPopedChar = tempStack.pop();
					if (tempPopedChar != '[') {
						return false;
					} // Of if
					break;
				case '}':
					tempPopedChar = tempStack.pop();
					if (tempPopedChar != '{') {
						return false;
					} // Of if
					break;
				default:
					// Do nothing.
				}// Of switch
			} // Of for

			tempPopedChar = tempStack.pop();
			if (tempPopedChar != '#') {
				return false;
			} // Of if

			return true;
		}// Of bracketMatching


		/**
		 *********************
		 * The entrance of the program.
		 * 
		 * @param args Not used now.
		 *********************
		 */
		
		public static void main(String args[]) {
			CharStack tempStack = new CharStack();

			for (char ch = 'a'; ch < 'm'; ch++) {
				tempStack.push(ch);
				System.out.println("The current stack is: " + tempStack);
			} // Of for i

			char tempChar;
			for (int i = 0; i < 12; i++) {
				tempChar = tempStack.pop();
				System.out.println("Poped: " + tempChar);
				System.out.println("The current stack is: " + tempStack);
			} // Of for i

			boolean tempMatch;
			String tempExpression = "[2 + (1 - 3)] * 4";
			tempMatch = bracketMatching(tempExpression);
			System.out
					.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

			tempExpression = "( )  )";
			tempMatch = bracketMatching(tempExpression);
			System.out
					.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

			tempExpression = "()()(())";
			tempMatch = bracketMatching(tempExpression);
			System.out
					.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

			tempExpression = "({}[])";
			tempMatch = bracketMatching(tempExpression);
			System.out
					.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);

			tempExpression = ")(";
			tempMatch = bracketMatching(tempExpression);
			System.out
					.println("Is the expression " + tempExpression + " bracket matching? " + tempMatch);
		}// Of main
}

其中对于用#初始化栈不太理解,对最后出栈再判断是否为#,这个前面不匹配直接return false,如果都对了就可以直接return true了,感觉不用再管那个#了。具体代码如下。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值