[swift] LeetCode 394. Decode String

栏目: Swift · 发布时间: 5年前

内容简介:Given an encoded string, return it’s decoded string.The encoding rule is:You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Given an encoded string, return it’s decoded string.

The encoding rule is: k[encoded_string] , where the  encoded_string inside the square brackets is being repeated exactly  k times. Note that  k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k . For example, there won’t be input like  3a or  2[4] .

Examples:

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
func decodeString(_ s: String) -> String {
    var stack = [String]()
    s.forEach {
        switch $0 {
        case "]":
            var s = "", i = ""
            while let c = stack.popLast(), c != "[" {
                s = "\(c)\(s)"
            }
            while let sl = stack.last, nil != Int("\(sl)"), let c = stack.popLast() {
                i = "\(c)\(i)"
            }
            stack.append(String(repeating: s, count: Int(i)!))
        default:
            stack.append(String($0))
        }
    }
 
    return stack.joined()
}
[swift] LeetCode 394. Decode String [swift] LeetCode 394. Decode String

(随着网站访问量的激增,服务器配置只得一再升级以维持网站不“404 Not Found”,所以网站的维护费用也在不断上涨……(目前的阿里云服务器ECS+云数据库RDS+域名购买+七牛云的费用是2200元/年),为了能不放弃该网站,所以我又把打赏链接放上来啦~所有打赏金额都会被记账并投入博客维护中,感谢厚爱,多多关照~)


以上所述就是小编给大家介绍的《[swift] LeetCode 394. Decode String》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

构建高性能Web站点

构建高性能Web站点

郭欣 / 电子工业出版社 / 2012-6 / 75.00元

《构建高性能Web站点(修订版)》是畅销修订版,围绕如何构建高性能Web站点,从多个方面、多个角度进行了全面的阐述,几乎涵盖了Web站点性能优化的所有内容,包括数据的网络传输、服务器并发处理能力、动态网页缓存、动态网页静态化、应用层数据缓存、分布式缓存、Web服务器缓存、反向代理缓存、脚本解释速度、页面组件分离、浏览器本地缓存、浏览器并发请求、文件的分发、数据库I/O优化、数据库访问、数据库分布式......一起来看看 《构建高性能Web站点》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换