mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-05 01:36:32 +08:00
init
This commit is contained in:
52
else/kuohao.h
Normal file
52
else/kuohao.h
Normal file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/4.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_KUOHAO_H
|
||||
#define LEECODE_C_KUOHAO_H
|
||||
|
||||
#include <string>
|
||||
#include <stack>
|
||||
|
||||
bool isKuoHao(std::string target) {
|
||||
std::stack<char> tk;
|
||||
for (int i = 0; i < target.length(); ++i) {
|
||||
if (tk.empty()) {
|
||||
tk.push(target[i]);
|
||||
continue;
|
||||
}
|
||||
char temp;
|
||||
switch (target[i]) {
|
||||
case '{':
|
||||
temp = '}';
|
||||
break;
|
||||
case '}':
|
||||
temp = '{';
|
||||
break;
|
||||
case '[':
|
||||
temp = ']';
|
||||
break;
|
||||
case ']':
|
||||
temp = '[';
|
||||
break;
|
||||
case '(':
|
||||
temp = ')';
|
||||
break;
|
||||
case ')':
|
||||
temp = '(';
|
||||
break;
|
||||
default:
|
||||
temp = 'x';
|
||||
}
|
||||
if (temp == tk.top()) {
|
||||
tk.pop();
|
||||
} else {
|
||||
tk.push(target[i]);
|
||||
}
|
||||
}
|
||||
return tk.empty();
|
||||
}
|
||||
|
||||
// cout << isKuoHao("{}(({}))") << endl;
|
||||
|
||||
#endif //LEECODE_C_KUOHAO_H
|
||||
Reference in New Issue
Block a user