Skip to content

Commit 3f6f70e

Browse files
committed
feat: add solutions to lc problem: No.2042
No.2042.Check if Numbers Are Ascending in a Sentence
1 parent 7446980 commit 3f6f70e

File tree

6 files changed

+143
-94
lines changed

6 files changed

+143
-94
lines changed

solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/README.md

+65-37
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77+
**方法一:模拟**
78+
79+
我们可以将字符串 $s$ 按空格分割成若干个单词。然后遍历每个单词,判断其是否为数字,若是数字,则将其转换为整数,与前一个数字比较,若不严格递增,返回 `false`,否则,将当前数字赋值给前一个数字,继续遍历。
80+
81+
遍历结束,说明字符串中的数字严格递增,返回 `true`
82+
83+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
84+
7785
<!-- tabs:start -->
7886

7987
### **Python3**
@@ -83,12 +91,31 @@
8391
```python
8492
class Solution:
8593
def areNumbersAscending(self, s: str) -> bool:
86-
curr = 0
87-
for t in s.split(' '):
94+
pre = 0
95+
for t in s.split():
8896
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:
90115
return False
91-
curr = int(t)
116+
pre = cur
117+
else:
118+
i += 1
92119
return True
93120
```
94121

@@ -99,53 +126,37 @@ class Solution:
99126
```java
100127
class Solution {
101128
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) {
108134
return false;
109135
}
110-
curr = x;
136+
pre = cur;
111137
}
112138
}
113139
return true;
114140
}
115141
}
116142
```
117143

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-
135144
### **C++**
136145

137146
```cpp
138147
class Solution {
139148
public:
140149
bool areNumbersAscending(string s) {
141-
int curr = 0;
150+
int pre = 0;
142151
istringstream is(s);
143152
string t;
144153
while (is >> t) {
145154
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;
149160
}
150161
}
151162
return true;
@@ -157,20 +168,37 @@ public:
157168
158169
```go
159170
func areNumbersAscending(s string) bool {
160-
curr := 0
171+
pre := 0
161172
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 {
165176
return false
166177
}
167-
curr = x
178+
pre = cur
168179
}
169180
}
170181
return true
171182
}
172183
```
173184

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+
174202
### **...**
175203

176204
```

solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/README_EN.md

+57-37
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,31 @@ They are strictly increasing from left to right: 1 &lt; 3 &lt; 4 &lt; 6 &lt; 12.
6262
```python
6363
class Solution:
6464
def areNumbersAscending(self, s: str) -> bool:
65-
curr = 0
66-
for t in s.split(' '):
65+
pre = 0
66+
for t in s.split():
6767
if t[0].isdigit():
68-
if curr >= int(t):
68+
if (cur := int(t)) <= pre:
6969
return False
70-
curr = int(t)
70+
pre = cur
71+
return True
72+
```
73+
74+
```python
75+
class Solution:
76+
def areNumbersAscending(self, s: str) -> bool:
77+
pre = i = 0
78+
n = len(s)
79+
while i < n:
80+
if s[i].isdigit():
81+
cur = 0
82+
while i < n and s[i].isdigit():
83+
cur = cur * 10 + int(s[i])
84+
i += 1
85+
if pre >= cur:
86+
return False
87+
pre = cur
88+
else:
89+
i += 1
7190
return True
7291
```
7392

@@ -76,53 +95,37 @@ class Solution:
7695
```java
7796
class Solution {
7897
public boolean areNumbersAscending(String s) {
79-
int curr = 0;
80-
for (String t : s.split(" ")) {
81-
char c = t.charAt(0);
82-
if (Character.isDigit(c)) {
83-
int x = Integer.parseInt(t);
84-
if (curr >= x) {
98+
int pre = 0;
99+
for (var t : s.split(" ")) {
100+
if (t.charAt(0) <= '9') {
101+
int cur = Integer.parseInt(t);
102+
if (pre >= cur) {
85103
return false;
86104
}
87-
curr = x;
105+
pre = cur;
88106
}
89107
}
90108
return true;
91109
}
92110
}
93111
```
94112

95-
### **TypeScript**
96-
97-
```ts
98-
function areNumbersAscending(s: string): boolean {
99-
let strs = s.split(' ');
100-
let prev = Number.MIN_SAFE_INTEGER;
101-
for (let str of strs) {
102-
let num = Number(str);
103-
if (!isNaN(num)) {
104-
if (num <= prev) return false;
105-
prev = num;
106-
}
107-
}
108-
return true;
109-
}
110-
```
111-
112113
### **C++**
113114

114115
```cpp
115116
class Solution {
116117
public:
117118
bool areNumbersAscending(string s) {
118-
int curr = 0;
119+
int pre = 0;
119120
istringstream is(s);
120121
string t;
121122
while (is >> t) {
122123
if (isdigit(t[0])) {
123-
int x = stoi(t);
124-
if (curr >= x) return false;
125-
curr = x;
124+
int cur = stoi(t);
125+
if (pre >= cur) {
126+
return false;
127+
}
128+
pre = cur;
126129
}
127130
}
128131
return true;
@@ -134,20 +137,37 @@ public:
134137
135138
```go
136139
func areNumbersAscending(s string) bool {
137-
curr := 0
140+
pre := 0
138141
for _, t := range strings.Split(s, " ") {
139-
if unicode.IsDigit(rune(t[0])) {
140-
x, _ := strconv.Atoi(t)
141-
if curr >= x {
142+
if t[0] <= '9' {
143+
cur, _ := strconv.Atoi(t)
144+
if pre >= cur {
142145
return false
143146
}
144-
curr = x
147+
pre = cur
145148
}
146149
}
147150
return true
148151
}
149152
```
150153

154+
### **TypeScript**
155+
156+
```ts
157+
function areNumbersAscending(s: string): boolean {
158+
let strs = s.split(' ');
159+
let prev = Number.MIN_SAFE_INTEGER;
160+
for (let str of strs) {
161+
let num = Number(str);
162+
if (!isNaN(num)) {
163+
if (num <= prev) return false;
164+
prev = num;
165+
}
166+
}
167+
return true;
168+
}
169+
```
170+
151171
### **...**
152172

153173
```

solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/Solution.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
class Solution {
22
public:
33
bool areNumbersAscending(string s) {
4-
int curr = 0;
4+
int pre = 0;
55
istringstream is(s);
66
string t;
77
while (is >> t) {
88
if (isdigit(t[0])) {
9-
int x = stoi(t);
10-
if (curr >= x) return false;
11-
curr = x;
9+
int cur = stoi(t);
10+
if (pre >= cur) {
11+
return false;
12+
}
13+
pre = cur;
1214
}
1315
}
1416
return true;

solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/Solution.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
func areNumbersAscending(s string) bool {
2-
curr := 0
2+
pre := 0
33
for _, t := range strings.Split(s, " ") {
4-
if unicode.IsDigit(rune(t[0])) {
5-
x, _ := strconv.Atoi(t)
6-
if curr >= x {
4+
if t[0] <= '9' {
5+
cur, _ := strconv.Atoi(t)
6+
if pre >= cur {
77
return false
88
}
9-
curr = x
9+
pre = cur
1010
}
1111
}
1212
return true

solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/Solution.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
class Solution {
22
public boolean areNumbersAscending(String s) {
3-
int curr = 0;
4-
for (String t : s.split(" ")) {
5-
char c = t.charAt(0);
6-
if (Character.isDigit(c)) {
7-
int x = Integer.parseInt(t);
8-
if (curr >= x) {
3+
int pre = 0;
4+
for (var t : s.split(" ")) {
5+
if (t.charAt(0) <= '9') {
6+
int cur = Integer.parseInt(t);
7+
if (pre >= cur) {
98
return false;
109
}
11-
curr = x;
10+
pre = cur;
1211
}
1312
}
1413
return true;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution:
22
def areNumbersAscending(self, s: str) -> bool:
3-
curr = 0
4-
for t in s.split(' '):
3+
pre = 0
4+
for t in s.split():
55
if t[0].isdigit():
6-
if curr >= int(t):
6+
if (cur := int(t)) <= pre:
77
return False
8-
curr = int(t)
8+
pre = cur
99
return True

0 commit comments

Comments
 (0)