Skip to content

Commit 2179d83

Browse files
committed
feat: add solutions to lc problem: No.1880,1881
* No.1880.Check if Word Equals Summation of Two Words * No.1881.Maximum Value after Insertion
1 parent 2365750 commit 2179d83

File tree

13 files changed

+223
-194
lines changed

13 files changed

+223
-194
lines changed

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md

+33-31
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ targetWord 的数值为 "aaaa" -> "0000" -> 0
7474
```python
7575
class Solution:
7676
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
77-
def convert(word):
77+
def f(s):
7878
res = 0
79-
for c in word:
80-
res *= 10
81-
res += (ord(c) - ord('a'))
79+
for c in s:
80+
res = res * 10 + (ord(c) - ord('a'))
8281
return res
83-
return convert(firstWord) + convert(secondWord) == convert(targetWord)
82+
83+
return f(firstWord) + f(secondWord) == f(targetWord)
8484
```
8585

8686
### **Java**
@@ -90,14 +90,13 @@ class Solution:
9090
```java
9191
class Solution {
9292
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
93-
return convert(firstWord) + convert(secondWord) == convert(targetWord);
93+
return f(firstWord) + f(secondWord) == f(targetWord);
9494
}
9595

96-
private int convert(String word) {
96+
private int f(String s) {
9797
int res = 0;
98-
for (char c : word.toCharArray()) {
99-
res *= 10;
100-
res += (c - 'a');
98+
for (char c : s.toCharArray()) {
99+
res = res * 10 + (c - 'a');
101100
}
102101
return res;
103102
}
@@ -110,15 +109,12 @@ class Solution {
110109
class Solution {
111110
public:
112111
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
113-
return convert(firstWord) + convert(secondWord) == convert(targetWord);
112+
return f(firstWord) + f(secondWord) == f(targetWord);
114113
}
115-
private:
116-
int convert(string word) {
114+
115+
int f(string s) {
117116
int res = 0;
118-
for (char c : word) {
119-
res *= 10;
120-
res += (c - 'a');
121-
}
117+
for (char c : s) res = res * 10 + (c - 'a');
122118
return res;
123119
}
124120
};
@@ -134,23 +130,29 @@ private:
134130
* @return {boolean}
135131
*/
136132
var isSumEqual = function (firstWord, secondWord, targetWord) {
137-
let carry = 0;
138-
let n1 = firstWord.length,
139-
n2 = secondWord.length;
140-
let n3 = targetWord.length;
141-
for (let i = 0; i < n3; i++) {
142-
let num1 = getNum(firstWord.charAt(n1 - 1 - i));
143-
let num2 = getNum(secondWord.charAt(n2 - 1 - i));
144-
let sum = carry + num1 + num2;
145-
if (getNum(targetWord.charAt(n3 - 1 - i)) != sum % 10) return false;
146-
carry = parseInt(sum / 10);
133+
function f(s) {
134+
let res = 0;
135+
for (let c of s) {
136+
res = res * 10 + (c.charCodeAt() - 'a'.charCodeAt());
137+
}
138+
return res;
147139
}
148-
return true;
140+
return f(firstWord) + f(secondWord) == f(targetWord);
149141
};
142+
```
150143

151-
function getNum(char) {
152-
if (!char) return 0;
153-
return char.charCodeAt() - 'a'.charCodeAt();
144+
### **Go**
145+
146+
```go
147+
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
148+
f := func(s string) int {
149+
res := 0
150+
for _, c := range s {
151+
res = res*10 + int(c-'a')
152+
}
153+
return res
154+
}
155+
return f(firstWord)+f(secondWord) == f(targetWord)
154156
}
155157
```
156158

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md

+33-31
Original file line numberDiff line numberDiff line change
@@ -70,28 +70,27 @@ We return true because 0 + 0 == 0.
7070
```python
7171
class Solution:
7272
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
73-
def convert(word):
73+
def f(s):
7474
res = 0
75-
for c in word:
76-
res *= 10
77-
res += (ord(c) - ord('a'))
75+
for c in s:
76+
res = res * 10 + (ord(c) - ord('a'))
7877
return res
79-
return convert(firstWord) + convert(secondWord) == convert(targetWord)
78+
79+
return f(firstWord) + f(secondWord) == f(targetWord)
8080
```
8181

8282
### **Java**
8383

8484
```java
8585
class Solution {
8686
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
87-
return convert(firstWord) + convert(secondWord) == convert(targetWord);
87+
return f(firstWord) + f(secondWord) == f(targetWord);
8888
}
8989

90-
private int convert(String word) {
90+
private int f(String s) {
9191
int res = 0;
92-
for (char c : word.toCharArray()) {
93-
res *= 10;
94-
res += (c - 'a');
92+
for (char c : s.toCharArray()) {
93+
res = res * 10 + (c - 'a');
9594
}
9695
return res;
9796
}
@@ -104,15 +103,12 @@ class Solution {
104103
class Solution {
105104
public:
106105
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
107-
return convert(firstWord) + convert(secondWord) == convert(targetWord);
106+
return f(firstWord) + f(secondWord) == f(targetWord);
108107
}
109-
private:
110-
int convert(string word) {
108+
109+
int f(string s) {
111110
int res = 0;
112-
for (char c : word) {
113-
res *= 10;
114-
res += (c - 'a');
115-
}
111+
for (char c : s) res = res * 10 + (c - 'a');
116112
return res;
117113
}
118114
};
@@ -128,23 +124,29 @@ private:
128124
* @return {boolean}
129125
*/
130126
var isSumEqual = function (firstWord, secondWord, targetWord) {
131-
let carry = 0;
132-
let n1 = firstWord.length,
133-
n2 = secondWord.length;
134-
let n3 = targetWord.length;
135-
for (let i = 0; i < n3; i++) {
136-
let num1 = getNum(firstWord.charAt(n1 - 1 - i));
137-
let num2 = getNum(secondWord.charAt(n2 - 1 - i));
138-
let sum = carry + num1 + num2;
139-
if (getNum(targetWord.charAt(n3 - 1 - i)) != sum % 10) return false;
140-
carry = parseInt(sum / 10);
127+
function f(s) {
128+
let res = 0;
129+
for (let c of s) {
130+
res = res * 10 + (c.charCodeAt() - 'a'.charCodeAt());
131+
}
132+
return res;
141133
}
142-
return true;
134+
return f(firstWord) + f(secondWord) == f(targetWord);
143135
};
136+
```
144137

145-
function getNum(char) {
146-
if (!char) return 0;
147-
return char.charCodeAt() - 'a'.charCodeAt();
138+
### **Go**
139+
140+
```go
141+
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
142+
f := func(s string) int {
143+
res := 0
144+
for _, c := range s {
145+
res = res*10 + int(c-'a')
146+
}
147+
return res
148+
}
149+
return f(firstWord)+f(secondWord) == f(targetWord)
148150
}
149151
```
150152

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
class Solution {
22
public:
33
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
4-
return convert(firstWord) + convert(secondWord) == convert(targetWord);
4+
return f(firstWord) + f(secondWord) == f(targetWord);
55
}
6-
private:
7-
int convert(string word) {
6+
7+
int f(string s) {
88
int res = 0;
9-
for (char c : word) {
10-
res *= 10;
11-
res += (c - 'a');
12-
}
9+
for (char c : s) res = res * 10 + (c - 'a');
1310
return res;
1411
}
1512
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func isSumEqual(firstWord string, secondWord string, targetWord string) bool {
2+
f := func(s string) int {
3+
res := 0
4+
for _, c := range s {
5+
res = res*10 + int(c-'a')
6+
}
7+
return res
8+
}
9+
return f(firstWord)+f(secondWord) == f(targetWord)
10+
}

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class Solution {
22
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
3-
return convert(firstWord) + convert(secondWord) == convert(targetWord);
3+
return f(firstWord) + f(secondWord) == f(targetWord);
44
}
55

6-
private int convert(String word) {
6+
private int f(String s) {
77
int res = 0;
8-
for (char c : word.toCharArray()) {
9-
res *= 10;
10-
res += (c - 'a');
8+
for (char c : s.toCharArray()) {
9+
res = res * 10 + (c - 'a');
1110
}
1211
return res;
1312
}

solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.js

+7-16
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@
55
* @return {boolean}
66
*/
77
var isSumEqual = function (firstWord, secondWord, targetWord) {
8-
let carry = 0;
9-
let n1 = firstWord.length,
10-
n2 = secondWord.length;
11-
let n3 = targetWord.length;
12-
for (let i = 0; i < n3; i++) {
13-
let num1 = getNum(firstWord.charAt(n1 - 1 - i));
14-
let num2 = getNum(secondWord.charAt(n2 - 1 - i));
15-
let sum = carry + num1 + num2;
16-
if (getNum(targetWord.charAt(n3 - 1 - i)) != sum % 10) return false;
17-
carry = parseInt(sum / 10);
8+
function f(s) {
9+
let res = 0;
10+
for (let c of s) {
11+
res = res * 10 + (c.charCodeAt() - 'a'.charCodeAt());
12+
}
13+
return res;
1814
}
19-
return true;
15+
return f(firstWord) + f(secondWord) == f(targetWord);
2016
};
21-
22-
function getNum(char) {
23-
if (!char) return 0;
24-
return char.charCodeAt() - 'a'.charCodeAt();
25-
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class Solution:
22
def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
3-
def convert(word):
3+
def f(s):
44
res = 0
5-
for c in word:
6-
res *= 10
7-
res += ord(c) - ord('a')
5+
for c in s:
6+
res = res * 10 + (ord(c) - ord('a'))
87
return res
98

10-
return convert(firstWord) + convert(secondWord) == convert(targetWord)
9+
return f(firstWord) + f(secondWord) == f(targetWord)

solution/1800-1899/1881.Maximum Value after Insertion/README.md

+46-33
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,16 @@
6060
```python
6161
class Solution:
6262
def maxValue(self, n: str, x: int) -> str:
63-
negative = n[0] == '-'
64-
i, res = 0, []
65-
if negative:
66-
i += 1
67-
res.append('-')
68-
find = False
69-
while i < len(n):
70-
num = int(n[i])
71-
if (negative and x < num) or (not negative and x > num):
72-
res.append(str(x))
73-
find = True
74-
break
75-
res.append(n[i])
76-
i += 1
77-
res.append(n[i:] if find else str(x))
78-
return ''.join(res)
63+
if n[0] != '-':
64+
for i, c in enumerate(n):
65+
if int(c) < x:
66+
return n[:i] + str(x) + n[i:]
67+
return n + str(x)
68+
else:
69+
for i, c in enumerate(n[1:]):
70+
if int(c) > x:
71+
return n[:i + 1] + str(x) + n[i + 1:]
72+
return n + str(x)
7973
```
8074

8175
### **Java**
@@ -85,29 +79,48 @@ class Solution:
8579
```java
8680
class Solution {
8781
public String maxValue(String n, int x) {
88-
boolean negative = n.charAt(0) == '-';
89-
StringBuilder res = new StringBuilder();
9082
int i = 0;
91-
if (negative) {
92-
++i;
93-
res.append("-");
83+
if (n.charAt(0) != '-') {
84+
for (; i < n.length() && n.charAt(i) - '0' >= x; ++i);
85+
} else {
86+
for (i = 1; i < n.length() && n.charAt(i) - '0' <= x; ++i);
9487
}
95-
boolean find = false;
96-
for (; i < n.length(); ++i) {
97-
int num = n.charAt(i) - '0';
98-
if ((negative && x < num) || (!negative && x > num)) {
99-
res.append(x);
100-
find = true;
101-
break;
102-
}
103-
res.append(n.charAt(i));
104-
}
105-
res.append(find ? n.substring(i) : x);
106-
return res.toString();
88+
return n.substring(0, i) + x + n.substring(i);
10789
}
10890
}
10991
```
11092

93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
string maxValue(string n, int x) {
99+
int i = 0;
100+
if (n[0] != '-') for (; i < n.size() && n[i] - '0' >= x; ++i);
101+
else for (i = 1; i < n.size() && n[i] - '0' <= x; ++i);
102+
return n.substr(0, i) + to_string(x) + n.substr(i);
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func maxValue(n string, x int) string {
111+
i := 0
112+
y := byte('0' + x)
113+
if n[0] != '-' {
114+
for ; i < len(n) && n[i] >= y; i++ {
115+
}
116+
} else {
117+
for i = 1; i < len(n) && n[i] <= y; i++ {
118+
}
119+
}
120+
return n[:i] + string(y) + n[i:]
121+
}
122+
```
123+
111124
### **JavaScript**
112125

113126
```js

0 commit comments

Comments
 (0)