2024.3.31力扣(1200-1400)刷题记录

一、1523. 在区间范围内统计奇数数目

1.模拟

python 复制代码
class Solution:
    def countOdds(self, low: int, high: int) -> int:
        # 模拟
        return len(range(low,high+1,2)) if low & 1 else len(range(low+1,high+1,2))

2.数学

总结规律。首为偶数就向下取整;奇数就向上取整。注意整数向上向下取整值相同。

python 复制代码
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))。

python 复制代码
class Solution:
    def countOdds(self, low: int, high: int) -> int:
        # 前缀和
        # 前low-1包含的奇数 - 前high包含的奇数,从0开始
        def pre_odd(num):
            return (num + 1) // 2
        return pre_odd(high) - pre_odd(low - 1)

二、1822. 数组元素积的符号

遍历

python 复制代码
class Solution:
    def arraySign(self, nums: List[int]) -> int:
        # 遍历
        # 有0为0,无0统计负数的个数
        cnt = 0
        for x in nums:
            if x == 0:
                return 0
            if x < 0:
                cnt += 1
        return -1 if cnt & 1 else 1

三、3046. 分割数组

1.遍历+哈希表。

python 复制代码
class Solution:
    def isPossibleToSplit(self, nums: List[int]) -> bool:
        # 每一个元素最多只能出现2次
        # 遍历+哈希表
        # 时复O(n),空复O(101)
        hash_list = [0] * 101
        for x in nums:
            if hash_list[x] == 2:
                return False
            hash_list[x] += 1
        return True

2.排序+遍历

python 复制代码
class Solution:
    def isPossibleToSplit(self, nums: List[int]) -> bool:
        # 每一个元素最多只能出现2次
        # 排序+遍历
        # 时复O(nlogn),空复O(1)
        nums.sort()
        flag = 0
        pre = nums[0]
        for i in range(1,len(nums)):
            if flag and nums[i] == pre:
                return False
            if nums[i] == pre:
                flag = 1    #出现两次标记为1
            else:
                flag = 0
            pre = nums[i]
        return True

3.Counter函数1。老忘记有这函数,来自灵神题解(. - 力扣(LeetCode))。

python 复制代码
class Solution:
    def isPossibleToSplit(self, nums: List[int]) -> bool:
        # 每一个元素最多只能出现2次
        # Counter函数1
        return max(Counter(nums).values()) <= 2
  1. Counter函数2。来自灵神题解。
python 复制代码
class Solution:
    def isPossibleToSplit(self, nums: List[int]) -> bool:
        # 每一个元素最多只能出现2次
        # Counter函数2
        return all(x <= 2 for x in Counter(nums).values())

四、1413. 逐步求和得到正数的最小值

遍历

python 复制代码
class Solution:
    def minStartValue(self, nums: List[int]) -> int:
        # 遍历
        # 求出最小前n项和
        s = 0
        mins = inf
        for x in nums:
            s += x
            mins = min(mins, s)     #更新前n和最小值
        return 1 - mins if mins < 1 else 1

感谢你看到这里!一起加油吧!

相关推荐
QuantumStack15 分钟前
【C++ 真题】P1104 生日
开发语言·c++·算法
写个博客1 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠1 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
笑稀了的野生俊1 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
Naiva1 小时前
【小技巧】Python+PyCharm IDE 配置解释器出错,环境配置不完整或不兼容。(小智AI、MCP、聚合数据、实时新闻查询、NBA赛事查询)
ide·python·pycharm
hie988942 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
路来了2 小时前
Python小工具之PDF合并
开发语言·windows·python
杰克尼2 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表
蓝婷儿2 小时前
Python 机器学习核心入门与实战进阶 Day 3 - 决策树 & 随机森林模型实战
人工智能·python·机器学习
AntBlack2 小时前
拖了五个月 ,不当韭菜体验版算是正式发布了
前端·后端·python