博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
剑指 Offer 65. 不用加减乘除做加法
阅读量:4034 次
发布时间:2019-05-24

本文共 423 字,大约阅读时间需要 1 分钟。

题目描述

写一个函数,求两个整数之和,要求在函数体内不得使用 “+”、“-”、“*”、“/” 四则运算符号。

示例:

输入: a = 1, b = 1

输出: 2

提示:

a, b 均可能是负数或 0

结果不会溢出 32 位整数

Java

class Solution {
public int add(int a, int b) {
//用二进制位运算 int sum=0; int carry=0; do{
sum=a^b; //1.先二进制相加,不进位,用异或代替, carry=(a & b)<<1; //2.计算进位, a=sum; b=carry; }while(b!=0); return a; }}

转载地址:http://zafdi.baihongyu.com/

你可能感兴趣的文章
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
如何打开ipynb文件
查看>>