@@ -74,13 +74,13 @@ targetWord 的数值为 "aaaa" -> "0000" -> 0
74
74
``` python
75
75
class Solution :
76
76
def isSumEqual (self , firstWord : str , secondWord : str , targetWord : str ) -> bool :
77
- def convert ( word ):
77
+ def f ( s ):
78
78
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' ))
82
81
return res
83
- return convert(firstWord) + convert(secondWord) == convert(targetWord)
82
+
83
+ return f(firstWord) + f(secondWord) == f(targetWord)
84
84
```
85
85
86
86
### ** Java**
@@ -90,14 +90,13 @@ class Solution:
90
90
``` java
91
91
class Solution {
92
92
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);
94
94
}
95
95
96
- private int convert (String word ) {
96
+ private int f (String s ) {
97
97
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' );
101
100
}
102
101
return res;
103
102
}
@@ -110,15 +109,12 @@ class Solution {
110
109
class Solution {
111
110
public:
112
111
bool isSumEqual(string firstWord, string secondWord, string targetWord) {
113
- return convert (firstWord) + convert (secondWord) == convert (targetWord);
112
+ return f (firstWord) + f (secondWord) == f (targetWord);
114
113
}
115
- private:
116
- int convert (string word ) {
114
+
115
+ int f (string s ) {
117
116
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');
122
118
return res;
123
119
}
124
120
};
@@ -134,23 +130,29 @@ private:
134
130
* @return {boolean}
135
131
*/
136
132
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;
147
139
}
148
- return true ;
140
+ return f (firstWord) + f (secondWord) == f (targetWord) ;
149
141
};
142
+ ```
150
143
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)
154
156
}
155
157
```
156
158
0 commit comments