Skip to content

Commit 30a99b9

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0387
1 parent 6b5863c commit 30a99b9

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

solution/0300-0399/0387.First Unique Character in a String/README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,48 @@ s = "loveleetcode",
2525

2626
<!-- 这里可写通用的实现逻辑 -->
2727

28+
遍历字符串,用一个 map 或者字典存放字符串中每个字符出现的次数。然后再次遍历字符串,取出对应字符出现的次数,若次数为 1,直接返回当前字符串的下标。遍历结束,返回 -1。
29+
2830
<!-- tabs:start -->
2931

3032
### **Python3**
3133

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

3436
```python
35-
37+
class Solution:
38+
def firstUniqChar(self, s: str) -> int:
39+
chars = {}
40+
for ch in s:
41+
ch = ord(ch)
42+
chars[ch] = chars.get(ch, 0) + 1
43+
for i, ch in enumerate(s):
44+
ch = ord(ch)
45+
if chars[ch] == 1:
46+
return i
47+
return -1
3648
```
3749

3850
### **Java**
3951

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

4254
```java
43-
55+
class Solution {
56+
public int firstUniqChar(String s) {
57+
Map<Character, Integer> chars = new HashMap<>(26);
58+
int n = s.length();
59+
for (int i = 0; i < n; ++i) {
60+
char ch = s.charAt(i);
61+
chars.put(ch, chars.getOrDefault(ch, 0) + 1);
62+
}
63+
for (int i = 0; i < n; ++i) {
64+
char ch = s.charAt(i);
65+
if (chars.get(ch) == 1) return i;
66+
}
67+
return -1;
68+
}
69+
}
4470
```
4571

4672
### **...**

solution/0300-0399/0387.First Unique Character in a String/README_EN.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,37 @@ return 2.
4141
### **Python3**
4242

4343
```python
44-
44+
class Solution:
45+
def firstUniqChar(self, s: str) -> int:
46+
chars = {}
47+
for ch in s:
48+
ch = ord(ch)
49+
chars[ch] = chars.get(ch, 0) + 1
50+
for i, ch in enumerate(s):
51+
ch = ord(ch)
52+
if chars[ch] == 1:
53+
return i
54+
return -1
4555
```
4656

4757
### **Java**
4858

4959
```java
50-
60+
class Solution {
61+
public int firstUniqChar(String s) {
62+
Map<Character, Integer> chars = new HashMap<>(26);
63+
int n = s.length();
64+
for (int i = 0; i < n; ++i) {
65+
char ch = s.charAt(i);
66+
chars.put(ch, chars.getOrDefault(ch, 0) + 1);
67+
}
68+
for (int i = 0; i < n; ++i) {
69+
char ch = s.charAt(i);
70+
if (chars.get(ch) == 1) return i;
71+
}
72+
return -1;
73+
}
74+
}
5175
```
5276

5377
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int firstUniqChar(String s) {
3+
Map<Character, Integer> chars = new HashMap<>(26);
4+
int n = s.length();
5+
for (int i = 0; i < n; ++i) {
6+
char ch = s.charAt(i);
7+
chars.put(ch, chars.getOrDefault(ch, 0) + 1);
8+
}
9+
for (int i = 0; i < n; ++i) {
10+
char ch = s.charAt(i);
11+
if (chars.get(ch) == 1) return i;
12+
}
13+
return -1;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def firstUniqChar(self, s: str) -> int:
3+
chars = {}
4+
for ch in s:
5+
ch = ord(ch)
6+
chars[ch] = chars.get(ch, 0) + 1
7+
for i, ch in enumerate(s):
8+
ch = ord(ch)
9+
if chars[ch] == 1:
10+
return i
11+
return -1

0 commit comments

Comments
 (0)