Python 解LeetCode:394 Decode String

  • 题目描述:按照规定,把字符串解码,具体示例见题目链接
  • 思路:使用两个栈分别存储数字和字母
  • 注意1: 数字是多位的话,要处理后入数字栈
  • 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈
  • 注意3: 记得字母出栈的时候字符要逆序组合成字符串
  • 注意4: 不用字符串而用字母栈的原因是字符串的 join 效率会比字符串加法高一些
  • 结果: 30 ms, beat 98.02%
  • 缺点:判断是数字那里有点代码不简洁,可以把 j 挪到循环外面的
class Solution(object):
    def decodeString(self, s):
        """
        :type s: str
        :rtype: str
        """
        nums, chars = [], []
        i, length = 0, len(s)
        while i < length:
            j = i + 1
            if s[i].isdigit():
                num = int(s[i])
                while j < length:
                    if s[j].isdigit():
                        num = num * 10 + int(s[j])
                        j += 1
                    else:
                        break
                nums.append(num)
            elif s[i] == ‘[‘ or s[i].isalpha():
                chars.append(s[i])
            else:
                t, tmpc = chars.pop(), []
                while t != ‘[‘:
                    tmpc.append(t)
                    t = chars.pop()
                tchars = nums.pop() * ‘‘.join(tmpc[::-1])
                chars.append(tchars)
            i = j
        return ‘‘.join(chars)

原文地址:https://www.cnblogs.com/qiaojushuang/p/9048675.html

时间: 2024-11-06 07:50:44

Python 解LeetCode:394 Decode String的相关文章

Leetcode 394: Decode String

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 th

Python 解leetcode:48. Rotate Image

题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place inst

394. Decode String

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 thatk is guaranteed to be a positive integer. You may assume tha

Python解Leetcode: 226. Invert Binary Tree

leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): d

Python解Leetcode: 539. Minimum Time Difference

题目描述:给定一个由时间字符组成的列表,找出任意两个时间之间最小的差值. 思路: 把给定的链表排序,并且在排序的同时把60进制的时间转化成十进制整数: 遍历排序的数组,求出两个相邻值之间的差值: 求出首尾两个值之间的差值. class Solution(object): def findMinDifference(self, timePoints): """ :type timePoints: List[str] :rtype: int """

394. Decode String 解码icc字符串3[i2[c]]

[抄题]: 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 ass

[LC] 394. Decode String

Given an encoded string, return its 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 tha

Python 解leetcode:3. Longest Substring Without Repeating Characters

题目描述:求一个字符串的不含重复字符的最长连续子串的长度: 思路: 使用一个哈希表保存字符出现的位置: 使用left和right分别表示子串的最左和最右字符的下标: 遍历字符串,如果当前字符在哈希表中并且当前字符在哈希中的位置大于left,表明left和right之间存在和right索引上相同的字符,此时把left置为当前值在哈希表中的值: 把当前索引值+1,表示是第几个字符,求出当前最大长度: 3-4步重复,直到获得遍历完成: 感觉这是个动态规划题,暂时没用动态规划分析,后续再说. class

Python 解leetcode:49. Group Anagrams

题目描述:给出一个由字符串组成的数组,把数组中字符串的组成字母相同的部分放在一个数组中,并把组合后的数组输出: 思路: 使用一个字典,键为数组中字符串排序后的部分,值为排序后相同的字符串组成的列表: 遍历数组完成后,返回字典的值就可以了. class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] ""&qu