相关疑难解决方法(0)

python a,b = b,一个实现?它与C++交换函数有什么不同?

当我想尝试python版本时遇到了这个问题:https://leetcode.com/problems/first-missing-positive/discuss/17071/My-short-c++-solution-O( 1)-space- and -准时

我不确定为什么a[0], a[a[0]] = a[a[0]], a[0]这个不进行交换?

>>> nums
[2, 1, 0]
>>> a = [2,1,0]
>>> a[0], a[a[0]] = a[a[0]], a[0]
>>> a
[2, 1, 0]
>>> a[0]
2
>>> a[0],a[2] = a[2], a[0]
>>> a
[0, 1, 2]
Run Code Online (Sandbox Code Playgroud)

我的猜测是a,b = b,语法的实现类似于:

tmp = a[0] (tmp = 2)
a[0]  = a[a[0]] (a[0] = a[2] = 0)
a[a[0]] = tmp (a[a[0]] = a[0] = tmp = 2)
Run Code Online (Sandbox Code Playgroud)

然后我检查了C++中swap函数的实现.我对C++一无所知,但看起来这个想法是一样的:http: //www.cplusplus.com/reference/algorithm/swap/

The behavior …
Run Code Online (Sandbox Code Playgroud)

python algorithm

15
推荐指数
2
解决办法
905
查看次数

标签 统计

algorithm ×1

python ×1