1.遍历
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
# 遍历
n = len(nums)
ans = [0] * (2*n)
for i in range(n):
ans[i] = nums[i]
ans[i+n] = nums[i]
return ans
2.插入数组
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
# 插入数组
ans = nums
ans.extend(nums) #extend()时间复杂度O(n)
return ans
两种方式时间复杂度相同。
3.数组拼接
另外还看到别人的另一种解法,时间复杂度也为O(n),如下:
class Solution:
def getConcatenation(self, nums: List[int]) -> List[int]:
return nums + nums
# return nums * 2
# 返回新列表,原列表不变
排序
class Solution:
def triangleType(self, nums: List[int]) -> str:
if nums[0] == nums[1] == nums[2]:
return "equilateral"
nums.sort()
if nums[0] + nums[1] > nums[2]:
# 排序后只需比较两小边的和大不大于第三边
if nums[0] == nums[1] or nums[1] == nums[2]:
return "isosceles"
return "scalene"
return "none"
1.转化为字符串,长度即为位数
class Solution:
def findNumbers(self, nums: List[int]) -> int:
# 转化为字符串
chars = list(map(str,nums))
ans = 0
for c in chars:
if len(c) % 2 == 0:
ans += 1
return ans
官方题解(. - 力扣(LeetCode))中同一方法的另一种写法:
class Solution:
def findNumbers(self, nums: List[int]) -> int:
# 转化为字符串 生成器
return sum(1 for num in nums if len(str(num)) % 2 == 0)
# return sum(len(str(num)) % 2 == 0 for num in nums)
2.运用数学知识取对数
这是在官方题解中看到的解法,学习一下:
class Solution:
def findNumbers(self, nums: List[int]) -> int:
# 运用数学知识,取以10为底的对数
return sum(int(math.log10(num) + 1) % 2 == 0 for num in nums) #int()向下取整,所以+1
3.特殊解法(仅针对该题)
看到官方题解下“sin猪”用户的评论(. - 力扣(LeetCode)),简直太妙了哈哈哈,记录一下。因为题目给出了范围,可以直接将情况列出来。代码如下:
class Solution:
def findNumbers(self, nums: List[int]) -> int:
# 特殊解法
return sum(10 <= num <= 99 or 1000 <= num <= 9999 or num == 100000 for num in nums)
1.遍历法
先假设所有的数均为不能被m整除的数,那么num1-num2就等于前n项等差数列求和。然后再遍历每一个数,一旦有能被m整除的数,ans就减去2倍i(因为num1-i,num2+i,结果就-2i)。代码如下:
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
# 遍历
ans = (1+n)*n//2
for i in range(1,n+1):
if i % m == 0:
ans -= 2*i
return ans
2.数学公式
这是灵神的方法(. - 力扣(LeetCode)),时复O(1)。来学习一下:
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
# 灵神代码 数学公式
return n*(n+1)//2 - (n//m)*(n//m+1)*m
1.遍历
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
# 遍历
ans = 0
for i in range(len(hours)):
if hours[i] >= target:
ans += 1
return ans
2.排序+遍历
虽然我这样用排序好像用处不太大,但是想到了就写了(不过运行速度要比直接遍历快)。
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
# 排序,遍历
hours.sort()
for i in range(len(hours)):
if hours[i] >= target:
return len(hours) - i
return 0
3.排序+查找函数
受下面这段代码的启发(未知用户):
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
hours.append(target)
hours.sort()
return len(hours)-hours.index(target)-1
更改代码如下:
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
# 排序,函数查找
hours.sort()
return len(hours) - bisect_left(hours,target)
bisect_left()函数时复为O(logn)。
1.遍历+移动窗口(动态规划)
class Solution:
def tribonacci(self, n: int) -> int:
# 遍历+移动窗口
if n == 0:
return 0
if n == 1 or n == 2:
return 1
ans = [0] * 3
ans[0],ans[1],ans[2] = 0,1,1
for i in range(n-2):
ans[0],ans[1],ans[2] = ans[1],ans[2],sum(ans)
return ans[2]
2024.3.10续:
2.打表
学习参考宫水三叶题解(. - 力扣(LeetCode)),代码如下:
class Solution:
# 打表
# 在这里写为静态变量,避免多次打表
# 通过写为类变量来模拟静态变量
cache = [0] * 38 # n<= 37
cache[0] = 0
cache[1] = 1
cache[2] = 1
for i in range(3,38):
cache[i] = cache[i-1] + cache[i-2] + cache[i-3]
def tribonacci(self, n: int) -> int:
return Solution.cache[n]
cache
是类变量,它在整个类的生命周期内只会被初始化一次。每个实例对象共享同一个 cache
数组,所以无论创建多少个实例对象,它们都会共享相同的 cache
数组。每个实例对象调用 tribonacci
函数时,都可以直接访问该共享的 cache
数组,而不会重新计算。这样可以节省内存,并且避免重复计算。————来自chatgpt
在python中模拟静态变量,学习记录:http://t.csdnimg.cn/SwwSl
3.矩阵快速幂
(仍在学习中)