在字符串 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

 

 

//时间复杂度:O(n)  遍历s的长度两次
    // 空间复杂度:O(n) 有个map结构
    public char firstUniqChar(String s) {
        Map<Character,Boolean> map = new HashMap<Character,Boolean>();
        for(Character charStr : s.toCharArray()){
            map.put(charStr,map.containsKey(charStr));
        }
        for(Character charStr : s.toCharArray()){
            if(!map.get(charStr)){
                return charStr;
            }
        }
        return ' ';
    }

 

相关文章:

  • 2022-01-02
  • 2021-07-12
  • 2021-12-16
  • 2021-08-13
  • 2021-06-13
  • 2022-02-02
  • 2022-12-23
  • 2021-09-20
猜你喜欢
  • 2021-08-06
  • 2022-01-05
  • 2021-07-04
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案