我想弄清楚这个算法的时间复杂度。A 是数组输入。顺便说一下,代码没有运行,只是为了演示目的。
def func(A):
result = 0
n = len(A)
while n > 1:
n = n/2
result = result + min(A[1,...,n])
return result
Run Code Online (Sandbox Code Playgroud)
这假设数组 A 的长度为 n。
我假设它的时间复杂度为 O(n(log n)),因为 while 循环的复杂度为 O(log n),而 min 函数的复杂度为 O(n)。然而,这个函数的复杂度显然是 O(n) 而不是 O(n(log n))。我想知道这怎么可能?
真快速而简单的问题.
我喜欢使用{} .format,即使我没有格式化变量本身,如:
print("Welcome to {}, {} {}!".format(location,firstname,lastname))
Run Code Online (Sandbox Code Playgroud)
因为这意味着我没有在字符串和变量之间放置逗号,并且可以直接控制字符串中的间距.但是,即使只使用一个变量,我仍然会这样做
print("I am at {} right now.".format(location))
Run Code Online (Sandbox Code Playgroud)
使用这样的格式有什么问题吗?
有没有更好的方法来做同样的事情?
我已经简化了大部分代码,因为问题在于 Promise 和 async/await 部分。
我希望这个组件从我的 API 获取数据并从中创建一个图。如果 API 仍在检索,我希望它显示加载图标。
class Data extends Component {
state = {};
componentDidMount() {
this.setState({ data: this.getData() });
}
async getData() {
try {
const response = await axios.get('/api/data');
return response.data;
} catch (err) {
return [];
}
}
renderLoading() {
return <Loading/>; // this represents a loading icon
}
renderPlot(data) {
return <Plot data={data}/>; // this represents the plot that needs the data
}
render() {
return {this.state.data
? this.renderLoading()
: this.renderPlot(this.state.data)};
} …Run Code Online (Sandbox Code Playgroud)