网站布局案例,北京有哪些电商平台公司,新商盟显示 检查网站开发错误呢,凡客整装一、1523. 在区间范围内统计奇数数目
1.模拟
class Solution:def countOdds(self, low: int, high: int) - int:# 模拟return len(range(low,high1,2)) if low 1 else len(range(low1,high1,2))
2.数学
总结规律。首为偶数就向下取整#xff1b;奇数就向上取整。…一、1523. 在区间范围内统计奇数数目
1.模拟
class Solution:def countOdds(self, low: int, high: int) - int:# 模拟return len(range(low,high1,2)) if low 1 else len(range(low1,high1,2))
2.数学
总结规律。首为偶数就向下取整奇数就向上取整。注意整数向上向下取整值相同。
class Solution:def countOdds(self, low: int, high: int) - int:# 数学return (high - low 1) // 2 if low % 2 0 else ceil((high - low 1) / 2)
3.前缀和。来自官方题解. - 力扣LeetCode。
class Solution:def countOdds(self, low: int, high: int) - int:# 前缀和# 前low-1包含的奇数 - 前high包含的奇数从0开始def pre_odd(num):return (num 1) // 2return pre_odd(high) - pre_odd(low - 1) 二、1822. 数组元素积的符号
遍历
class Solution:def arraySign(self, nums: List[int]) - int:# 遍历# 有0为0无0统计负数的个数cnt 0for x in nums:if x 0:return 0if x 0:cnt 1return -1 if cnt 1 else 1
三、3046. 分割数组
1.遍历哈希表。
class Solution:def isPossibleToSplit(self, nums: List[int]) - bool:# 每一个元素最多只能出现2次# 遍历哈希表# 时复O(n),空复O(101)hash_list [0] * 101for x in nums:if hash_list[x] 2:return Falsehash_list[x] 1return True
2.排序遍历
class Solution:def isPossibleToSplit(self, nums: List[int]) - bool:# 每一个元素最多只能出现2次# 排序遍历# 时复O(nlogn),空复O(1)nums.sort()flag 0pre nums[0]for i in range(1,len(nums)):if flag and nums[i] pre:return Falseif nums[i] pre:flag 1 #出现两次标记为1else:flag 0pre nums[i]return True
3.Counter函数1。老忘记有这函数来自灵神题解. - 力扣LeetCode。
class Solution:def isPossibleToSplit(self, nums: List[int]) - bool:# 每一个元素最多只能出现2次# Counter函数1return max(Counter(nums).values()) 2
4. Counter函数2。来自灵神题解。
class Solution:def isPossibleToSplit(self, nums: List[int]) - bool:# 每一个元素最多只能出现2次# Counter函数2return all(x 2 for x in Counter(nums).values()) 四、1413. 逐步求和得到正数的最小值
遍历
class Solution:def minStartValue(self, nums: List[int]) - int:# 遍历# 求出最小前n项和s 0mins inffor x in nums:s xmins min(mins, s) #更新前n和最小值return 1 - mins if mins 1 else 1
完
感谢你看到这里一起加油吧