LeetCode: 371 Sum of Two Integers(easy)
题目:
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.
代码:
使用移位来计算,用异或求不带进位的和,用与并左移1位来算进位,然后将两者相加。
class Solution {
public:
int getSum(int a, int b) {
return b == ? a : getSum(a^b, (a&b)<<);
}
};
什么样的数据可以直接移位? char、 short、 int、 long、 unsigned char、 unsigned short、 unsigned int、 unsigned long
什么样的数据不能直接移位? double、 float、 bool、 long double
LeetCode: 371 Sum of Two Integers(easy)的更多相关文章
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- 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 ...
- 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 ...
- 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 -. ...
- LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers
344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 【一天一道LeetCode】#371. Sum of Two Integers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Calcula ...
- 【LeetCode】371. Sum of Two Integers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位运算 日期 题目地址:https://leetco ...
- [LeetCode&Python] Problem 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 ...
随机推荐
- java 对象变量 c++对象指针 初始化对象变量的2中方法
java 对象变量 c++对象指针 java null引用 c++ null指针 Date deadline 是 对象变量,它可以引用Date类型的对象,但它不是一个对象,实际上它也没有引用对象. ...
- 【学员管理系统】0x01 班级信息管理功能
[学员管理系统]0x01 班级信息管理功能 写在前面 项目详细需求参见:Django项目之[学员管理系统] 视图函数: 我们把所有的处理请求相关的函数从 urls.py中拿出来,统一放在一个叫view ...
- linux c编程:网络编程
在网络上,通信服务都是采用C/S机制,也就是客户端/服务器机制.流程可以参考下图: 服务器端工作流程: 使用socket()函数创建服务器端通信套接口 使用bind()函数将创建的套接口与服务器地址绑 ...
- Hadoop基础学习(一)分析、编写并执行WordCount词频统计程序
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jiq408694711/article/details/34181439 前面已经在我的Ubuntu ...
- 用JAVA 的for循环输出 菱形
public class For{ public static void main(String[] args){ //首先.把菱形看成上下两部分,上五下四,所以,第一个for循环有5次,第二个for ...
- Java for LeetCode 120 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- SpringBoot学习笔记(11):使用WebSocket构建交互式Web应用程序
SpringBoot学习笔记(11):使用WebSocket构建交互式Web应用程序 快速开始 本指南将引导您完成创建“hello world”应用程序的过程,该应用程序在浏览器和服务器之间来回发送消 ...
- vs2012环境配置
快捷键 css格式设置 字体设置 新建项目 项目创建失败? 更改默认开发语言环境 1.快捷键 代码格式化:Ctrl+K+D 2.css格式设置: 工具→选项→文本编辑器→CSS→格式设置→选择“紧凑模 ...
- P4298 [CTSC2008]祭祀
P4298 [CTSC2008]祭祀 传递闭包跑一遍按联通建图 $(1)$最长反链长度=最小链覆盖=n-最大匹配 $(2)$定义作为最大匹配出现在左端点的集合为$S$,作为最大匹配出现在右端点的集合为 ...
- 使用aspnet_regiis.exe重新注册.NET Framework
cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319 aspnet_regiis.exe -i 重新安装IIS以后,需要用aspnet_re ...