死磕算法系列文章
“Leetcode : https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof
“GitHub : https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_50_lsumNums/Solution.java
“题目描述 :写一个函数,求两个整数之和,要求在函数体内不得使用 “+”、“-”、“*”、“/” 四则运算符号。 难度:简单
示例 1:
输入: a = 1, b = 1
输出: 2
// &
// ^ 相同为0,不同为1
// 0^0 = 1 1^1 = 0 0^1 = 1
package com.nateshao.sword_offer.topic_51_add;
/**
* @date Created by 邵桐杰 on 2022/2/24 22:34
* @微信公众号 千羽的编程时光
* @个人网站 www.nateshao.cn
* @博客 https://nateshao.gitee.io
* @GitHub https://github.com/nateshao
* @Gitee https://gitee.com/nateshao
* Description: https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/
* 剑指 Offer 65. 不用加减乘除做加法
* 写一个函数,求两个整数之和,要求在函数体内不得使用 “+”、“-”、“*”、“/” 四则运算符号。
* <p>
* 示例:
* <p>
* 输入: a = 1, b = 1
* 输出: 2
*/
public class Solution {
public static void main(String[] args) {
System.out.println("add(1, 2) = " + add(1, 2));
}
public static int add(int a, int b) {
while (b != 0) {
int sum = a ^ b;// 计算个位
b = (a & b) << 1; // 计算进位
a = sum;
}
return a;
}
}
复杂度分析:
参考链接:https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/solution/mian-shi-ti-65-bu-yong-jia-jian-cheng-chu-zuo-ji-7
END