1523. Count Odd Numbers in an Interval Range 链接到标题 数学题,数据范围是10^9,O(n) 会超时,统计 high 中有多少个奇数,减去 low-1 中的奇数个数,需要考虑边界条件:low/high 自身为奇数情况。
class Solution: def countOdds(self, low: int, high: int) -> int: res = high // 2 if high % 2 == 1: res += 1 if low - 1 >= 0: res -= (low - 1) // 2 if (low - 1) % 2 == 1: res -= 1 return res 1528. Shuffle String 链接到标题 按照指定顺序调整字符串,直接定义一个新的长度为 len(s)字符串,然后按照 indices 的索引顺序进行对应调整。