Skip to content

Commit 5d85103

Browse files
committed
feat: update solutions to lc problems: No.2108~2110
* No.2108.Find First Palindromic String in the Array * No.2109.Adding Spaces to a String * No.2110.Number of Smooth Descent Periods of a Stock
1 parent aca34c1 commit 5d85103

File tree

17 files changed

+270
-224
lines changed

17 files changed

+270
-224
lines changed

solution/2100-2199/2108.Find First Palindromic String in the Array/README.md

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:模拟**
52+
53+
遍历数组 `words`,对于每个字符串 `w`,判断其是否为回文字符串,如果是,则返回 `w`,否则继续遍历。
54+
55+
判断一个字符串是否为回文字符串,可以使用双指针,分别指向字符串的首尾,向中间移动,判断对应的字符是否相等。如果遍历完整个字符串,都没有发现不相等的字符,则该字符串为回文字符串。
56+
57+
时间复杂度 $O(L)$,空间复杂度 $O(1)$,其中 $L$ 为数组 `words` 中所有字符串的长度之和。
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**
@@ -57,19 +65,7 @@
5765
```python
5866
class Solution:
5967
def firstPalindrome(self, words: List[str]) -> str:
60-
def check(s):
61-
i, j = 0, len(s) - 1
62-
while i < j:
63-
if s[i] != s[j]:
64-
return False
65-
i += 1
66-
j -= 1
67-
return True
68-
69-
for word in words:
70-
if check(word):
71-
return word
72-
return ''
68+
return next((w for w in words if w == w[::-1]), "")
7369
```
7470

7571
### **Java**
@@ -79,21 +75,18 @@ class Solution:
7975
```java
8076
class Solution {
8177
public String firstPalindrome(String[] words) {
82-
for (String word : words) {
83-
if (check(word)) {
84-
return word;
78+
for (var w : words) {
79+
boolean ok = true;
80+
for (int i = 0, j = w.length() - 1; i < j && ok; ++i, --j) {
81+
if (w.charAt(i) != w.charAt(j)) {
82+
ok = false;
83+
}
8584
}
86-
}
87-
return "";
88-
}
89-
90-
private boolean check(String s) {
91-
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
92-
if (s.charAt(i) != s.charAt(j)) {
93-
return false;
85+
if (ok) {
86+
return w;
9487
}
9588
}
96-
return true;
89+
return "";
9790
}
9891
}
9992
```
@@ -104,35 +97,35 @@ class Solution {
10497
class Solution {
10598
public:
10699
string firstPalindrome(vector<string>& words) {
107-
for (auto& word : words)
108-
if (check(word)) return word;
100+
for (auto& w : words) {
101+
bool ok = true;
102+
for (int i = 0, j = w.size() - 1; i < j; ++i, --j) {
103+
if (w[i] != w[j]) {
104+
ok = false;
105+
}
106+
}
107+
if (ok) {
108+
return w;
109+
}
110+
}
109111
return "";
110112
}
111-
112-
bool check(string s) {
113-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
114-
if (s[i] != s[j]) return false;
115-
return true;
116-
}
117113
};
118114
```
119115
120116
### **Go**
121117
122118
```go
123119
func firstPalindrome(words []string) string {
124-
check := func(s string) bool {
125-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
126-
if s[i] != s[j] {
127-
return false
120+
for _, w := range words {
121+
ok := true
122+
for i, j := 0, len(w)-1; i < j && ok; i, j = i+1, j-1 {
123+
if w[i] != w[j] {
124+
ok = false
128125
}
129126
}
130-
return true
131-
}
132-
133-
for _, word := range words {
134-
if check(word) {
135-
return word
127+
if ok {
128+
return w
136129
}
137130
}
138131
return ""

solution/2100-2199/2108.Find First Palindromic String in the Array/README_EN.md

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -52,41 +52,26 @@ Note that &quot;racecar&quot; is also palindromic, but it is not the first.
5252
```python
5353
class Solution:
5454
def firstPalindrome(self, words: List[str]) -> str:
55-
def check(s):
56-
i, j = 0, len(s) - 1
57-
while i < j:
58-
if s[i] != s[j]:
59-
return False
60-
i += 1
61-
j -= 1
62-
return True
63-
64-
for word in words:
65-
if check(word):
66-
return word
67-
return ''
55+
return next((w for w in words if w == w[::-1]), "")
6856
```
6957

7058
### **Java**
7159

7260
```java
7361
class Solution {
7462
public String firstPalindrome(String[] words) {
75-
for (String word : words) {
76-
if (check(word)) {
77-
return word;
63+
for (var w : words) {
64+
boolean ok = true;
65+
for (int i = 0, j = w.length() - 1; i < j && ok; ++i, --j) {
66+
if (w.charAt(i) != w.charAt(j)) {
67+
ok = false;
68+
}
7869
}
79-
}
80-
return "";
81-
}
82-
83-
private boolean check(String s) {
84-
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
85-
if (s.charAt(i) != s.charAt(j)) {
86-
return false;
70+
if (ok) {
71+
return w;
8772
}
8873
}
89-
return true;
74+
return "";
9075
}
9176
}
9277
```
@@ -97,35 +82,35 @@ class Solution {
9782
class Solution {
9883
public:
9984
string firstPalindrome(vector<string>& words) {
100-
for (auto& word : words)
101-
if (check(word)) return word;
85+
for (auto& w : words) {
86+
bool ok = true;
87+
for (int i = 0, j = w.size() - 1; i < j; ++i, --j) {
88+
if (w[i] != w[j]) {
89+
ok = false;
90+
}
91+
}
92+
if (ok) {
93+
return w;
94+
}
95+
}
10296
return "";
10397
}
104-
105-
bool check(string s) {
106-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
107-
if (s[i] != s[j]) return false;
108-
return true;
109-
}
11098
};
11199
```
112100
113101
### **Go**
114102
115103
```go
116104
func firstPalindrome(words []string) string {
117-
check := func(s string) bool {
118-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
119-
if s[i] != s[j] {
120-
return false
105+
for _, w := range words {
106+
ok := true
107+
for i, j := 0, len(w)-1; i < j && ok; i, j = i+1, j-1 {
108+
if w[i] != w[j] {
109+
ok = false
121110
}
122111
}
123-
return true
124-
}
125-
126-
for _, word := range words {
127-
if check(word) {
128-
return word
112+
if ok {
113+
return w
129114
}
130115
}
131116
return ""
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
class Solution {
22
public:
33
string firstPalindrome(vector<string>& words) {
4-
for (auto& word : words)
5-
if (check(word)) return word;
4+
for (auto& w : words) {
5+
bool ok = true;
6+
for (int i = 0, j = w.size() - 1; i < j; ++i, --j) {
7+
if (w[i] != w[j]) {
8+
ok = false;
9+
}
10+
}
11+
if (ok) {
12+
return w;
13+
}
14+
}
615
return "";
716
}
8-
9-
bool check(string s) {
10-
for (int i = 0, j = s.size() - 1; i < j; ++i, --j)
11-
if (s[i] != s[j]) return false;
12-
return true;
13-
}
1417
};

solution/2100-2199/2108.Find First Palindromic String in the Array/Solution.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
func firstPalindrome(words []string) string {
2-
check := func(s string) bool {
3-
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
4-
if s[i] != s[j] {
5-
return false
2+
for _, w := range words {
3+
ok := true
4+
for i, j := 0, len(w)-1; i < j && ok; i, j = i+1, j-1 {
5+
if w[i] != w[j] {
6+
ok = false
67
}
78
}
8-
return true
9-
}
10-
11-
for _, word := range words {
12-
if check(word) {
13-
return word
9+
if ok {
10+
return w
1411
}
1512
}
1613
return ""
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
class Solution {
22
public String firstPalindrome(String[] words) {
3-
for (String word : words) {
4-
if (check(word)) {
5-
return word;
3+
for (var w : words) {
4+
boolean ok = true;
5+
for (int i = 0, j = w.length() - 1; i < j && ok; ++i, --j) {
6+
if (w.charAt(i) != w.charAt(j)) {
7+
ok = false;
8+
}
69
}
7-
}
8-
return "";
9-
}
10-
11-
private boolean check(String s) {
12-
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
13-
if (s.charAt(i) != s.charAt(j)) {
14-
return false;
10+
if (ok) {
11+
return w;
1512
}
1613
}
17-
return true;
14+
return "";
1815
}
1916
}
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
11
class Solution:
22
def firstPalindrome(self, words: List[str]) -> str:
3-
def check(s):
4-
i, j = 0, len(s) - 1
5-
while i < j:
6-
if s[i] != s[j]:
7-
return False
8-
i += 1
9-
j -= 1
10-
return True
11-
12-
for word in words:
13-
if check(word):
14-
return word
15-
return ''
3+
return next((w for w in words if w == w[::-1]), "")

solution/2100-2199/2109.Adding Spaces to a String/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
**方法一:双指针**
67+
68+
我们可以用双指针 $i$ 和 $j$ 分别指向字符串 $s$ 和数组 $spaces$ 的头部,然后从头到尾遍历字符串 $s$,当 $i$ 等于 $spaces[j]$ 时,我们往结果字符串中添加一个空格,然后 $j$ 自增 1。接下来,我们将 $s[i]$ 添加到结果字符串中,然后 $i$ 自增 1。继续这个过程,直到遍历完字符串 $s$。
69+
70+
时间复杂度 $O(n + m)$,其中 $n$ 和 $m$ 分别是字符串 $s$ 和数组 $spaces$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
71+
6672
<!-- tabs:start -->
6773

6874
### **Python3**
@@ -82,6 +88,20 @@ class Solution:
8288
return ''.join(ans)
8389
```
8490

91+
```python
92+
class Solution:
93+
def addSpaces(self, s: str, spaces: List[int]) -> str:
94+
ans = []
95+
i, j = len(s) - 1, len(spaces) - 1
96+
while i >= 0:
97+
ans.append(s[i])
98+
if j >= 0 and i == spaces[j]:
99+
ans.append(' ')
100+
j -= 1
101+
i -= 1
102+
return ''.join(ans[::-1])
103+
```
104+
85105
### **Java**
86106

87107
<!-- 这里可写当前语言的特殊实现逻辑 -->

solution/2100-2199/2109.Adding Spaces to a String/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ class Solution:
7272
return ''.join(ans)
7373
```
7474

75+
```python
76+
class Solution:
77+
def addSpaces(self, s: str, spaces: List[int]) -> str:
78+
ans = []
79+
i, j = len(s) - 1, len(spaces) - 1
80+
while i >= 0:
81+
ans.append(s[i])
82+
if j >= 0 and i == spaces[j]:
83+
ans.append(' ')
84+
j -= 1
85+
i -= 1
86+
return ''.join(ans[::-1])
87+
```
88+
7589
### **Java**
7690

7791
```java

0 commit comments

Comments
 (0)