Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f9321e

Browse files
committed
feat: add solutions to lc problem: No.0415
No.0415.Add Strings
1 parent c2fbd37 commit 8f9321e

File tree

7 files changed

+398
-0
lines changed

7 files changed

+398
-0
lines changed

‎solution/0400-0499/0415.Add Strings/README.md‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
时间复杂度 $O(max(m, n)),ドル其中 $m$ 和 $n$ 分别是两个字符串的长度。忽略答案字符串的空间消耗,空间复杂度 $O(1)$。
5959

60+
以下代码还实现了字符串相减,参考 `subStrings(num1, num2)` 函数。
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**
@@ -76,6 +78,25 @@ class Solution:
7678
ans.append(str(v))
7779
i, j = i - 1, j - 1
7880
return "".join(ans[::-1])
81+
82+
def subStrings(self, num1: str, num2: str) -> str:
83+
m, n = len(num1), len(num2)
84+
neg = m < n or (m == n and num1 < num2)
85+
if neg:
86+
num1, num2 = num2, num1
87+
i, j = len(num1) - 1, len(num2) - 1
88+
ans = []
89+
c = 0
90+
while i >= 0:
91+
c = int(num1[i]) - c - (0 if j < 0 else int(num2[j]))
92+
ans.append(str((c + 10) % 10))
93+
c = 1 if c < 0 else 0
94+
i, j = i - 1, j - 1
95+
while len(ans) > 1 and ans[-1] == '0':
96+
ans.pop()
97+
if neg:
98+
ans.append('-')
99+
return ''.join(ans[::-1])
79100
```
80101

81102
### **Java**
@@ -96,6 +117,30 @@ class Solution {
96117
}
97118
return ans.reverse().toString();
98119
}
120+
121+
public String subStrings(String num1, String num2) {
122+
int m = num1.length(), n = num2.length();
123+
boolean neg = m < n || (m == n && num1.compareTo(num2) < 0);
124+
if (neg) {
125+
String t = num1;
126+
num1 = num2;
127+
num2 = t;
128+
}
129+
int i = num1.length() - 1, j = num2.length() - 1;
130+
StringBuilder ans = new StringBuilder();
131+
for (int c = 0; i >= 0; --i, --j) {
132+
c = (num1.charAt(i) - '0') - c - (j < 0 ? 0 : num2.charAt(j) - '0');
133+
ans.append((c + 10) % 10);
134+
c = c < 0 ? 1 : 0;
135+
}
136+
while (ans.length() > 1 && ans.charAt(ans.length() - 1) == '0') {
137+
ans.deleteCharAt(ans.length() - 1);
138+
}
139+
if (neg) {
140+
ans.append('-');
141+
}
142+
return ans.reverse().toString();
143+
}
99144
}
100145
```
101146

@@ -117,6 +162,29 @@ public:
117162
reverse(ans.begin(), ans.end());
118163
return ans;
119164
}
165+
166+
string subStrings(string num1, string num2) {
167+
int m = num1.size(), n = num2.size();
168+
bool neg = m < n || (m == n && num1 < num2);
169+
if (neg) {
170+
swap(num1, num2);
171+
}
172+
int i = num1.size() - 1, j = num2.size() - 1;
173+
string ans;
174+
for (int c = 0; i >= 0; --i, --j) {
175+
c = (num1[i] - '0') - c - (j < 0 ? 0 : num2[j] - '0');
176+
ans += to_string((c + 10) % 10);
177+
c = c < 0 ? 1 : 0;
178+
}
179+
while (ans.size() > 1 && ans.back() == '0') {
180+
ans.pop_back();
181+
}
182+
if (neg) {
183+
ans.push_back('-');
184+
}
185+
reverse(ans.begin(), ans.end());
186+
return ans;
187+
}
120188
};
121189
```
122190

@@ -141,6 +209,38 @@ func addStrings(num1 string, num2 string) string {
141209
}
142210
return string(ans)
143211
}
212+
213+
func subStrings(num1 string, num2 string) string {
214+
m, n := len(num1), len(num2)
215+
neg := m < n || (m == n && num1 < num2)
216+
if neg {
217+
num1, num2 = num2, num1
218+
}
219+
i, j := len(num1)-1, len(num2)-1
220+
ans := []byte{}
221+
for c := 0; i >= 0; i, j = i-1, j-1 {
222+
c = int(num1[i]-'0') - c
223+
if j >= 0 {
224+
c -= int(num2[j] - '0')
225+
}
226+
ans = append(ans, byte((c+10)%10+'0'))
227+
if c < 0 {
228+
c = 1
229+
} else {
230+
c = 0
231+
}
232+
}
233+
for len(ans) > 1 && ans[len(ans)-1] == '0' {
234+
ans = ans[:len(ans)-1]
235+
}
236+
if neg {
237+
ans = append(ans, '-')
238+
}
239+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
240+
ans[i], ans[j] = ans[j], ans[i]
241+
}
242+
return string(ans)
243+
}
144244
```
145245

146246
### **JavaScript**
@@ -163,6 +263,40 @@ var addStrings = function (num1, num2) {
163263
}
164264
return ans.reverse().join('');
165265
};
266+
267+
/**
268+
* @param {string} num1
269+
* @param {string} num2
270+
* @return {string}
271+
*/
272+
var subStrings = function (num1, num2) {
273+
const m = num1.length;
274+
const n = num2.length;
275+
const neg = m < n || (m == n && num1 < num2);
276+
if (neg) {
277+
const t = num1;
278+
num1 = num2;
279+
num2 = t;
280+
}
281+
let i = num1.length - 1;
282+
let j = num2.length - 1;
283+
const ans = [];
284+
for (let c = 0; i >= 0; --i, --j) {
285+
c = parseInt(num1.charAt(i), 10) - c;
286+
if (j >= 0) {
287+
c -= parseInt(num2.charAt(j), 10);
288+
}
289+
ans.push((c + 10) % 10);
290+
c = c < 0 ? 1 : 0;
291+
}
292+
while (ans.length > 1 && ans[ans.length - 1] == '0') {
293+
ans.pop();
294+
}
295+
if (neg) {
296+
ans.push('-');
297+
}
298+
return ans.reverse().join('');
299+
};
166300
```
167301

168302
### **TypeScript**

‎solution/0400-0499/0415.Add Strings/README_EN.md‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ class Solution:
5858
ans.append(str(v))
5959
i, j = i - 1, j - 1
6060
return "".join(ans[::-1])
61+
62+
def subStrings(self, num1: str, num2: str) -> str:
63+
m, n = len(num1), len(num2)
64+
neg = m < n or (m == n and num1 < num2)
65+
if neg:
66+
num1, num2 = num2, num1
67+
i, j = len(num1) - 1, len(num2) - 1
68+
ans = []
69+
c = 0
70+
while i >= 0:
71+
c = int(num1[i]) - c - (0 if j < 0 else int(num2[j]))
72+
ans.append(str((c + 10) % 10))
73+
c = 1 if c < 0 else 0
74+
i, j = i - 1, j - 1
75+
while len(ans) > 1 and ans[-1] == '0':
76+
ans.pop()
77+
if neg:
78+
ans.append('-')
79+
return ''.join(ans[::-1])
6180
```
6281

6382
### **Java**
@@ -76,6 +95,30 @@ class Solution {
7695
}
7796
return ans.reverse().toString();
7897
}
98+
99+
public String subStrings(String num1, String num2) {
100+
int m = num1.length(), n = num2.length();
101+
boolean neg = m < n || (m == n && num1.compareTo(num2) < 0);
102+
if (neg) {
103+
String t = num1;
104+
num1 = num2;
105+
num2 = t;
106+
}
107+
int i = num1.length() - 1, j = num2.length() - 1;
108+
StringBuilder ans = new StringBuilder();
109+
for (int c = 0; i >= 0; --i, --j) {
110+
c = (num1.charAt(i) - '0') - c - (j < 0 ? 0 : num2.charAt(j) - '0');
111+
ans.append((c + 10) % 10);
112+
c = c < 0 ? 1 : 0;
113+
}
114+
while (ans.length() > 1 && ans.charAt(ans.length() - 1) == '0') {
115+
ans.deleteCharAt(ans.length() - 1);
116+
}
117+
if (neg) {
118+
ans.append('-');
119+
}
120+
return ans.reverse().toString();
121+
}
79122
}
80123
```
81124

@@ -97,6 +140,29 @@ public:
97140
reverse(ans.begin(), ans.end());
98141
return ans;
99142
}
143+
144+
string subStrings(string num1, string num2) {
145+
int m = num1.size(), n = num2.size();
146+
bool neg = m < n || (m == n && num1 < num2);
147+
if (neg) {
148+
swap(num1, num2);
149+
}
150+
int i = num1.size() - 1, j = num2.size() - 1;
151+
string ans;
152+
for (int c = 0; i >= 0; --i, --j) {
153+
c = (num1[i] - '0') - c - (j < 0 ? 0 : num2[j] - '0');
154+
ans += to_string((c + 10) % 10);
155+
c = c < 0 ? 1 : 0;
156+
}
157+
while (ans.size() > 1 && ans.back() == '0') {
158+
ans.pop_back();
159+
}
160+
if (neg) {
161+
ans.push_back('-');
162+
}
163+
reverse(ans.begin(), ans.end());
164+
return ans;
165+
}
100166
};
101167
```
102168

@@ -121,6 +187,38 @@ func addStrings(num1 string, num2 string) string {
121187
}
122188
return string(ans)
123189
}
190+
191+
func subStrings(num1 string, num2 string) string {
192+
m, n := len(num1), len(num2)
193+
neg := m < n || (m == n && num1 < num2)
194+
if neg {
195+
num1, num2 = num2, num1
196+
}
197+
i, j := len(num1)-1, len(num2)-1
198+
ans := []byte{}
199+
for c := 0; i >= 0; i, j = i-1, j-1 {
200+
c = int(num1[i]-'0') - c
201+
if j >= 0 {
202+
c -= int(num2[j] - '0')
203+
}
204+
ans = append(ans, byte((c+10)%10+'0'))
205+
if c < 0 {
206+
c = 1
207+
} else {
208+
c = 0
209+
}
210+
}
211+
for len(ans) > 1 && ans[len(ans)-1] == '0' {
212+
ans = ans[:len(ans)-1]
213+
}
214+
if neg {
215+
ans = append(ans, '-')
216+
}
217+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
218+
ans[i], ans[j] = ans[j], ans[i]
219+
}
220+
return string(ans)
221+
}
124222
```
125223

126224
### **JavaScript**
@@ -143,6 +241,40 @@ var addStrings = function (num1, num2) {
143241
}
144242
return ans.reverse().join('');
145243
};
244+
245+
/**
246+
* @param {string} num1
247+
* @param {string} num2
248+
* @return {string}
249+
*/
250+
var subStrings = function (num1, num2) {
251+
const m = num1.length;
252+
const n = num2.length;
253+
const neg = m < n || (m == n && num1 < num2);
254+
if (neg) {
255+
const t = num1;
256+
num1 = num2;
257+
num2 = t;
258+
}
259+
let i = num1.length - 1;
260+
let j = num2.length - 1;
261+
const ans = [];
262+
for (let c = 0; i >= 0; --i, --j) {
263+
c = parseInt(num1.charAt(i), 10) - c;
264+
if (j >= 0) {
265+
c -= parseInt(num2.charAt(j), 10);
266+
}
267+
ans.push((c + 10) % 10);
268+
c = c < 0 ? 1 : 0;
269+
}
270+
while (ans.length > 1 && ans[ans.length - 1] == '0') {
271+
ans.pop();
272+
}
273+
if (neg) {
274+
ans.push('-');
275+
}
276+
return ans.reverse().join('');
277+
};
146278
```
147279

148280
### **TypeScript**

‎solution/0400-0499/0415.Add Strings/Solution.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,27 @@ class Solution {
1313
reverse(ans.begin(), ans.end());
1414
return ans;
1515
}
16+
17+
string subStrings(string num1, string num2) {
18+
int m = num1.size(), n = num2.size();
19+
bool neg = m < n || (m == n && num1 < num2);
20+
if (neg) {
21+
swap(num1, num2);
22+
}
23+
int i = num1.size() - 1, j = num2.size() - 1;
24+
string ans;
25+
for (int c = 0; i >= 0; --i, --j) {
26+
c = (num1[i] - '0') - c - (j < 0 ? 0 : num2[j] - '0');
27+
ans += to_string((c + 10) % 10);
28+
c = c < 0 ? 1 : 0;
29+
}
30+
while (ans.size() > 1 && ans.back() == '0') {
31+
ans.pop_back();
32+
}
33+
if (neg) {
34+
ans.push_back('-');
35+
}
36+
reverse(ans.begin(), ans.end());
37+
return ans;
38+
}
1639
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /