iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >JavaC++题解leetcode856括号的分数
  • 548
分享到

JavaC++题解leetcode856括号的分数

JavaC++括号的分数JavaC++leetcode题解 2022-11-13 18:11:06 548人浏览 薄情痞子
摘要

目录题目要求思路一:栈Javac++Rust思路二:模拟计算JavaC++Rust总结题目要求 思路一:栈 Java class Solution { public in

题目要求

思路一:栈

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 { // 结束一个括号
                int cur = sta.pollLast(); // 取出当前分数
                sta.addLast(sta.pollLast() + Math.max(cur * 2, 1)); // 更新上级括号分数
            }
        }
        return sta.peekLast();
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

C++

class Solution {
public:
    int scoreOfParentheses(string s) {
        stack<int> sta;
        sta.push(0); // 初始0用于记录结果分数
        for (auto c : s) {
            if (c == '(')
                sta.push(0);
            else { // 结束一个括号
                int cur = sta.top(); // 取出当前分数
                sta.pop();
                sta.top() += max(cur * 2, 1); // 更新上级括号分数
            }
        }
        return sta.top();
    }
};
  • 时间复杂度:O(n)
  • 空间复杂度: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用于记录结果分数
        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]
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

思路二:模拟计算

  • 略去栈,直接记录分数;
  • 根据题意发现其实分数来源就只是(),所以记录其所在深度depth考虑乘几个222,然后累加到答案上即可。
  • 因为第一个字符一定是(,所以默认深度为1,遍历字符串时直接掠过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) == ')') // 分数来源
                res += 1 << depth;
        }
        return res;
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度: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] == ')') // 分数来源
                res += 1 << depth;
        }
        return res;
    }
};
  • 时间复杂度:O(n)
  • 空间复杂度: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'(' { // 分数来源
                    res += 1 << depth;
                }
            }
        }
        res
    }
}
  • 时间复杂度:O(n)
  • 空间复杂度:O(1)

总结

自己想到的方法有点类似两种结合,用栈记录分数来源的括号并记录最后计算分数,没有意识到可以直接累加计算,顺序不影响结果。

以上就是Java C++题解LeetCode856括号的分数的详细内容,更多关于Java C++ 括号的分数的资料请关注编程网其它相关文章!

--结束END--

本文标题: JavaC++题解leetcode856括号的分数

本文链接: https://www.lsjlt.com/news/169520.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作