题目:        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和b进行循环,直到进位为0为止。计算过程与二进制加法器一致。 

 1 public class Solution {
 2     public int getSum(int a, int b) {
 3         while(b!=0){
 4             int carry = a & b; //计算进位
 5             a = a ^ b;         //计算和数
 6             b = carry<<1;
 7         }
 8         return a;
 9     }
10 }

转载于:https://www.cnblogs.com/tmx22484/p/6349327.html

Leetcode:371.Sum Of Two Integer相关推荐

  1. leetcode 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  2. leetcode 371. Sum of Two Integers | 371. 两整数之和(补码运算)

    题目 https://leetcode.com/problems/sum-of-two-integers/ 题解 根据 related topics 可知,本题考察二进制运算. 第一次提交的时候,没想 ...

  3. Leetcode PHP题解--D84 371. Sum of Two Integers

    D84 371. Sum of Two Integers 题目链接 371. Sum of Two Integers 题目分析 相加给定的两个数,但不能使用+或-运算符. 思路 可以用二进制的与运算完 ...

  4. LeetCode算法入门- Roman to Integer Integer to Roman -day8

    LeetCode算法入门- Roman to Integer -day8 Roman to Integer: 题目描述: Roman numerals are represented by seven ...

  5. LeetCode算法入门- String to Integer (atoi)-day7

    LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts ...

  6. 【同113】LeetCode 129. Sum Root to Leaf Numbers

    LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...

  7. 乘风破浪:LeetCode真题_008_String to Integer (atoi)

    乘风破浪:LeetCode真题_008_String to Integer (atoi) 一.前言 将整型转换成字符串,或者将字符串转换成整型,是经常出现的,也是必要的,因此我们需要熟练的掌握,当然也 ...

  8. leetcode 371. 两整数之和

    leetcode 371. 两整数之和 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a .b ​​​​​​​之和. 示例 1: 输入: a = 1, b = 2 输出: 3 示 ...

  9. LeetCode 371. 两整数之和(异或操作) / 639. 解码方法 II(动态规划)/ 437. 路径总和 III

    371. 两整数之和 2021.9.26 每日一题 题目描述 给你两个整数 a 和 b ,不使用 运算符 + 和 - ​​​​​​​,计算并返回两整数之和. 示例 1: 输入:a = 1, b = 2 ...

最新文章

  1. java带参数的方法笔记_具有Java参数的方法的类声明
  2. SAP创建webservice
  3. 办公室网络打印机的连接
  4. python的时间差计算
  5. json_encode 处理中文乱码
  6. VueX的store的简单使用心结
  7. matlab中scrsz的意思,matlab图片c存储方法
  8. sklearn决策树概述
  9. 信息学奥赛C++语言: 验证子串
  10. Bailian4019 黑色星期五【模拟】
  11. 一文掌握 Docker 技术体系
  12. 海思Hi3519A开发(5.梳理海思文档与运行sample代码)
  13. 【iphone】开发者传授APP开发,审核,发布流程!
  14. 【预测模型】基于最小二乘法算法实现股票预测matlab代码
  15. 投稿经验分享之三:干货| 如何找寻合适的投稿期刊?
  16. 计算机之大尾数、小尾数
  17. Python中返回数字绝对值的方法abs()函数
  18. 距离2021年还剩75天,我在想什么?
  19. 推荐一些Fortran参考书
  20. Swi-Prolog 数值约束

热门文章

  1. 数据结构与算法笔记(十六)—— 二叉搜索树
  2. docker安装redmine步骤
  3. 从浏览器输入URL到最终看到页面, 这其中经历了哪些过程 ?
  4. python调整数组顺序使奇数位于偶数前面
  5. python读取文件报错必须有一个正确的读写方式_python文件操作
  6. linux grep 正则搜索某段时间内的日志
  7. android中的标题栏是什么意思,Android通用标题栏组合控件
  8. 网站打不开 换服务器,【图】网站更换服务器打不开—深圳天下信息网
  9. 递归删除单链表中所有值为x的元素_如何纯递归反转链表的一部分
  10. linux开发板蓝牙连接,开发板蓝牙通信问题,有这方面经验的请进