add: 220317 [cpp]
This commit is contained in:
parent
d6f6ffecff
commit
a321d557be
|
|
@ -0,0 +1,23 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static int scoreOfParentheses(const std::string& s) {
|
||||
int stack[30], top = -1, ret = 0;
|
||||
for (char ch : s) {
|
||||
if (ch == '(') {
|
||||
stack[++top] = 0;
|
||||
} else { // ch == ')'
|
||||
*(top ? &stack[top - 1] : &ret) += (stack[top] ? stack[top] << 1 : 1);
|
||||
--top;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << Solution::scoreOfParentheses("((())())");
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2203)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2203 220316-CN.cpp)
|
||||
ADD_EXECUTABLE(2203 220317.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue