理解作用域 - Leetcode 1640
Understanding scoping - Leetcode 1640
我正在尝试 Leetcode 挑战 1640 - found here
这是我第一次尝试解决这个问题。但是我一直收到运行时错误,因为它说
local variable "found" referenced before assignment
我认为外部作用域变量可以被内部作用域访问。我不明白为什么我会遇到这个问题。
请你帮忙。谢谢
class Solution(object):
def canBeEqual(self, target, arr):
s = set()
found = False
def reverse(target, arr):
global s, found
if found:
return found
if arr == target:
found = True
return found
if tuple(arr) in s: return False
s.add((tuple(arr)))
for start in range(len(arr)):
for end in range(start + 1, len(arr)):
arr = arr[:start] + arr[start:end+1][::-1] + arr[end+1:]
reverse(target, arr)
return found
return (reverse(target, arr))
为了解决这个问题,我们可以使用 collection.Counter
:
class Solution:
def canBeEqual(self, target, arr):
return collections.Counter(target) == collections.Counter(arr)
- 我们可以将
global
更改为 nonlocal
for found
:
from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue
from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter
class Solution(object):
def canBeEqual(self, target, arr):
s = set()
found = False
def reverse(target, arr):
nonlocal found
if found:
return found
if arr == target:
found = True
return found
if tuple(arr) in s:
return False
s.add((tuple(arr)))
for start in range(len(arr)):
for end in range(start + 1, len(arr)):
arr = arr[:start] + arr[start:end + 1][::-1] + arr[end + 1:]
reverse(target, arr)
return found
return reverse(target, arr)
print(Solution().canBeEqual(target = [1, 2, 3, 4], arr = [2, 4, 1, 3]))
解释一下in this post
我们也可以简单地使用self
:
from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue
from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter
class Solution(object):
def canBeEqual(self, target, arr):
s = set()
self.found = False
def reverse(target, arr):
if self.found:
return self.found
if arr == target:
self.found = True
return self.found
if tuple(arr) in s:
return False
s.add((tuple(arr)))
for start in range(len(arr)):
for end in range(start + 1, len(arr)):
arr = arr[:start] + arr[start:end + 1][::-1] + arr[end + 1:]
reverse(target, arr)
return self.found
return reverse(target, arr)
print(Solution().canBeEqual(target = [1, 2, 3, 4], arr = [2, 4, 1, 3]))
我正在尝试 Leetcode 挑战 1640 - found here
这是我第一次尝试解决这个问题。但是我一直收到运行时错误,因为它说
local variable "found" referenced before assignment
我认为外部作用域变量可以被内部作用域访问。我不明白为什么我会遇到这个问题。
请你帮忙。谢谢
class Solution(object):
def canBeEqual(self, target, arr):
s = set()
found = False
def reverse(target, arr):
global s, found
if found:
return found
if arr == target:
found = True
return found
if tuple(arr) in s: return False
s.add((tuple(arr)))
for start in range(len(arr)):
for end in range(start + 1, len(arr)):
arr = arr[:start] + arr[start:end+1][::-1] + arr[end+1:]
reverse(target, arr)
return found
return (reverse(target, arr))
为了解决这个问题,我们可以使用 collection.Counter
:
class Solution:
def canBeEqual(self, target, arr):
return collections.Counter(target) == collections.Counter(arr)
- 我们可以将
global
更改为nonlocal
forfound
:
from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue
from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter
class Solution(object):
def canBeEqual(self, target, arr):
s = set()
found = False
def reverse(target, arr):
nonlocal found
if found:
return found
if arr == target:
found = True
return found
if tuple(arr) in s:
return False
s.add((tuple(arr)))
for start in range(len(arr)):
for end in range(start + 1, len(arr)):
arr = arr[:start] + arr[start:end + 1][::-1] + arr[end + 1:]
reverse(target, arr)
return found
return reverse(target, arr)
print(Solution().canBeEqual(target = [1, 2, 3, 4], arr = [2, 4, 1, 3]))
解释一下in this post
我们也可以简单地使用self
:
from typing import List
import collections
import itertools
import functools
import math
import string
import random
import bisect
import re
import operator
import heapq
import queue
from queue import PriorityQueue
from itertools import combinations, permutations
from functools import lru_cache
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from collections import Counter
class Solution(object):
def canBeEqual(self, target, arr):
s = set()
self.found = False
def reverse(target, arr):
if self.found:
return self.found
if arr == target:
self.found = True
return self.found
if tuple(arr) in s:
return False
s.add((tuple(arr)))
for start in range(len(arr)):
for end in range(start + 1, len(arr)):
arr = arr[:start] + arr[start:end + 1][::-1] + arr[end + 1:]
reverse(target, arr)
return self.found
return reverse(target, arr)
print(Solution().canBeEqual(target = [1, 2, 3, 4], arr = [2, 4, 1, 3]))