题目描述:

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. c# 折半查找法实现代码

    ] { , , , , , , , , , , , , , , , , , , , }; , i; string j, k; , ); ) { k = String.Format("未找到{ ...

  2. 51Nod 1087 1 10 100 1000 | 数学

    Input示例 3 1 2 3 Output示例 1 1 0 #include "bits/stdc++.h" using namespace std; #define LL lo ...

  3. Informatica _组件使用介绍及优化

    转载 http://blog.csdn.net/yongjian1092/article/details/52588434 有空自己会写一个关于这方面的文章.

  4. Spring Boot 启动报错:LoggingFailureAnalysisReporter

    17:57:19: Executing task 'bootRun'... Parallel execution with configuration on demand is an incubati ...

  5. 【BZOJ1093】【ZJOI2007】最大半联通子图 [DP][Tarjan]

    最大半连通子图 Time Limit: 30 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 一个有向图G=(V,E)称为 ...

  6. 【51NOD】数字1的数量

    [算法]数位DP [题解]数位dp总结 之 从入门到模板 #include<cstdio> #include<algorithm> #include<cstring> ...

  7. Spring总结以及在面试中的一些问题(山东数漫江湖)

    1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控制权,交由Spri ...

  8. word-wrap word-break 区别

    word-wrap word-break 区别 word-break * word-break:break-all;//直接把单词截断 * word-break:break-word;//虽然单词截断 ...

  9. 6.0docker Dockerfile文件

    指令格式 #注释 FROM :基础镜像 MAINTAINER:镜像的作者信息 RUN :指定(构建过程中)当前镜像中运行的命令 EXPOSE :指定运行镜像的容器应用程序所使用的端口 容器但不会打开, ...

  10. 海量数据排序——如果有1TB的数据需要排序,但只有32GB的内存如何排序处理?

    转载:https://blog.csdn.net/fx677588/article/details/72471357 1.外排序  传统的排序算法一般指内排序算法,针对的是数据可以一次全部载入内存中的 ...