Sum of Two Integers问题及解法
来源:互联网 发布:编程 数学 编辑:程序博客网 时间:2024/05/21 12:08
问题描述:
Calculate the sum of two integers a and b, but you are not allowed to use the operator +
and -
.
示例:
Given a = 1 and b = 2, return 3.
问题分析:
不用+和-,我们可以考虑从位运算的角度出发。主要用到了进位操作和异或运算.
过程详见代码:
class Solution {public: int getSum(int a, int b) { int res = a ^ b; int flag = 0; int jin = 0; int i = 1,j = 1; while(i++ <= 32) { jin = jin ^ flag; flag = ((a & b & j) | (a & flag) | (b & flag)); flag <<= 1; j <<= 1; } return (res ^ jin); }};
0 0
- Sum of Two Integers问题及解法
- Two Sum问题及解法
- Minimum Index Sum of Two Lists问题及解法
- Divide Two Integers问题及解法
- leetcode之371. Sum of Two Integers(C++解法)
- 371. Sum of Two Integers的C++解法
- Sum of Two Integers
- Sum of Two Integers
- Sum of Two Integers
- Sum of Two Integers
- Sum of Two Integers
- Sum of Two Integers
- sum of two integers
- Sum of Two Integers
- Sum of Two Integers
- Sum of Two Integers
- Sum of Two Integers
- Sum of Two Integers
- PAT1011. World Cup Betting (20)
- 现操-week7
- 互联网大厂面试题回忆录
- C计算调和级数
- Eclipse 中XXX is missing required source folder 问题的解决
- Sum of Two Integers问题及解法
- SQL注入相关
- 关于内核缓冲区,和应用程序缓冲区
- 杭电5983之Pocket Cube
- Linker Tools Error LNK2019官方错误原因和个人碰到的一些问题
- C++作业3
- mmap映射问题
- JS範囲チェック追加
- 波动数列 蓝桥