File tree 6 files changed +86
-4
lines changed
6 files changed +86
-4
lines changed Original file line number Diff line number Diff line change 1
- - 程序员面试金典(第 6 版)题解
1
+ - LeetCode 《 程序员面试金典(第 6 版)》 题解
2
2
- [ 01.01. 判定字符是否唯一] ( /lcci/01.01.Is%20Unique/README.md )
3
3
- [ 01.02. 判定是否互为字符重排] ( /lcci/01.02.Check%20Permutation/README.md )
4
4
- [ 01.03. URL化] ( /lcci/01.03.String%20to%20URL/README.md )
Original file line number Diff line number Diff line change 1
- - 剑指 Offer(第 2 版)题解
1
+ - LeetCode 《 剑指 Offer(第 2 版)》 题解
2
2
- [ 03. 数组中重复的数字] ( /lcof/%E9%9D%A2%E8%AF%95%E9%A2%9803.%20%E6%95%B0%E7%BB%84%E4%B8%AD%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0%E5%AD%97/README.md )
3
3
- [ 04. 二维数组中的查找] ( /lcof/%E9%9D%A2%E8%AF%95%E9%A2%9804.%20%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9F%A5%E6%89%BE/README.md )
4
4
- [ 05. 替换空格] ( /lcof/%E9%9D%A2%E8%AF%95%E9%A2%9805.%20%E6%9B%BF%E6%8D%A2%E7%A9%BA%E6%A0%BC/README.md )
Original file line number Diff line number Diff line change
1
+ # [ 面试题65. 不用加减乘除做加法] ( https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/ )
2
+
3
+ ## 题目描述
4
+ <!-- 这里写题目描述 -->
5
+ 写一个函数,求两个整数之和,要求在函数体内不得使用 “+”、“-”、“* ”、“/” 四则运算符号。
6
+
7
+ ** 示例:**
8
+
9
+ ```
10
+ 输入: a = 1, b = 1
11
+ 输出: 2
12
+ ```
13
+
14
+ ** 提示:**
15
+
16
+ - ` a ` , ` b ` 均可能是负数或 0
17
+ - 结果不会溢出 32 位整数
18
+
19
+ ## 解法
20
+ <!-- 这里可写通用的实现逻辑 -->
21
+ 对两数进行 ` ^ ` 异或运算,得到不进位的和;对两数进行 ` & ` 与运算,得到进位。循环,直至进位为 0。
22
+
23
+ ### Python3
24
+ <!-- 这里可写当前语言的特殊实现逻辑 -->
25
+ 由于 python int 是无限长整型,左移不会自动溢出,因此需要特殊处理。
26
+
27
+ ``` python
28
+ class Solution :
29
+ def add (self , a : int , b : int ) -> int :
30
+ a, b = a & 0x ffffffff , b & 0x ffffffff
31
+ s = carry = 0
32
+ while b:
33
+ s = a ^ b
34
+ carry = ((a & b) << 1 ) & 0x ffffffff
35
+ a, b = s, carry
36
+ # 若a是正数,直接返回;若是负数,转为原码
37
+ return a if a < 0x 80000000 else ~ (a^ 0x ffffffff )
38
+ ```
39
+
40
+ ### Java
41
+ <!-- 这里可写当前语言的特殊实现逻辑 -->
42
+
43
+ ``` java
44
+ class Solution {
45
+ public int add (int a , int b ) {
46
+ int sum = 0 , carry = 0 ;
47
+ while (b != 0 ) {
48
+ sum = a ^ b;
49
+ carry = (a & b) << 1 ;
50
+ a = sum;
51
+ b = carry;
52
+ }
53
+ return a;
54
+ }
55
+ }
56
+ ```
57
+
58
+ ### ...
59
+ ```
60
+
61
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int add (int a , int b ) {
3
+ int sum = 0 , carry = 0 ;
4
+ while (b != 0 ) {
5
+ sum = a ^ b ;
6
+ carry = (a & b ) << 1 ;
7
+ a = sum ;
8
+ b = carry ;
9
+ }
10
+ return a ;
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def add (self , a : int , b : int ) -> int :
3
+ a , b = a & 0xffffffff , b & 0xffffffff
4
+ s = carry = 0
5
+ while b :
6
+ s = a ^ b
7
+ carry = ((a & b ) << 1 ) & 0xffffffff
8
+ a , b = s , carry
9
+ return a if a < 0x80000000 else ~ (a ^ 0xffffffff )
Original file line number Diff line number Diff line change 1
1
- 题解速览
2
2
- [ LeetCode(未完)] ( /solution/README.md )
3
- - [ 剑指 Offer(第 2 版)] ( /lcof/README.md )
4
- - [ 程序员面试金典(第 6 版)] ( /lcci/README.md )
3
+ - [ LeetCode 《 剑指 Offer(第 2 版)》 ] ( /lcof/README.md )
4
+ - [ LeetCode 《 程序员面试金典(第 6 版)》 ] ( /lcci/README.md )
You can’t perform that action at this time.
0 commit comments