Skip to content

Commit 301ee7d

Browse files
committed
二刷387
1 parent abce2ec commit 301ee7d

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

docs/0387-first-unique-character-in-a-string.adoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,38 @@ return 2.
2222

2323
*Note:* You may assume the string contain only lowercase letters.
2424

25+
== 思路分析
2526

27+
通过解法是再次遍历字符串,思考如何遍历字母表来获取第一个唯一字符的位置。
2628

2729
[[src-0387]]
30+
[tabs]
31+
====
32+
一刷::
33+
+
34+
--
2835
[{java_src_attr}]
2936
----
3037
include::{sourcedir}/_0387_FirstUniqueCharacterInAString.java[tag=answer]
3138
----
39+
--
40+
41+
二刷::
42+
+
43+
--
44+
[{java_src_attr}]
45+
----
46+
include::{sourcedir}/_0387_FirstUniqueCharacterInAString_2.java[tag=answer]
47+
----
48+
--
49+
====
50+
51+
== 参考资料
52+
53+
. https://leetcode.cn/problems/first-unique-character-in-a-string/solutions/2361521/387-zi-fu-chuan-zhong-de-di-yi-ge-wei-yi-c5md/?envType=study-plan-v2&envId=selected-coding-interview[387. 字符串中的第一个唯一字符 - 哈希表,清晰图解^]
54+
. https://leetcode.cn/problems/first-unique-character-in-a-string/solutions/531740/zi-fu-chuan-zhong-de-di-yi-ge-wei-yi-zi-x9rok/?envType=study-plan-v2&envId=selected-coding-interview[387. 字符串中的第一个唯一字符 - 官方题解^]
55+
56+
57+
58+
3259

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@
545545
|{doc_base_url}/0155-min-stack.adoc[题解]
546546
|对,这也是单调栈。
547547

548+
|{counter:codes}
549+
|{leetcode_base_url}/first-unique-character-in-a-string/[387. First Unique Character in a String^]
550+
|{doc_base_url}/0387-first-unique-character-in-a-string.adoc[题解]
551+
|哈希+字符串
552+
548553
|===
549554

550555
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0387_FirstUniqueCharacterInAString.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class _0387_FirstUniqueCharacterInAString {
3535
* Memory Usage: 37.4 MB, less than 100.00% of Java online submissions for First Unique Character in a String.
3636
*
3737
* Copy from: https://leetcode.com/problems/first-unique-character-in-a-string/solution/[First Unique Character in a String solution - LeetCode]
38+
*
39+
* @author D瓜哥 · https://www.diguage.com
40+
* @since 2020-01-11 10:48
3841
*/
3942
public int firstUniqChar(String s) {
4043
if (Objects.isNull(s) || s.length() == 0) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class _0387_FirstUniqueCharacterInAString_2 {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2020-01-11 10:48
11+
*/
12+
public int firstUniqChar(String s) {
13+
int[] cnt = new int[26];
14+
for (int i = 0; i < s.length(); i++) {
15+
int idx = s.charAt(i) - 'a';
16+
cnt[idx]++;
17+
}
18+
for (int i = 0; i < s.length(); i++) {
19+
int idx = s.charAt(i) - 'a';
20+
if (cnt[idx] == 1) {
21+
return i;
22+
}
23+
}
24+
return -1;
25+
}
26+
// end::answer[]
27+
}

0 commit comments

Comments
 (0)