题目描述:

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. html实现圆角矩形

    问题:如何通过div+css以及定位来实现圆角矩形? 解决方法概述: 内容:首先在<body>标签内部里添加一个大层(大层用来固定整体大框架),然后大层内包含四个小层(四个小层里分别放四个 ...

  2. python socket和简单tcp通信实现

    python 服务端和客户端的简单交互 TCP服务端: 1 创建套接字,绑定套接字到本地IP与端口 s = socket.socket(socket.AF_INET,socket.SOCK_STREA ...

  3. vector基础

    //STL基础 //容器 //vector #include "iostream" #include "cstdio" #include "vecto ...

  4. js 拖动滑块验证

    备注:拖动滑块时尽量平移,chrome浏览器上没有卡顿情况,但是搜狗极速模式和360极速模式都遇到了卡顿,拖不动情况,应是浏览器内部对事件响应速度导致吧. JS代码: ;(function ($,wi ...

  5. 【poj2182】【poj2828】树状数组/线段树经典模型:逆序查找-空位插入法

    poj2182题意:有一个1~n的排列,现在给定每个人前面有多少个人的编号比他大,求这个排列是什么.n<=8000 poj2182题解: 逆序做,可以确定二分最后一个是什么,然后删除这个数.树状 ...

  6. 【NOIP】普及组2011 表达式的值

    [算法]动态规划+后缀表达式 [题解] 先把算式转为后缀表达式后进行DP 令f[s][0]表示使表达式答案为0的方案数 f[s][1]表示使表达式答案为1的方案数 (加法) f[a+b][1]=f[a ...

  7. springboot:Spring boot中mongodb的使用(山东数漫江湖)

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  8. bzoj 1483 链表启发式合并

    首先我们可以比较容易的在n的时间内算出来开始的答案,我们维护一些链表,分别表示不同的颜色,那么我们在计算答案的时候,只需要扫一遍所有的链表,判断链表相邻两项是否在序列中相邻,不相邻的话肯定在这其中的一 ...

  9. (Git 钩子)自定义你的工作流 和引用日志

    Git 钩子是在 Git 仓库中特定事件发生时自动运行的脚本.它可以让你自定义 Git 内部的行为,在开发周期中的关键点触发自定义的行为. Git 钩子最常见的使用场景包括推行提交规范,根据仓库状态改 ...

  10. jq 浏览器窗口大小发生变化时

    当调整浏览器窗口的大小时,发生 resize 事件: $(selector).resize(); 实例 对浏览器窗口调整大小进行计数: $(window).resize(function() { $( ...