目录结构
1.题目
在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例:
s = "abaccdeff"
返回 "b"
s = ""
返回 " "
限制:
- 0 <= s 的长度 <= 50000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2.题解
有序Hash
public class Offer50 {
@Test
public void test() {
String s = "abaccdeff";
System.out.println(firstUniqChar(s));
}
public char firstUniqChar(String s) {
Map<Character, Boolean> map = new LinkedHashMap<>();
for (char c : s.toCharArray()) {
map.put(c, !map.containsKey(c));
}
for (char c : map.keySet()) {
if (map.get(c)) {
return c;
}
}
return ' ';
}
}
- 时间复杂度:
- 空间复杂度: