zlx362211854 / daily-study

每日一个知识点总结,以issue的形式体现
10 stars 6 forks source link

134. 两个数反转 #191

Open zlx362211854 opened 4 years ago

zlx362211854 commented 4 years ago
var a = 2;
var b = 5;

// 如何不使用第三个变量,使两个数反转,即a = 5, b = 2
zlx362211854 commented 4 years ago
 // 方法1:
var a = 2;
var b = 5;
[a,b] = [b,a]
 // 方法2(只适用a,b都是数字的情况):
var a = 2;
var b = 5;
a = b - a // 5 - 2 = 3
b = b - a // 5 - 3 = 2
a = a + b // 3 + 2 = 5