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

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

1.模拟

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.数学

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

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) // 2
        return pre_odd(high) - pre_odd(low - 1)

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

遍历

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.遍历+哈希表。

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.排序+遍历

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))。

class Solution:
    def isPossibleToSplit(self, nums: List[int]) -> bool:
        # 每一个元素最多只能出现2次
        # Counter函数1
        return max(Counter(nums).values()) <= 2

4. Counter函数2。来自灵神题解。

class Solution:
    def isPossibleToSplit(self, nums: List[int]) -> bool:
        # 每一个元素最多只能出现2次
        # Counter函数2
        return all(x <= 2 for x in Counter(nums).values())

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

遍历

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

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

相关推荐

  1. 2024.3.251200-1400记录

    2024-04-01 10:46:01       16 阅读
  2. 2024.3.271200-1400记录

    2024-04-01 10:46:01       19 阅读
  3. 2024.3.311200-1400记录

    2024-04-01 10:46:01       17 阅读
  4. 2024.4.11200-1400记录

    2024-04-01 10:46:01       18 阅读
  5. 100笔记[python]

    2024-04-01 10:46:01       12 阅读
  6. hot100笔记Day1

    2024-04-01 10:46:01       38 阅读
  7. hot100笔记Day4

    2024-04-01 10:46:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-01 10:46:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-01 10:46:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-01 10:46:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-01 10:46:01       20 阅读

热门阅读

  1. 著名的分布式数据库

    2024-04-01 10:46:01       14 阅读
  2. 从适用场景看,Spring Boot和Spring的不同

    2024-04-01 10:46:01       15 阅读
  3. Servlet

    Servlet

    2024-04-01 10:46:01      15 阅读
  4. Spring Boot集成Elasticsearch 8.12.2客户端

    2024-04-01 10:46:01       15 阅读
  5. ZooKeeper 负载均衡和 Nginx 负载均衡的区别

    2024-04-01 10:46:01       15 阅读
  6. Docker Swarm入门

    2024-04-01 10:46:01       12 阅读
  7. Redis 的常见问题及解决方案

    2024-04-01 10:46:01       19 阅读
  8. Meme币如何赋能Web3社交?

    2024-04-01 10:46:01       16 阅读
  9. 价值投资已死,MEME币永生?

    2024-04-01 10:46:01       18 阅读
  10. 证券市场概述

    2024-04-01 10:46:01       17 阅读