371. Sum of Two Integers
来源:互联网 发布:淘宝图片保护怎么破解 编辑:程序博客网 时间:2024/06/08 02:53
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.
因为计算两个数的和又不允许使用+或-操作,可以在二进制下实现这一操作:先用异或计算两个数的二进制相加结果sum,但是不考虑进位情况,然后用与计算得到产生进位的地方,再向左移一位模拟进位carry,(因为两个二进制位包括四种情况,1 1,1 0, 0 0, 0 1,第一种情况相与为1,且正好是产生进位的地方)。然后递归调用函数,直到carry为0,返回sum即可。
if(b==0){ return a; } int sum = a^b; int carry = (a&b)<<1; return getSum(sum, carry);
阅读全文
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
- caffe源码阅读遇到的问题
- 一个页面上有大量的图片(大型电商网站),加载很慢,你有哪些方法优化这些图片的加载,给用户更好的体验。
- 可以使用USB2.0接口进行全双工通讯么,双方都可以主动发送么?
- JQuery_基础
- JAVA知识点概述
- 371. Sum of Two Integers
- 矩阵乘以它的转置
- 5、ModelAndView——处理模型数据
- OC学习篇之---类的初始化方法和点语法的使用
- 从零开始前端学习[52]:js中的数据类型以及没有数据类型的数据类型typeof
- 利用Openssl制作自签名证书
- python学习笔记--json&&pickle
- 「mysql优化专题」高可用性、负载均衡的mysql集群解决方案(12)
- poj 3080 Blue Jeans