Java C++題解leetcode856括號(hào)的分?jǐn)?shù)
題目要求

思路一:棧

Java
class Solution {
public int scoreOfParentheses(String s) {
Deque<Integer> sta = new ArrayDeque<>();
sta.addLast(0);
for (char c : s.toCharArray()) {
if (c == '(')
sta.addLast(0);
else { // 結(jié)束一個(gè)括號(hào)
int cur = sta.pollLast(); // 取出當(dāng)前分?jǐn)?shù)
sta.addLast(sta.pollLast() + Math.max(cur * 2, 1)); // 更新上級(jí)括號(hào)分?jǐn)?shù)
}
}
return sta.peekLast();
}
}
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(n)
C++
class Solution {
public:
int scoreOfParentheses(string s) {
stack<int> sta;
sta.push(0); // 初始0用于記錄結(jié)果分?jǐn)?shù)
for (auto c : s) {
if (c == '(')
sta.push(0);
else { // 結(jié)束一個(gè)括號(hào)
int cur = sta.top(); // 取出當(dāng)前分?jǐn)?shù)
sta.pop();
sta.top() += max(cur * 2, 1); // 更新上級(jí)括號(hào)分?jǐn)?shù)
}
}
return sta.top();
}
};
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(n)
Rust
impl Solution {
pub fn score_of_parentheses(s: String) -> i32 {
let mut sta = Vec::with_capacity((s.len() >> 1) + 1);
sta.push(0); // 初始0用于記錄結(jié)果分?jǐn)?shù)
for c in s.bytes() {
if c == b'(' {
sta.push(0);
}
else {
let cur = sta.pop().unwrap();
*sta.last_mut().unwrap() += 1.max(cur << 1);
}
}
sta[0]
}
}
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(n)
思路二:模擬計(jì)算
- 略去棧,直接記錄分?jǐn)?shù);
- 根據(jù)題意發(fā)現(xiàn)其實(shí)分?jǐn)?shù)來(lái)源就只是
(),所以記錄其所在深度depth考慮乘幾個(gè)222,然后累加到答案上即可。 - 因?yàn)榈谝粋€(gè)字符一定是
(,所以默認(rèn)深度為1,遍歷字符串時(shí)直接掠過(guò)s[0]。
Java
class Solution {
public int scoreOfParentheses(String s) {
int depth = 1, res = 0;
for (int i = 1; i < s.length(); i++) {
depth += (s.charAt(i) == '(' ? 1 : -1);
if (s.charAt(i - 1) == '(' && s.charAt(i) == ')') // 分?jǐn)?shù)來(lái)源
res += 1 << depth;
}
return res;
}
}
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(1)
C++
class Solution {
public:
int scoreOfParentheses(string s) {
int depth = 1, res = 0;
for (int i = 1; i < s.size(); i++) {
depth += (s[i] == '(' ? 1 : -1);
if (s[i - 1] == '(' && s[i] == ')') // 分?jǐn)?shù)來(lái)源
res += 1 << depth;
}
return res;
}
};
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(1)
Rust
impl Solution {
pub fn score_of_parentheses(s: String) -> i32 {
let (mut depth, mut res) = (1, 0);
let ss = s.as_bytes();
for i in 1..s.len() {
if (ss[i] == b'(') {
depth += 1
}
else {
depth -= 1;
if ss[i - 1] == b'(' { // 分?jǐn)?shù)來(lái)源
res += 1 << depth;
}
}
}
res
}
}
- 時(shí)間復(fù)雜度:O(n)
- 空間復(fù)雜度:O(1)
總結(jié)
自己想到的方法有點(diǎn)類(lèi)似兩種結(jié)合,用棧記錄分?jǐn)?shù)來(lái)源的括號(hào)并記錄最后計(jì)算分?jǐn)?shù),沒(méi)有意識(shí)到可以直接累加計(jì)算,順序不影響結(jié)果。
以上就是Java C++題解leetcode856括號(hào)的分?jǐn)?shù)的詳細(xì)內(nèi)容,更多關(guān)于Java C++ 括號(hào)的分?jǐn)?shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
c語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版三子棋(附完整代碼)
大家好,本篇文章主要講的是c語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易版三子棋(附完整代碼),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
C++??STL?_?Vector使用及模擬實(shí)現(xiàn)
這篇文章主要介紹了C++ STL_Vector使用及模擬實(shí)現(xiàn),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
C語(yǔ)言基礎(chǔ)知識(shí)點(diǎn)解析(extern,static,typedef,const)
本篇文章是對(duì)C語(yǔ)言基礎(chǔ)知識(shí)點(diǎn)(extern,static,typedef,const)的用法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-10-10
C++實(shí)現(xiàn)LeetCode(95.獨(dú)一無(wú)二的二叉搜索樹(shù)之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(95.獨(dú)一無(wú)二的二叉搜索樹(shù)之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

