【leetcode74】Sum of Two Integers(不用+,-求两数之和)
题目描述:
不用+,-求两个数的和
原文描述:
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:
- 异或又被称其为“模2加法“
- 设置变量recipe模拟进位数字,模拟加法的实现过程
代码:
public class Solution {
public int getSum(int a, int b) {
int r = 0, c = 0, recipe = 1;
while ((a | b | c) != 0) {
if (((a ^ b ^ c) & 1) != 0)
r |= recipe;
recipe <<= 1;
c = (a & b | b & c | a & c) & 1;
a >>>= 1;
b >>>= 1;
}
return r;
}
}
方法二:异或求值
思路二:
- a^b,求得结果
- a&b,求得进位
- 相加
代码:
public class Solution {
public int getSum(int a, int b) {
while (b != 0) {
int c = a & b; //carry
a ^= b; //add
b = c << 1;
}
return a;
}
}
更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode74】Sum of Two Integers(不用+,-求两数之和)的更多相关文章
- c++作业:输入两个整数,用函数求两数之和。函数外部声明有什么作用?
#include <iostream> using namespace std; int main(){ //求两数的和? int a,b,s; cout<<"请你输 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- 167. Two Sum II - Input array is sorted两数之和
1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...
- 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538
第一章 #include <iostream>using namespace std; int main(){ int a,b,sum; sum=a+b; cin>>a> ...
- [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...
随机推荐
- Java数据库开发(一)之——JDBC连接数据库
一.MySQL数据库 1.创建数据库 CREATE DATABASE jdbc CHARACTER SET 'utf8'; 2.建表 CREATE TABLE user ( id int(10) NO ...
- Java Socket通信代码片
package zhang; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExcept ...
- redis在java客户端的操作
redis高性能,速度快,效率高的特点,用来做缓存服务器是很不错的选择.(和memcache相似)redis在客户端的操作步骤: 1.redis单机版操作 1.1通过Jedis对象操作 (1)将安装r ...
- windows系统和centos双系统安装引导项修改
在CentOS下修改Linux引导文件: (1)找到win10的引导 1.首先我们点击第一个系统进入centos 2.运行终端,敲入命令su,为了获取管理员权限,然后终端提 ...
- Python-Jupyter Notebook使用技巧
0. 体验与安装 首先可以通过Jupyter Notebook体验这个链接体验一下Jupyter Notebook. 首先安装ipython:pip3 install ipython 然后安装Jupy ...
- 解决HTML外部引用CSS文件不生效问题
作为一个前端小白,鼓捣了几天前端..今天突然发现我深信不疑的东西,竟然出现了问题..就比如我在css目录下面写了一个css样式文档:style.css.这时里面只有一句话: body { backgr ...
- Ubuntu环境下Anaconda安装TensorFlow并配置Jupyter远程访问
本文主要讲解在Ubuntu系统中,如何在Anaconda下安装TensorFlow以及配置Jupyter Notebook远程访问的过程. 在官方文档中提到,TensorFlow的安装主要有以下五种形 ...
- zookeeper分布式锁
摘要:分享牛原创,zookeeper使用,zookeeper锁在实际项目开发中还是很常用的,在这里我们介绍一下zookeeper分布式锁的使用,以及我们如何zookeeper分布式锁的原理.zooke ...
- Android Multimedia框架总结(十六)Camera2框架之openCamera及session过程
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52942533 前言:前一篇介绍了 ...
- springMVC源码分析--ControllerBeanNameHandlerMapping(八)
在上一篇博客springMVC源码分析--AbstractControllerUrlHandlerMapping(六)中我们介绍到AbstractControllerUrlHandlerMapping ...