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

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

相关推荐
BUG收容所所长21 分钟前
栈的奇妙世界:从冰棒到算法的华丽转身
前端·javascript·算法
有风南来22 分钟前
算术图片验证码(四则运算)+selenium
自动化测试·python·selenium·算术图片验证码·四则运算验证码·加减乘除图片验证码
wangjinjin18022 分钟前
Python Excel 文件处理:openpyxl 与 pandas 库完全指南
开发语言·python
XRZaaa27 分钟前
常见排序算法详解与C语言实现
c语言·算法·排序算法
@我漫长的孤独流浪30 分钟前
数据结构测试模拟题(4)
数据结构·c++·算法
智驱力人工智能34 分钟前
智慧零售管理中的客流统计与属性分析
人工智能·算法·边缘计算·零售·智慧零售·聚众识别·人员计数
Yxh181377845541 小时前
抖去推--短视频矩阵系统源码开发
人工智能·python·矩阵
WindSearcher2 小时前
大模型微调相关知识
后端·算法
Humbunklung2 小时前
PySide6 GUI 学习笔记——常用类及控件使用方法(多行文本控件QTextEdit)
笔记·python·学习·pyqt
取酒鱼食--【余九】2 小时前
rl_sar实现sim2real的整体思路
人工智能·笔记·算法·rl_sar