class Solution {
public:
vector<int> swapNumbers(vector<int>& numbers) {
}
};
面试题 16.01. 交换数字
编写一个函数,不用临时变量,直接交换numbers = [a, b]
中a
与b
的值。
示例:
输入: numbers = [1,2] 输出: [2,1]
提示:
numbers.length == 2
-2147483647 <= numbers[i] <= 2147483647
原站题解
golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2021-06-08 14:21:37
func swapNumbers(numbers []int) []int { numbers[0] = numbers[0] ^ numbers[1] numbers[1] = numbers[0] ^ numbers[1] numbers[0] = numbers[0] ^ numbers[1] return numbers }
golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2021-06-08 14:20:43
func swapNumbers(numbers []int) []int { numbers[0], numbers[1] = numbers[1], numbers[0] return numbers }