Skip to content

Commit 25d8ef3

Browse files
authored
feat: add solutions to lc problem: No.2235 (#1467)
No.2235.Add Two Integers
1 parent 158fc4c commit 25d8ef3

File tree

2 files changed

+172
-10
lines changed

2 files changed

+172
-10
lines changed

solution/2200-2299/2235.Add Two Integers/README.md

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,36 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41+
**方法一:使用加法运算符**
42+
43+
我们可以直接使用加法运算符 `+` 来计算两个整数的和。
44+
45+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
46+
47+
**方法二:位运算(不使用加法运算符)**
48+
49+
我们也可以在不使用加法运算符的前提下,使用位运算来计算两个整数的和。
50+
51+
假设 $num1_i$ 和 $num2_i$ 分别表示 $num1$ 和 $num2$ 的第 $i$ 个二进制位。一共有 $4$ 种情况:
52+
53+
| $num1_i$ | $num2_i$ | 不进位的和 | 进位 |
54+
| -------- | -------- | ---------- | ---- |
55+
| 0 | 0 | 0 | 0 |
56+
| 0 | 1 | 1 | 0 |
57+
| 1 | 0 | 1 | 0 |
58+
| 1 | 1 | 0 | 1 |
59+
60+
观察可以发现,“不进位的和”与“异或运算”有相同规律,而进位则与“与”运算规律相同,并且需要左移一位。
61+
62+
因此:
63+
64+
- 对两数进行按位 `&` 与运算,然后左移一位,得到进位,记为 $carry$;
65+
- 对两数进行按位 `^` 异或运算,得到不进位的和;
66+
- 问题转换为求:“不进位的数 + 进位” 之和;
67+
- 循环,直至第二个数为 $0$,返回第一个数即可(也可以用递归实现)。
68+
69+
时间复杂度 $O(\log M)$,其中 $M$ 为题目中数字的最大值。空间复杂度 $O(1)$。
70+
4171
<!-- tabs:start -->
4272

4373
### **Python3**
@@ -50,6 +80,16 @@ class Solution:
5080
return num1 + num2
5181
```
5282

83+
```python
84+
class Solution:
85+
def sum(self, num1: int, num2: int) -> int:
86+
num1, num2 = num1 & 0xFFFFFFFF, num2 & 0xFFFFFFFF
87+
while num2:
88+
carry = ((num1 & num2) << 1) & 0xFFFFFFFF
89+
num1, num2 = num1 ^ num2, carry
90+
return num1 if num1 < 0x80000000 else ~(num1 ^ 0xFFFFFFFF)
91+
```
92+
5393
### **Java**
5494

5595
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -62,11 +102,16 @@ class Solution {
62102
}
63103
```
64104

65-
### **TypeScript**
66-
67-
```ts
68-
function sum(num1: number, num2: number): number {
69-
return num1 + num2;
105+
```java
106+
class Solution {
107+
public int sum(int num1, int num2) {
108+
while (num2 != 0) {
109+
int carry = (num1 & num2) << 1;
110+
num1 ^= num2;
111+
num2 = carry;
112+
}
113+
return num1;
114+
}
70115
}
71116
```
72117

@@ -81,6 +126,20 @@ public:
81126
};
82127
```
83128
129+
```cpp
130+
class Solution {
131+
public:
132+
int sum(int num1, int num2) {
133+
while (num2) {
134+
unsigned int carry = (unsigned int) (num1 & num2) << 1;
135+
num1 ^= num2;
136+
num2 = carry;
137+
}
138+
return num1;
139+
}
140+
};
141+
```
142+
84143
### **Go**
85144

86145
```go
@@ -89,6 +148,17 @@ func sum(num1 int, num2 int) int {
89148
}
90149
```
91150

151+
```go
152+
func sum(num1 int, num2 int) int {
153+
for num2 != 0 {
154+
carry := (num1 & num2) << 1
155+
num1 ^= num2
156+
num2 = carry
157+
}
158+
return num1
159+
}
160+
```
161+
92162
### **TypeScript**
93163

94164
```ts
@@ -97,6 +167,17 @@ function sum(num1: number, num2: number): number {
97167
}
98168
```
99169

170+
```ts
171+
function sum(num1: number, num2: number): number {
172+
while (num2) {
173+
const carry = (num1 & num2) << 1;
174+
num1 ^= num2;
175+
num2 = carry;
176+
}
177+
return num1;
178+
}
179+
```
180+
100181
### **Rust**
101182

102183
```rust
@@ -107,6 +188,21 @@ impl Solution {
107188
}
108189
```
109190

191+
```rust
192+
impl Solution {
193+
pub fn sum(num1: i32, num2: i32) -> i32 {
194+
let mut num1 = num1;
195+
let mut num2 = num2;
196+
while num2 != 0 {
197+
let carry = (num1 & num2) << 1;
198+
num1 ^= num2;
199+
num2 = carry;
200+
}
201+
num1
202+
}
203+
}
204+
```
205+
110206
### **C**
111207

112208
```c

solution/2200-2299/2235.Add Two Integers/README_EN.md

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ class Solution:
4242
return num1 + num2
4343
```
4444

45+
```python
46+
class Solution:
47+
def sum(self, num1: int, num2: int) -> int:
48+
num1, num2 = num1 & 0xFFFFFFFF, num2 & 0xFFFFFFFF
49+
while num2:
50+
carry = ((num1 & num2) << 1) & 0xFFFFFFFF
51+
num1, num2 = num1 ^ num2, carry
52+
return num1 if num1 < 0x80000000 else ~(num1 ^ 0xFFFFFFFF)
53+
```
54+
4555
### **Java**
4656

4757
```java
@@ -52,11 +62,16 @@ class Solution {
5262
}
5363
```
5464

55-
### **TypeScript**
56-
57-
```ts
58-
function sum(num1: number, num2: number): number {
59-
return num1 + num2;
65+
```java
66+
class Solution {
67+
public int sum(int num1, int num2) {
68+
while (num2 != 0) {
69+
int carry = (num1 & num2) << 1;
70+
num1 ^= num2;
71+
num2 = carry;
72+
}
73+
return num1;
74+
}
6075
}
6176
```
6277

@@ -71,6 +86,20 @@ public:
7186
};
7287
```
7388
89+
```cpp
90+
class Solution {
91+
public:
92+
int sum(int num1, int num2) {
93+
while (num2) {
94+
unsigned int carry = (unsigned int) (num1 & num2) << 1;
95+
num1 ^= num2;
96+
num2 = carry;
97+
}
98+
return num1;
99+
}
100+
};
101+
```
102+
74103
### **Go**
75104

76105
```go
@@ -79,6 +108,17 @@ func sum(num1 int, num2 int) int {
79108
}
80109
```
81110

111+
```go
112+
func sum(num1 int, num2 int) int {
113+
for num2 != 0 {
114+
carry := (num1 & num2) << 1
115+
num1 ^= num2
116+
num2 = carry
117+
}
118+
return num1
119+
}
120+
```
121+
82122
### **TypeScript**
83123

84124
```ts
@@ -87,6 +127,17 @@ function sum(num1: number, num2: number): number {
87127
}
88128
```
89129

130+
```ts
131+
function sum(num1: number, num2: number): number {
132+
while (num2) {
133+
const carry = (num1 & num2) << 1;
134+
num1 ^= num2;
135+
num2 = carry;
136+
}
137+
return num1;
138+
}
139+
```
140+
90141
### **Rust**
91142

92143
```rust
@@ -97,6 +148,21 @@ impl Solution {
97148
}
98149
```
99150

151+
```rust
152+
impl Solution {
153+
pub fn sum(num1: i32, num2: i32) -> i32 {
154+
let mut num1 = num1;
155+
let mut num2 = num2;
156+
while num2 != 0 {
157+
let carry = (num1 & num2) << 1;
158+
num1 ^= num2;
159+
num2 = carry;
160+
}
161+
num1
162+
}
163+
}
164+
```
165+
100166
### **C**
101167

102168
```c

0 commit comments

Comments
 (0)