371. Sum of Two Integers
来源:互联网 发布:滑板淘宝 编辑:程序博客网 时间:2024/05/19 03:46
Problem description
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example:
Given a = 1 and b = 2, return 3.题目描述
计算两个整数a与b的和,但是你不能使用+、-运算
例子
输入a = 1,b = 2,返回3
链接:https://leetcode.com/problems/sum-of-two-integers/
解题思路
模拟计算机二进制形式的计算加法,采用异或、与、移位运算
public class Solution { public int getSum(int a, int b) { int c = a ^ b;int d = (a & b) << 1;while(d != 0){a = d;b = c;c = a ^ b;d = (a & b) << 1; }return c; }}此外java还自带的Math.addExact(a, b)函数,python的sum()函数都可以用来ac这道题,不过不是正途。
0 0
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- 371. Sum of Two Integers
- hdu4746 Mophues(莫比乌斯反演)-好题
- 各大平台免费接口
- linux下查看和添加PATH环境变量
- 总结【待续】
- Socket的accept函数解析
- 371. Sum of Two Integers
- MyBatis批量插入和性能问题
- 匈牙利算法 hdu 2063(过山车)
- 浏览器加载和渲染html的顺序
- android之Handler的使用
- centos下的mysql安装
- 平滑过渡的战争迷雾(一) 原理:Warcraft3地形拼接算法
- 2016.8.17 C组总结
- 项目上用到压力测试,记录loadrunner