QR4. 表达式合法判断
描述
给定一个表达式A,请返回一个bool值,代表它是否合法。
"[a+b*(5-4)]*{x+b+b*(({1+2}))}"
返回:true
"q*c*k+r-w-{f-e*c+f}"
返回:true
"g+{p+z-v"
返回:false
Java 解法, 执行用时: 1ms, 内存消耗: 251KB, 提交时间: 2016-01-18
import java.util.Stack; public class ChkExpression { public boolean chkLegal(String A) { Stack<Character> s1 = new Stack<>(); Stack<Character> s2 = new Stack<>(); Stack<Character> s3 = new Stack<>(); for (int i = 0; i < A.length(); ++i) { char c = A.charAt(i); switch (c) { case '{': s1.push(c); break; case '[': s2.push(c); break; case '(': s3.push(c); break; case '}': s1.pop(); break; case ']': s2.pop(); break; case ')': s3.pop(); break; } } return s1.isEmpty() && s2.isEmpty() && s3.isEmpty(); } }
C++ 解法, 执行用时: 2ms, 内存消耗: 416KB, 提交时间: 2021-09-27
class ChkExpression { public: bool chkLegal(string A) { stack<char> ch; for (int i = 0; i < A.length(); i++) { if (A[i] == '{' || A[i] == '[' || A[i] == '(') { ch.push(A[i]); } if (A[i] == '}' || A[i] == ']' || A[i] == ')') { if (ch.size() == 0) return false; ch.pop(); } } return ch.size() == 0; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 376KB, 提交时间: 2020-09-05
class ChkExpression { public: bool chkLegal(string A) { // write code here int num = A.length(); int arr[8] = {0}; int num1=0; while(num) { if(A[num1]=='(') arr[0]++; else if(A[num1]==')') arr[1]++; else if(A[num1]=='{') arr[2]++; else if(A[num1]=='}') arr[3]++; else if(A[num1]=='[') arr[4]++; else if(A[num1]==']') arr[5]++; num1++; num--; } if(arr[0]==arr[1]&&arr[2]==arr[3]&&arr[4]==arr[5]) return true; else return false; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 376KB, 提交时间: 2020-09-04
class ChkExpression { public: bool chkLegal(string A) { int num_left_cb = 0; int num_left_sb = 0; int num_left_p = 0; for (int i = 0; i < A.size(); ++i){ if (A[i] == '{'){ //if (num_left_p != 0 || num_left_sb != 0) //return false; ++num_left_cb; } else if (A[i] == '['){ //if (num_left_p != 0) //return false; ++num_left_sb; } else if (A[i] == '('){ ++num_left_p; } else if (A[i] == '}'){ //if (num_left_p != 0 || num_left_sb != 0) //return false; if (num_left_cb == 0) return false; --num_left_cb; } else if (A[i] == ']'){ //if (num_left_p != 0) //return false; if (num_left_sb == 0) return false; --num_left_sb; } else if (A[i] == ')'){ if (num_left_p == 0) return false; --num_left_p; } } if (num_left_p == 0 && num_left_cb == 0 && num_left_sb == 0) return true; else return false; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 408KB, 提交时间: 2021-09-01
class ChkExpression { public: bool chkLegal(string A) { // write code here stack<char> _stack = stack<char>(); char topChar; for(int i = 0; i < A.size(); i++){ if(A[i] == '{' || A[i] == '[' || A[i] == '('){ _stack.push(A[i]); } // if(A[i] == '}'){ // topChar = _stack.top(); // if(topChar != '{'){ // return false; // } // else{ // _stack.pop(); // } // } // else if(A[i] == ']'){ // topChar = _stack.top(); // if(topChar != '['){ // return false; // } // else{ // _stack.pop(); // } // } // else if(A[i] == ')'){ // topChar = _stack.top(); // if(topChar != '('){ // return false; // } // else{ // _stack.pop(); // } // } if(A[i] == '}' || A[i] == ']' || A[i] == ')'){ topChar = _stack.top(); if(topChar == '{' || topChar == '[' || topChar == '('){ _stack.pop(); } else{ return false; } } } if(_stack.size() != 0){ return false; } else{ return true; } } };