Update 字符串相关.md

master
Omooo 4 years ago
parent 28595c88ea
commit 678f51b262
  1. 40
      blogs/Algorithm/剑指 Offer/字符串相关.md

@ -24,3 +24,43 @@ class Solution {
} }
``` ```
[50. 第一个只出现一次的字符](https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/)
```java
class Solution {
public char firstUniqChar(String s) {
if (s.length() == 0) {
return ' ';
}
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (char c : s.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
return entry.getKey();
}
}
return ' ';
}
}
```
```java
class Solution {
public char firstUniqChar(String s) {
char[] array = s.toCharArray();
int[] ints = new int[256];
for (char c : array) {
ints[c]++;
}
for (char c : array) {
if (ints[c] == 1) {
return c;
}
}
return ' ';
}
}
```

Loading…
Cancel
Save