74
74
75
75
<!-- 这里可写通用的实现逻辑 -->
76
76
77
+ ** 方法一:模拟**
78
+
79
+ 我们可以将字符串 $s$ 按空格分割成若干个单词。然后遍历每个单词,判断其是否为数字,若是数字,则将其转换为整数,与前一个数字比较,若不严格递增,返回 ` false ` ,否则,将当前数字赋值给前一个数字,继续遍历。
80
+
81
+ 遍历结束,说明字符串中的数字严格递增,返回 ` true ` 。
82
+
83
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
84
+
77
85
<!-- tabs:start -->
78
86
79
87
### ** Python3**
83
91
``` python
84
92
class Solution :
85
93
def areNumbersAscending (self , s : str ) -> bool :
86
- curr = 0
87
- for t in s.split(' ' ):
94
+ pre = 0
95
+ for t in s.split():
88
96
if t[0 ].isdigit():
89
- if curr >= int (t):
97
+ if (cur := int (t)) <= pre:
98
+ return False
99
+ pre = cur
100
+ return True
101
+ ```
102
+
103
+ ``` python
104
+ class Solution :
105
+ def areNumbersAscending (self , s : str ) -> bool :
106
+ pre = i = 0
107
+ n = len (s)
108
+ while i < n:
109
+ if s[i].isdigit():
110
+ cur = 0
111
+ while i < n and s[i].isdigit():
112
+ cur = cur * 10 + int (s[i])
113
+ i += 1
114
+ if pre >= cur:
90
115
return False
91
- curr = int (t)
116
+ pre = cur
117
+ else :
118
+ i += 1
92
119
return True
93
120
```
94
121
@@ -99,53 +126,37 @@ class Solution:
99
126
``` java
100
127
class Solution {
101
128
public boolean areNumbersAscending (String s ) {
102
- int curr = 0 ;
103
- for (String t : s. split(" " )) {
104
- char c = t. charAt(0 );
105
- if (Character . isDigit(c)) {
106
- int x = Integer . parseInt(t);
107
- if (curr >= x) {
129
+ int pre = 0 ;
130
+ for (var t : s. split(" " )) {
131
+ if (t. charAt(0 ) <= ' 9' ) {
132
+ int cur = Integer . parseInt(t);
133
+ if (pre >= cur) {
108
134
return false ;
109
135
}
110
- curr = x ;
136
+ pre = cur ;
111
137
}
112
138
}
113
139
return true ;
114
140
}
115
141
}
116
142
```
117
143
118
- ### ** TypeScript**
119
-
120
- ``` ts
121
- function areNumbersAscending(s : string ): boolean {
122
- let strs = s .split (' ' );
123
- let prev = Number .MIN_SAFE_INTEGER;
124
- for (let str of strs ) {
125
- let num = Number (str );
126
- if (! isNaN (num )) {
127
- if (num <= prev ) return false ;
128
- prev = num ;
129
- }
130
- }
131
- return true ;
132
- }
133
- ```
134
-
135
144
### ** C++**
136
145
137
146
``` cpp
138
147
class Solution {
139
148
public:
140
149
bool areNumbersAscending(string s) {
141
- int curr = 0;
150
+ int pre = 0;
142
151
istringstream is(s);
143
152
string t;
144
153
while (is >> t) {
145
154
if (isdigit(t[ 0] )) {
146
- int x = stoi(t);
147
- if (curr >= x) return false;
148
- curr = x;
155
+ int cur = stoi(t);
156
+ if (pre >= cur) {
157
+ return false;
158
+ }
159
+ pre = cur;
149
160
}
150
161
}
151
162
return true;
@@ -157,20 +168,37 @@ public:
157
168
158
169
```go
159
170
func areNumbersAscending(s string) bool {
160
- curr := 0
171
+ pre := 0
161
172
for _, t := range strings.Split(s, " ") {
162
- if unicode.IsDigit(rune( t[0])) {
163
- x , _ := strconv.Atoi(t)
164
- if curr >= x {
173
+ if t[0] <= '9' {
174
+ cur , _ := strconv.Atoi(t)
175
+ if pre >= cur {
165
176
return false
166
177
}
167
- curr = x
178
+ pre = cur
168
179
}
169
180
}
170
181
return true
171
182
}
172
183
```
173
184
185
+ ### ** TypeScript**
186
+
187
+ ``` ts
188
+ function areNumbersAscending(s : string ): boolean {
189
+ let strs = s .split (' ' );
190
+ let prev = Number .MIN_SAFE_INTEGER;
191
+ for (let str of strs ) {
192
+ let num = Number (str );
193
+ if (! isNaN (num )) {
194
+ if (num <= prev ) return false ;
195
+ prev = num ;
196
+ }
197
+ }
198
+ return true ;
199
+ }
200
+ ```
201
+
174
202
### ** ...**
175
203
176
204
```
0 commit comments