当前位置: 首页 > news >正文

[Swift]LeetCode856. 括号的分数 | Score of Parentheses

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10594301.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given a balanced parentheses string S, compute the score of the string based on the following rule:

  • () has score 1
  • AB has score A + B, where A and B are balanced parentheses strings.
  • (A) has score 2 * A, where A is a balanced parentheses string. 

Example 1:

Input: "()"
Output: 1

Example 2:

Input: "(())"
Output: 2

Example 3:

Input: "()()"
Output: 2

Example 4:

Input: "(()(()))"
Output: 6 

Note:

  1. S is a balanced parentheses string, containing only (and ).
  2. 2 <= S.length <= 50

给定一个平衡括号字符串 S,按下述规则计算该字符串的分数:

  • () 得 1 分。
  • AB 得 A + B 分,其中 A 和 B 是平衡括号字符串。
  • (A) 得 2 * A 分,其中 A 是平衡括号字符串。 

示例 1:

输入: "()"
输出: 1

示例 2:

输入: "(())"
输出: 2

示例 3:

输入: "()()"
输出: 2

示例 4:

输入: "(()(()))"
输出: 6 

提示:

  1. S 是平衡括号字符串,且只含有 ( 和 ) 。
  2. 2 <= S.length <= 50

Runtime: 4 ms
Memory Usage: 19.5 MB
 1 class Solution {
 2     func scoreOfParentheses(_ S: String) -> Int {
 3         var stack:[Int] = [Int]()
 4         var cur:Int = 0
 5         for c in S
 6         {
 7             if c == "("
 8             {
 9                 stack.append(cur)
10                 cur = 0
11             }
12             else
13             {
14                 cur = stack.removeLast() + max(cur * 2, 1)
15             }
16         }
17         return cur
18     }
19 }

4ms

 1 class Solution {
 2     func scoreOfParentheses(_ S: String) -> Int {
 3         guard S.count > 1 && S.count%2 == 0 else {
 4             return 0
 5         }
 6         
 7         var mulArr = [Int]()
 8         mulArr.append(0)
 9         var mulInx = 0
10         let strArr = Array(S)
11         for i in 0..<strArr.count-1 {
12             let phrase = "\(strArr[i])\(strArr[i+1])"
13             switch phrase{
14                 case "((":
15                     mulInx += 1
16                     if mulInx == mulArr.count  {
17                         mulArr.append(0)
18                     } 
19                 case "))":                
20                     mulArr[mulInx-1] += mulArr[mulInx]*2
21                     mulArr[mulInx] = 0
22                     mulInx -= 1
23                     
24                 case "()":
25                     mulArr[mulInx] += 1
26                 case ")(":
27                     continue
28                 default:
29                     continue
30             }
31         }
32         return mulArr[0]
33     }
34 }

8ms

 1 class Solution {
 2     func scoreOfParentheses(_ S: String) -> Int {
 3         guard S.count % 2 == 0 else {
 4             // this means that it is an invalid parentheses combination
 5             return -1
 6         }
 7         
 8         var sum: Int = 0 
 9         var stack: [Character] = []
10         var shouldCount: Bool = false 
11         for character in S {
12             if character == "(" {
13                 stack.append("(")
14                 shouldCount = true 
15             } else {
16                 // if character == ")"
17                 stack.removeLast()
18                 if shouldCount {
19                     sum += Int(pow(Double(2), Double(stack.count)))
20                     shouldCount = false 
21                 }
22             }
23         }
24         return sum
25     }
26 }

12ms

 1 class Solution {
 2     func scoreOfParentheses(_ S: String) -> Int {
 3         if S.count == 0 {
 4             return 0
 5         }
 6         let chars = Array(S)
 7         
 8         // O(n)方法,因为每一次括号的嵌套都会乘以2,所以对于一个基础"()",
 9         // 它前面有多少个开"(",就要乘以多少个2,2^(d-1),d是"()"自己的"("加上前面发现的开"("
10         // 把(()(()))转化为 -> (()) + ((())) = 2^1 + 2^2 = 6
11         var sum = 0, leftCount = 0
12         var stack = [Character]()
13         
14         for char in chars {
15             if char == "(" {
16                 // add to stack
17                 leftCount += 1
18             } else {
19                 if let previous = stack.last, previous == "(" {
20                     sum += Int(pow(Double(2), Double(leftCount - 1)))
21                 }
22                 leftCount -= 1
23             }
24             stack.append(char)
25         }
26         
27         return sum
28     }
29 }

12ms

 1 class Solution {
 2     func scoreOfParentheses(_ S: String) -> Int {
 3     var arr = [0]    
 4     for c in Array(S) {
 5         if c == "(" {
 6             arr.append(0)
 7         } else {
 8             let last = arr.removeLast()
 9             if last == 0 {
10                 arr[arr.count-1] += 1
11             } else {
12                 arr[arr.count-1] += (2 * last)
13             }
14         }
15     }    
16     return arr.last!
17   }
18 }

16ms

 1 class Solution {        
 2     func scoreOfParentheses(_ S: String) -> Int {
 3         if let result = Int(S) {
 4             return result
 5         }
 6         
 7         var array = Array(S).map({ String($0) })
 8         var i = array.count - 1
 9         while i > 0 {
10             if array[i - 1] == "(" && array[i] == ")" {
11                 array.remove(at: i)
12                 array[i - 1] = "1"
13                 i -= 1
14             }
15             
16             i -= 1
17         }
18         
19         return Int(scoreOfParentheses(array)) ?? 0
20     }
21     
22     func scoreOfParentheses(_ string: [String]) -> String {
23         if string.count == 1 {
24             return string[0]
25         }
26         var string = string
27         
28         var i = string.count - 1
29         while i > 0 {
30             let index = i
31             
32             if let num1 = Int(String(string[index])), 
33                 let num2 = Int(String(string[index - 1])){
34                 string.remove(at: index)
35                 string[index - 1] = String(num1 + num2)
36                 i -= 1
37             }
38             i -= 1
39         }
40         
41         i = string.count - 1
42         while i > 1 {
43                 let index = i
44             
45                 if string[index] == ")", string[index - 2] == "(", 
46                     let num = Int(string[index - 1]) {
47                     string.remove(at: index)
48                     string.remove(at: index - 1)
49                     string[index - 2] = String(num * 2)
50                     i -= 2
51                 }
52             i -= 1
53         }
54         
55         return scoreOfParentheses(string)
56     }
57 }

 

转载于:https://www.cnblogs.com/strengthen/p/10594301.html

相关文章:

  • 关于Python程序的运行方面,有什么手段能提升性能
  • 牛客练习赛42 E.热爆了
  • Linux内核启动流程分析
  • day16 匿名函数
  • BZOJ 3744 Gty的妹子序列 (分块+树状数组+主席树)
  • 前端——HTML
  • Kali Linux Metasploit Framework
  • 位运算的初了解(二)
  • AtCoder Beginner Contest 120 题解
  • 第三章小结
  • 微信小程序_(组件)icon、text、rich-text、progress四大基础组件
  • 处理机调度算法
  • 上周热点回顾(3.25-3.31)
  • 软工第三次团队作业 - 功能规格说明书
  • [北航软工]技术规格说明书
  • ----------
  • 【css3】浏览器内核及其兼容性
  • 【EOS】Cleos基础
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • axios 和 cookie 的那些事
  • IDEA 插件开发入门教程
  • Intervention/image 图片处理扩展包的安装和使用
  • MobX
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • node和express搭建代理服务器(源码)
  • spring boot下thymeleaf全局静态变量配置
  • spring cloud gateway 源码解析(4)跨域问题处理
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • STAR法则
  • vue总结
  • 多线程 start 和 run 方法到底有什么区别?
  • 基于遗传算法的优化问题求解
  • 基于游标的分页接口实现
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • Spring第一个helloWorld
  • ​香农与信息论三大定律
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (阿里云万网)-域名注册购买实名流程
  • (笔试题)分解质因式
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (实战篇)如何缓存数据
  • (四) Graphivz 颜色选择
  • (转)h264中avc和flv数据的解析
  • ***测试-HTTP方法
  • *1 计算机基础和操作系统基础及几大协议
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net快速开发框架源码分享
  • @AutoConfigurationPackage的使用
  • @在php中起什么作用?
  • [ linux ] linux 命令英文全称及解释
  • [.NET]桃源网络硬盘 v7.4
  • [AIGC] 开源流程引擎哪个好,如何选型?
  • [Android学习笔记]ScrollView的使用