Skip to content

Commit c82eadc

Browse files
committed
一刷1839(优化)
1 parent a4f1bd8 commit c82eadc

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

docs/1839-longest-substring-of-all-vowels-in-order.adoc

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ include::{sourcedir}/_1839_LongestSubstringOfAllVowelsInOrder.java[tag=answer]
6565
----
6666
--
6767
68+
一刷(优化)::
69+
+
70+
--
71+
[{java_src_attr}]
72+
----
73+
include::{sourcedir}/_1839_LongestSubstringOfAllVowelsInOrder_1.java[tag=answer]
74+
----
75+
--
76+
6877
// 二刷::
6978
// +
7079
// --
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1839_LongestSubstringOfAllVowelsInOrder_1 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-05-14 15:41:55
8+
*/
9+
public int longestBeautifulSubstring(String word) {
10+
// 注意:这里直接 cnt = 1,并且从 r = 1 开始遍历,
11+
// 已经假设 word[0] == a(假设不成立也不影响结果正确)
12+
int l = 0, cnt = 1, result = 0;
13+
for (int r = 1; r < word.length(); r++) {
14+
if (word.charAt(r - 1) > word.charAt(r)) {
15+
l = r;
16+
cnt = 1;
17+
} else if (word.charAt(r - 1) < word.charAt(r)) {
18+
cnt++;
19+
}
20+
if (cnt == 5) {
21+
result = Math.max(result, r - l + 1);
22+
}
23+
}
24+
return result;
25+
}
26+
// end::answer[]
27+
public static void main(String[] args) {
28+
new _1839_LongestSubstringOfAllVowelsInOrder_1()
29+
.longestBeautifulSubstring("aeiou");
30+
}
31+
}

0 commit comments

Comments
 (0)