Python 是 |比 or 运算符慢?
对于这个Leetcode问题,似乎如果我使用以下代码,它会通过在线判断:
Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Example 1:
Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
1 <= nums.length <= 200
1 <= nums[i] <= 100
解决方案:
class Solution:
def canPartition(self, nums: List[int]) -> bool:
@cache
def dp(i, s):
if s == 0:
return True
if i == 0 or s<0:
return False
if nums[i]<=s:
return dp(i-1, s) or dp(i-1, s-nums[i])
else:
return dp(i-1, s)
total = sum(nums)
if total%2 != 0:
return False
half = total//2
return dp(len(nums)-1, half)
但是,如果我将 dp 函数中的 or
运算符更改为 |
,算法将遇到 TLE(Time Limited Exceeded),虽然答案仍然正确:
class Solution:
def canPartition(self, nums: List[int]) -> bool:
@cache
def dp(i, s):
if s == 0:
return True
if i == 0 or s<0:
return False
if nums[i]<=s:
return dp(i-1, s) | dp(i-1, s-nums[i])
else:
return dp(i-1, s)
total = sum(nums)
if total%2 != 0:
return False
half = total//2
return dp(len(nums)-1, half)
我知道 |
用于二进制运算。但这是否意味着 |
如果应用于布尔值会很慢?
For this Leetcode question, it seems that if I use the following code, it will pass the online judge:
Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Example 1:
Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
1 <= nums.length <= 200
1 <= nums[i] <= 100
Solution:
class Solution:
def canPartition(self, nums: List[int]) -> bool:
@cache
def dp(i, s):
if s == 0:
return True
if i == 0 or s<0:
return False
if nums[i]<=s:
return dp(i-1, s) or dp(i-1, s-nums[i])
else:
return dp(i-1, s)
total = sum(nums)
if total%2 != 0:
return False
half = total//2
return dp(len(nums)-1, half)
However, if I change the or
operator in the dp function to |
, the algorithm will run into TLE (Time Limited Exceeded), although the answers are still correct:
class Solution:
def canPartition(self, nums: List[int]) -> bool:
@cache
def dp(i, s):
if s == 0:
return True
if i == 0 or s<0:
return False
if nums[i]<=s:
return dp(i-1, s) | dp(i-1, s-nums[i])
else:
return dp(i-1, s)
total = sum(nums)
if total%2 != 0:
return False
half = total//2
return dp(len(nums)-1, half)
I know |
is meant for binary operations. But does this mean that |
is slow if applied to boolean values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为@khelwood 和@luk2302 是对的。
或
短路,|
则不然。换句话说,如果第一个深度优先搜索为 True,则or
将停止评估第二个深度优先搜索,而|
始终评估两者,导致堆栈过长,导致内存爆炸。I think @khelwood and @luk2302 are right.
or
short circuits,|
does not. In other words,or
will stop evaluating the second depth-first-search if the first is True, while|
always evaluate both, resulting an overly long stack that exploded the memory.