小编aba*_*awi的帖子

集合上最小值、最大值的时间复杂度

min, max 的时间复杂度为 O(N),因为它们必须遍历给定的列表/字符串并检查每个索引以找到最小值/最大值。但我想知道如果在集合上使用 min,max 的时间复杂度是多少?例如:

s = {1,2,3,4} # s is a set
Run Code Online (Sandbox Code Playgroud)

使用最小值/最大值我们得到:

min(s) = 1
max(s) = 4
Run Code Online (Sandbox Code Playgroud)

由于集合不使用列表和字符串之类的索引,而是使用可以直接访问的桶进行操作,因此 min/max 的时间复杂度是否与一般情况不同?

谢谢!

python

6
推荐指数
1
解决办法
7208
查看次数

java中的原始数据类型是如何定义/编写的?

例如,是否有int.javadouble.java 之类的东西?

由于原始数据类型是 Java 语言的构建块,我假设它们会用某种较低级别的语言编写。

  1. Java 中的原始数据类型是用什么语言编写的?

  2. 可以访问这些文件并查看原始数据类型(如 int)是如何定义的吗?

java computer-science primitive-types data-structures

5
推荐指数
1
解决办法
728
查看次数

list.count() 与 Counter() 性能

在尝试查找字符串中一堆字符的频率时,为什么对 4 个不同的字符运行 string.count(character) 4 次会比使用 collections.Counter(string) 产生更快的执行时间(使用 time.time()) )?

背景:给定由字符串表示的一系列动作。有效移动为 R(右)、L(左)、U(上)和 D(下)。如果移动顺序带我回到原点,则返回 True。否则,返回 false。


# approach - 1 : iterate 4 times (3.9*10^-6 seconds)
def foo1(moves):
    return moves.count('U') == moves.count('D') and moves.count('L') == moves.count('R')

# approach - 2 iterate once (3.9*10^-5 seconds)
def foo2(moves): 
    from collections import Counter
    d = Counter(moves)
    return d['R'] == d['L'] and d['U'] == d['D']

import time
start = time.time()
moves = "LDRRLRUULRLRLRLRLRLRLRLRLRLRL"
foo1(moves)
# foo2(moves)
end = time.time()
print("--- %s seconds ---" % (end …
Run Code Online (Sandbox Code Playgroud)

counter performancecounter count performance-testing python-3.x

3
推荐指数
1
解决办法
3397
查看次数