Skip to content

Commit ac77ddd

Browse files
committed
feat: add solutions to lc problem: No.2496
No.2496.Maximum Value of a String in an Array
1 parent ab21931 commit ac77ddd

File tree

8 files changed

+193
-99
lines changed

8 files changed

+193
-99
lines changed

solution/2400-2499/2496.Maximum Value of a String in an Array/README.md

+64-25
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@
5555

5656
**方法一:模拟**
5757

58-
根据题意模拟即可
58+
我们定义一个函数 $f(s)$,用于计算字符串 $s$ 的值。如果 $s$ 只包含数字,那么 $f(s)$ 就是 $s$ 在十进制下的值;否则 $f(s)$ 就是 $s$ 的长度
5959

60-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `strs` 的长度。
60+
答案为 $\max\limits_{s \in \textit{strs}} f(s)$。
61+
62+
时间复杂度 $O(n)$,其中 $n$ 是数组 $strs$ 的长度。空间复杂度 $O(1)$。
6163

6264
<!-- tabs:start -->
6365

@@ -68,12 +70,26 @@
6870
```python
6971
class Solution:
7072
def maximumValue(self, strs: List[str]) -> int:
71-
def f(s):
73+
def f(s: str) -> int:
7274
return int(s) if all(c.isdigit() for c in s) else len(s)
7375

7476
return max(f(s) for s in strs)
7577
```
7678

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+
7793
### **Java**
7894

7995
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -82,19 +98,22 @@ class Solution:
8298
class Solution {
8399
public int maximumValue(String[] strs) {
84100
int ans = 0;
85-
for (String s : strs) {
101+
for (var s : strs) {
86102
ans = Math.max(ans, f(s));
87103
}
88104
return ans;
89105
}
90106

91107
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;
95113
}
114+
x = x * 10 + (c - '0');
96115
}
97-
return Integer.parseInt(s);
116+
return x;
98117
}
99118
}
100119
```
@@ -106,15 +125,19 @@ class Solution {
106125
public:
107126
int maximumValue(vector<string>& strs) {
108127
auto f = [](string& s) {
109-
int n = s.size(), m = 0;
128+
int x = 0;
110129
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';
113134
}
114-
return m;
135+
return x;
115136
};
116137
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+
}
118141
return ans;
119142
}
120143
};
@@ -124,19 +147,18 @@ public:
124147
125148
```go
126149
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) {
129151
for _, c := range s {
130152
if c >= 'a' && c <= 'z' {
131-
return n
153+
return len(s)
132154
}
133-
m = m*10 + int(c-'0')
155+
x = x*10 + int(c-'0')
134156
}
135-
return m
157+
return
136158
}
137159
for _, s := range strs {
138-
if t := f(s); ans < t {
139-
ans = t
160+
if x := f(s); ans < x {
161+
ans = x
140162
}
141163
}
142164
return
@@ -147,12 +169,29 @@ func maximumValue(strs []string) (ans int) {
147169

148170
```ts
149171
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;
154194
}
155-
return ans;
156195
}
157196
```
158197

solution/2400-2499/2496.Maximum Value of a String in an Array/README_EN.md

+60-23
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,48 @@ Each string in the array has value 1. Hence, we return 1.
5555
```python
5656
class Solution:
5757
def maximumValue(self, strs: List[str]) -> int:
58-
def f(s):
58+
def f(s: str) -> int:
5959
return int(s) if all(c.isdigit() for c in s) else len(s)
6060

6161
return max(f(s) for s in strs)
6262
```
6363

64+
```python
65+
class Solution:
66+
def maximumValue(self, strs: List[str]) -> int:
67+
def f(s: str) -> int:
68+
x = 0
69+
for c in s:
70+
if c.isalpha():
71+
return len(s)
72+
x = x * 10 + ord(c) - ord("0")
73+
return x
74+
75+
return max(f(s) for s in strs)
76+
```
77+
6478
### **Java**
6579

6680
```java
6781
class Solution {
6882
public int maximumValue(String[] strs) {
6983
int ans = 0;
70-
for (String s : strs) {
84+
for (var s : strs) {
7185
ans = Math.max(ans, f(s));
7286
}
7387
return ans;
7488
}
7589

7690
private int f(String s) {
77-
for (int i = 0; i < s.length(); ++i) {
78-
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
79-
return s.length();
91+
int x = 0;
92+
for (int i = 0, n = s.length(); i < n; ++i) {
93+
char c = s.charAt(i);
94+
if (Character.isLetter(c)) {
95+
return n;
8096
}
97+
x = x * 10 + (c - '0');
8198
}
82-
return Integer.parseInt(s);
99+
return x;
83100
}
84101
}
85102
```
@@ -91,15 +108,19 @@ class Solution {
91108
public:
92109
int maximumValue(vector<string>& strs) {
93110
auto f = [](string& s) {
94-
int n = s.size(), m = 0;
111+
int x = 0;
95112
for (char& c : s) {
96-
if (!isdigit(c)) return n;
97-
m = m * 10 + (c - '0');
113+
if (!isdigit(c)) {
114+
return (int) s.size();
115+
}
116+
x = x * 10 + c - '0';
98117
}
99-
return m;
118+
return x;
100119
};
101120
int ans = 0;
102-
for (auto& s : strs) ans = max(ans, f(s));
121+
for (auto& s : strs) {
122+
ans = max(ans, f(s));
123+
}
103124
return ans;
104125
}
105126
};
@@ -109,19 +130,18 @@ public:
109130
110131
```go
111132
func maximumValue(strs []string) (ans int) {
112-
f := func(s string) int {
113-
n, m := len(s), 0
133+
f := func(s string) (x int) {
114134
for _, c := range s {
115135
if c >= 'a' && c <= 'z' {
116-
return n
136+
return len(s)
117137
}
118-
m = m*10 + int(c-'0')
138+
x = x*10 + int(c-'0')
119139
}
120-
return m
140+
return
121141
}
122142
for _, s := range strs {
123-
if t := f(s); ans < t {
124-
ans = t
143+
if x := f(s); ans < x {
144+
ans = x
125145
}
126146
}
127147
return
@@ -132,12 +152,29 @@ func maximumValue(strs []string) (ans int) {
132152

133153
```ts
134154
function maximumValue(strs: string[]): number {
135-
let ans = 0;
136-
for (const s of strs) {
137-
const num = Number(s);
138-
ans = Math.max(ans, Number.isNaN(num) ? s.length : num);
155+
const f = (s: string) => (Number.isNaN(Number(s)) ? s.length : Number(s));
156+
return Math.max(...strs.map(f));
157+
}
158+
```
159+
160+
### **C#**
161+
162+
```cs
163+
public class Solution {
164+
public int MaximumValue(string[] strs) {
165+
return strs.Max(f);
166+
}
167+
168+
private int f(string s) {
169+
int x = 0;
170+
foreach (var c in s) {
171+
if (c >= 'a') {
172+
return s.Length;
173+
}
174+
x = x * 10 + (c - '0');
175+
}
176+
return x;
139177
}
140-
return ans;
141178
}
142179
```
143180

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
class Solution {
2-
public:
3-
int maximumValue(vector<string>& strs) {
4-
auto f = [](string& s) {
5-
int n = s.size(), m = 0;
6-
for (char& c : s) {
7-
if (!isdigit(c)) return n;
8-
m = m * 10 + (c - '0');
9-
}
10-
return m;
11-
};
12-
int ans = 0;
13-
for (auto& s : strs) ans = max(ans, f(s));
14-
return ans;
15-
}
1+
class Solution {
2+
public:
3+
int maximumValue(vector<string>& strs) {
4+
auto f = [](string& s) {
5+
int x = 0;
6+
for (char& c : s) {
7+
if (!isdigit(c)) {
8+
return (int) s.size();
9+
}
10+
x = x * 10 + c - '0';
11+
}
12+
return x;
13+
};
14+
int ans = 0;
15+
for (auto& s : strs) {
16+
ans = max(ans, f(s));
17+
}
18+
return ans;
19+
}
1620
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int MaximumValue(string[] strs) {
3+
return strs.Max(f);
4+
}
5+
6+
private int f(string s) {
7+
int x = 0;
8+
foreach (var c in s) {
9+
if (c >= 'a') {
10+
return s.Length;
11+
}
12+
x = x * 10 + (c - '0');
13+
}
14+
return x;
15+
}
16+
}

solution/2400-2499/2496.Maximum Value of a String in an Array/Solution.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
func maximumValue(strs []string) (ans int) {
2-
f := func(s string) int {
3-
n, m := len(s), 0
2+
f := func(s string) (x int) {
43
for _, c := range s {
54
if c >= 'a' && c <= 'z' {
6-
return n
5+
return len(s)
76
}
8-
m = m*10 + int(c-'0')
7+
x = x*10 + int(c-'0')
98
}
10-
return m
9+
return
1110
}
1211
for _, s := range strs {
13-
if t := f(s); ans < t {
14-
ans = t
12+
if x := f(s); ans < x {
13+
ans = x
1514
}
1615
}
1716
return

0 commit comments

Comments
 (0)