Skip to content

Latest commit

 

History

History
50 lines (38 loc) · 1.39 KB

L0 G2000.md

File metadata and controls

50 lines (38 loc) · 1.39 KB

LeetCode 383

def str2dict(str):
        dict = {}
        for it in str:
            if it not in dict:
                dict[it] = 1
            else:
                dict[it] += 1
        return dict

class Solution(object):
    def canConstruct(self, ransomNote, magazine):
        """
        :type ransomNote: str
        :type magazine: str
        :rtype: bool
        """
        dt1 = str2dict(ransomNote)
        dt2 = str2dict(magazine)
        for key in dt1:
            if key not in dt2:
                return False
            elif dt1[key] > dt2[key]:
                return False
        return True

image

debug

  1. 安装openai依赖
  2. 直接运行发现报错,报错发生在json.loads(res)上面,在这里打断点
  3. 看中间的res变量,发现是有JSON之外的符号,导致了解析失败
  4. 处理res变量,掐头去尾,去掉额外的字符,再进行解析

报错:

HWI@ UO37{RQY9C BSW2L

debug:

0G~HPX 0Q8@R9$S69 HRN

在json解析之前,加上res = res.strip('```json\n').strip('\n```'):

{}STQ67DK1H3@5X0`VS7L{3