leetcode0-global的用法

背景:46. 全排列

  • 总结:全局变量如果使用global在局部初始化,不要忘记赋初值
  • 在类方法的局部函数中进行全局计数,可以采用设置全局变量和实例变量,这两种方式。

1,一段会报错的代码

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        length = len(nums)
        count = 0  #这里的count只是定义在类的方法中的一个局部变量,不是全局变量(局部变量count有初值0,全局变量count没有)

        def backtrack(track, choiceList):
            global count  #就算这里将count使用global修饰,但是全局变量count未赋值
            count += 1    #由于count未赋值,所以报错
            print(count)

            if len(track) == length:
                result.append(track)
                return 

            for num in choiceList:
                track.append(num)
                choiceList.remove(num)
                backtrack(track, choiceList)
                track.remove(num)
                choiceList.append(num)
        
        backtrack([], nums)
        return result

修改方法1

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        length = len(nums)
        global count #将局部变量count设置为全局变量
        count = 0    #给全局变量count赋初值

        def backtrack(track, choiceList):
            global count  #将count使用global修饰
            count += 1    #由于count有初始赋值,所以不会报错
            print(count)

            if len(track) == length:
                result.append(track)
                return 

            for num in choiceList:
                track.append(num)
                choiceList.remove(num)
                backtrack(track, choiceList)
                track.remove(num)
                choiceList.append(num)
        
        backtrack([], nums)
        return result

修改方法2

count = 0    #这一步相当于设置全局变量count,并赋初值0

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        length = len(nums)

        def backtrack(track, choiceList):
            global count  #将count使用global修饰
            count += 1    #由于count有初始赋值,所以不会报错
            print(count)

            if len(track) == length:
                result.append(track)
                return 

            for num in choiceList:
                track.append(num)
                choiceList.remove(num)
                backtrack(track, choiceList)
                track.remove(num)
                choiceList.append(num)
        
        backtrack([], nums)
        return result

修改方法3

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        length = len(nums)
        self.count = 0    #将count定义为实例变量

        def backtrack(track, choiceList):
            self.count += 1    #使用实例变量进行计数
            print(self.count)

            if len(track) == length:
                result.append(track)
                return 

            for num in choiceList:
                track.append(num)
                choiceList.remove(num)
                backtrack(track, choiceList)
                track.remove(num)
                choiceList.append(num)
        
        backtrack([], nums)
        return result

leetcode0-global的用法

上一篇:7网络与通信-


下一篇:LVS+Keepalived 高可用群集部署