编写一个函数,不用临时变量,直接交换numbers = [a, b]中a与b的值。
示例:
输入: numbers = [1,2]
输出: [2,1]
提示:
- numbers.length == 2
Python 解答:
1.Python特性
class Solution:
def swapNumbers(self, numbers: List[int]) -> List[int]:
numbers[0], numbers[1] = numbers[1], numbers[0]
return numbers
2.轮换
class Solution:
def swapNumbers(self, numbers: List[int]) -> List[int]:
numbers[0] = numbers[0] - numbers[1]
numbers[1] = numbers[0] + numbers[1]
numbers[0] = numbers[1] - numbers[0]
return numbers
留言