题目描述:

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

解题分析:

这种类型的题必然要用位运算,虽然自己写了关于位运算的代码,但是不够简洁。

后来参考了这篇博文:

http://blog.csdn.net/zhongjiekangping/article/details/6855864

这篇博文对位运算加法的实现讲的得十分清楚,我这里就只放按此思路写的实现代码了。

具体代码:

 public class Solution {
public int getSum(int num1, int num2) {
//执行加法
int n=num1^num2;
//执行进位操作
int m=(num1&num2)<<1;
//必须保证没有进位了,才能结束操作,否则重复上面的操作
if(m!=0)
return getSum(n,m); return n;
}
}

【leetcode】371. Sum of Two Integers的更多相关文章

  1. 【LeetCode】371. Sum of Two Integers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...

  2. 【一天一道LeetCode】#371. Sum of Two Integers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...

  3. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  4. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  5. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  6. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

  7. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  8. 【Leetcode】404. Sum of Left Leaves

    404. Sum of Left Leaves [题目]中文版  英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...

  9. 【leetcode】Path Sum IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

随机推荐

  1. css渐变知识知多少

    <!DOCTYPE html><html><head><meta charset="utf-8"> <title>教程( ...

  2. nodejs与mongo

    1.连接URL (使用数据用户名与密码连接或不使用连接数据库) npm install mongodb --save var mon = require('mongodb').MongoClient; ...

  3. session验证-使用filter过滤器

    public override void OnActionExecuting(ActionExecutingContext context) { string test = context.Contr ...

  4. [POI2008]BLO-Blockade

    https://www.luogu.org/problem/show?pid=3469 题目描述 There are exactly  towns in Byteotia. Some towns ar ...

  5. Maven:Non-resolvable parent POM: Failure to find错误

    使用Maven编译项目时遇到如下错误: [ERROR] The project dfjr.generic:dfjr-generic:1.0-SNAPSHOT (F:\workspace\DFJR-PE ...

  6. 快速搭建rabbitmq单节点并配置使用

    安装erlang环境 wget http://erlang.org/download/otp_src_20.3.tar.gz tar xf otp_src_20.3.tar.gz && ...

  7. Send Email in Robot Framework Python Using Gmail

    转载自:http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-fra ...

  8. 回顾一下MyBatis逆向工程——自动生成代码

    前言 最近做的项目(SSM+Shiro)的数据库表已经创建完成,一共有15张表,如果我们一个个去写pojo/bean的代码以及各种sql语句的话未免太过麻烦而且很容易出错,这个时候我们就需要MyBat ...

  9. Spring注解@Resource和@Autowired区别对比、spring扫描的默认bean的Id、程序获取spring容器对象

    -------------------------注解扫面的bean的ID问题-------------------------- 0.前提需要明白注解扫描出来的bean的id默认是类名首字母小写,当 ...

  10. Python自动化运维 - Django(三)CSRF - Cookie&Session

    CSRF跨站请求伪造 CSRF跨站点请求伪造(Cross—Site Request Forgery),跟XSS攻击一样,存在巨大的危害性,你可以这样来理解:攻击者盗用了你的身份,以你的名义发送恶意请求 ...