diff --git a/README.md b/README.md index c0e373d..38e3189 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ | 62 | [Last Number In Circle](/solution/剑指Offer/62_LastNumberInCircle) | | 63 | [Maximal Profit](/solution/剑指Offer/63_MaximalProfit) | | 64 | [Accumulate](/solution/剑指Offer/64_Accumulate) | +| 65 | [Add Two Numbers](/solution/剑指Offer/65_AddTwoNumbers) | ### 编程之美题集 diff --git "a/solution/\345\211\221\346\214\207Offer/65_AddTwoNumbers/README.md" "b/solution/\345\211\221\346\214\207Offer/65_AddTwoNumbers/README.md" new file mode 100644 index 0000000..ef5cae2 --- /dev/null +++ "b/solution/\345\211\221\346\214\207Offer/65_AddTwoNumbers/README.md" @@ -0,0 +1,49 @@ +## [不用加减乘除做加法](https://www.acwing.com/problem/content/81/) + +### 题目描述 +写一个函数,求两个整数之和,要求在函数体内不得使用+、-、×、÷ 四则运算符号。 + +**样例** +``` +输入:num1 = 1 , num2 = 2 + +输出:3 +``` + +### 解法 +先对两数进行异或,求得相加不仅位的结果。再循环对两数进行按位与运算,并左移一位,直至进位为 0。 + +```java +/** + * @author bingo + * @since 2018/12/13 + */ + +class Solution { + + /** + * 不用加减乘除做加法 + * + * @param num1 数1 + * @param num2 数2 + * @return 两数之和 + */ + public int add(int num1, int num2) { + int sum, carry; + while (true) { + sum = num1 ^ num2; + carry = (num1 & num2) << 1; + num1 = sum; + num2 = carry; + if (num2 == 0) { + break; + } + } + return num1; + } +} +``` + + +### 测试用例 +1. 输入正数、负数、0。 \ No newline at end of file diff --git "a/solution/\345\211\221\346\214\207Offer/65_AddTwoNumbers/Solution.java" "b/solution/\345\211\221\346\214\207Offer/65_AddTwoNumbers/Solution.java" new file mode 100644 index 0000000..ce34063 --- /dev/null +++ "b/solution/\345\211\221\346\214\207Offer/65_AddTwoNumbers/Solution.java" @@ -0,0 +1,28 @@ +/** + * @author bingo + * @since 2018/12/13 + */ + +class Solution { + + /** + * 不用加减乘除做加法 + * + * @param num1 数1 + * @param num2 数2 + * @return 两数之和 + */ + public int add(int num1, int num2) { + int sum, carry; + while (true) { + sum = num1 ^ num2; + carry = (num1 & num2) << 1; + num1 = sum; + num2 = carry; + if (num2 == 0) { + break; + } + } + return num1; + } +} \ No newline at end of file