add: 220317 [cpp]

This commit is contained in:
Lam Haoyin 2022-03-17 11:17:26 +08:00
parent d6f6ffecff
commit a321d557be
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 24 additions and 1 deletions

23
cpp/2203/220317.cpp Normal file
View File

@ -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;
}

View File

@ -3,4 +3,4 @@ PROJECT(2203)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2203 220316-CN.cpp) ADD_EXECUTABLE(2203 220317.cpp)