55
55
56
56
** 方法一:模拟**
57
57
58
- 根据题意模拟即可 。
58
+ 我们定义一个函数 $f(s)$,用于计算字符串 $s$ 的值。如果 $s$ 只包含数字,那么 $f(s)$ 就是 $s$ 在十进制下的值;否则 $f(s)$ 就是 $s$ 的长度 。
59
59
60
- 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 ` strs ` 的长度。
60
+ 答案为 $\max\limits_ {s \in \textit{strs}} f(s)$。
61
+
62
+ 时间复杂度 $O(n)$,其中 $n$ 是数组 $strs$ 的长度。空间复杂度 $O(1)$。
61
63
62
64
<!-- tabs:start -->
63
65
68
70
``` python
69
71
class Solution :
70
72
def maximumValue (self , strs : List[str ]) -> int :
71
- def f (s ) :
73
+ def f (s : str ) -> int :
72
74
return int (s) if all (c.isdigit() for c in s) else len (s)
73
75
74
76
return max (f(s) for s in strs)
75
77
```
76
78
79
+ ``` python
80
+ class Solution :
81
+ def maximumValue (self , strs : List[str ]) -> int :
82
+ def f (s : str ) -> int :
83
+ x = 0
84
+ for c in s:
85
+ if c.isalpha():
86
+ return len (s)
87
+ x = x * 10 + ord (c) - ord (" 0" )
88
+ return x
89
+
90
+ return max (f(s) for s in strs)
91
+ ```
92
+
77
93
### ** Java**
78
94
79
95
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -82,19 +98,22 @@ class Solution:
82
98
class Solution {
83
99
public int maximumValue (String [] strs ) {
84
100
int ans = 0 ;
85
- for (String s : strs) {
101
+ for (var s : strs) {
86
102
ans = Math . max(ans, f(s));
87
103
}
88
104
return ans;
89
105
}
90
106
91
107
private int f (String s ) {
92
- for (int i = 0 ; i < s. length(); ++ i) {
93
- if (s. charAt(i) >= ' a' && s. charAt(i) <= ' z' ) {
94
- return s. length();
108
+ int x = 0 ;
109
+ for (int i = 0 , n = s. length(); i < n; ++ i) {
110
+ char c = s. charAt(i);
111
+ if (Character . isLetter(c)) {
112
+ return n;
95
113
}
114
+ x = x * 10 + (c - ' 0' );
96
115
}
97
- return Integer . parseInt(s) ;
116
+ return x ;
98
117
}
99
118
}
100
119
```
@@ -106,15 +125,19 @@ class Solution {
106
125
public:
107
126
int maximumValue(vector<string >& strs) {
108
127
auto f = [ ] (string& s) {
109
- int n = s.size(), m = 0;
128
+ int x = 0;
110
129
for (char& c : s) {
111
- if (!isdigit(c)) return n;
112
- m = m * 10 + (c - '0');
130
+ if (!isdigit(c)) {
131
+ return (int) s.size();
132
+ }
133
+ x = x * 10 + c - '0';
113
134
}
114
- return m ;
135
+ return x ;
115
136
};
116
137
int ans = 0;
117
- for (auto& s : strs) ans = max(ans, f(s));
138
+ for (auto& s : strs) {
139
+ ans = max(ans, f(s));
140
+ }
118
141
return ans;
119
142
}
120
143
};
@@ -124,19 +147,18 @@ public:
124
147
125
148
```go
126
149
func maximumValue(strs []string) (ans int) {
127
- f := func(s string) int {
128
- n, m := len(s), 0
150
+ f := func(s string) (x int) {
129
151
for _, c := range s {
130
152
if c >= 'a' && c <= 'z' {
131
- return n
153
+ return len(s)
132
154
}
133
- m = m *10 + int(c-'0')
155
+ x = x *10 + int(c-'0')
134
156
}
135
- return m
157
+ return
136
158
}
137
159
for _, s := range strs {
138
- if t := f(s); ans < t {
139
- ans = t
160
+ if x := f(s); ans < x {
161
+ ans = x
140
162
}
141
163
}
142
164
return
@@ -147,12 +169,29 @@ func maximumValue(strs []string) (ans int) {
147
169
148
170
``` ts
149
171
function maximumValue(strs : string []): number {
150
- let ans = 0 ;
151
- for (const s of strs ) {
152
- const num = Number (s );
153
- ans = Math .max (ans , Number .isNaN (num ) ? s .length : num );
172
+ const f = (s : string ) => (Number .isNaN (Number (s )) ? s .length : Number (s ));
173
+ return Math .max (... strs .map (f ));
174
+ }
175
+ ```
176
+
177
+ ### ** C#**
178
+
179
+ ``` cs
180
+ public class Solution {
181
+ public int MaximumValue (string [] strs ) {
182
+ return strs .Max (f );
183
+ }
184
+
185
+ private int f (string s ) {
186
+ int x = 0 ;
187
+ foreach (var c in s ) {
188
+ if (c >= 'a' ) {
189
+ return s .Length ;
190
+ }
191
+ x = x * 10 + (c - '0' );
192
+ }
193
+ return x ;
154
194
}
155
- return ans ;
156
195
}
157
196
```
158
197
0 commit comments