【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 -.
解题分析:
这种类型的题必然要用位运算,虽然自己写了关于位运算的代码,但是不够简洁。
后来参考了这篇博文:
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的更多相关文章
- 【LeetCode】371. Sum of Two Integers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- 【一天一道LeetCode】#371. Sum of Two Integers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【Leetcode】404. Sum of Left Leaves
404. Sum of Left Leaves [题目]中文版 英文版 /** * Definition for a binary tree node. * struct TreeNode { * ...
- 【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 ...
随机推荐
- CMDB资产管理系统开发【day26】:批准资产入库
刚才都是一条像内存,硬盘,网卡.多条的话如何操作 只有一条数据 下面的是有多条数据的 硬盘必须字段的验证 def __create_disk_component(self): disk_info = ...
- 51Nod 1010 只包含因子2 3 5的数 | 预处理+二分
Input示例 5 1 8 13 35 77 Output示例 2 8 15 36 80 分析:将所有的只含有2 3 5因子的数打一个表保存在一个数组里,然后二分查找第一个>=数组里的数,输出 ...
- 图论:最短路-Bellman-Ford
我们之前介绍了一种,(最常用的)SPFA算法,SPFA算法是对Bellman-Ford算法的队列优化,用队列替代了Bellman-Ford中的循环检查部分 然后这里我们介绍Bellman-Ford算法 ...
- Python遍历文件夹和读写文件的方法
需 求 分 析 1.读取指定目录下的所有文件2.读取指定文件,输出文件内容3.创建一个文件并保存到指定目录 实 现 过 程 Python写代码简洁高效,实现以上功能仅用了40行左右的代码~ 昨天用Ja ...
- Zyan 一个通信框架
原文地址 本文示例 Zyan是一个简单直观的分布式应用程序开发框架. 以下是Zyan项目的简要概述. 架构图: Zyan一般由客户端和服务端组成.服务端(以组件的形式)提供服务,客户端远程调用服务端的 ...
- 【hdu1255】线段树求矩形面积交
题意大概就是上图这个样子.<=100组测试数据,每组<=1000个矩形. 题解: 这个问题怎么解决..做了上一题矩形面积并应该就会了.. 对于每个节点维护3个值: cnt:该节点所代表的这 ...
- 【转】IOS版本自定义字体步骤
本文转载自:http://quick.cocoachina.com/wiki/doku.php?id=ios%E7%89%88%E6%9C%AC%E4%BD%BF%E7%94%A8%E8%87%AA% ...
- 【洛谷 P4180】【模板】严格次小生成树[BJWC2010](倍增)
题目链接 题意如题. 这题作为我们KS图论的T4,我直接打了个很暴力的暴力,骗了20分.. 当然,我们KS里的数据范围远不及这题. 这题我debug了整整一个晚上还没debug出来,第二天早上眼前一亮 ...
- HDU 1284 钱币兑换问题 (dp)
题目链接 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于327 ...
- Mojo_1_第一个简单例子
use Mojolicious::Lite; #根目录,Get方法打开 #正接显示文本text get '/' => sub{ my $service = shift; $service-> ...