Swap two variables without intermediate variable
two variables variable swap Without
2023-09-11 14:16:15 时间
# swap two variables without intermediate variable, but intermediate variable is applicable to various situations(even with complex objects)
# the two methods below only applicable to numerics
# solution 1
a = 10
b = 8
a = a + b
b = a - b
a = a - b
print(f'{a=!r}\t{b=!r}')
# solution 2 exclusive OR
a = 10
b = 8
a = a ^ b # hold info about different bits of the two numbers
b = a ^ b
a = a ^ b
print(f'{a=!r}\t{b=!r}')
相关文章
- Leetcode: Sum of Two Integers && Summary: Bit Manipulation
- Leetcode: Divide Two Integers
- Leetcode: Merge Two Sorted Lists
- 21. Merge Two Sorted Lists
- leetcode 2 Add Two Numbers
- 【转】二维高斯分布(Two-dimensional Gaussian distribution)的参数分析
- Two Sum II - Input array is sorted
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
- [LeetCode] 371. Sum of Two Integers 两数之和
- [LintCode] Two Sum 两数之和
- Comparing Two High-Performance I/O Design Patterns--reference
- 350. Intersection of Two Arrays II