在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例:
s = “abaccdeff” 返回 “b”
s = “” 返回 " "
import java.util.HashMap; import java.util.Map; /** * @Auther: truedei * @Date: 2020 /20-5-20 08:45 * @Description: */ public class Test { static public char firstUniqChar(String s) { Map<Character,Boolean> dic = new HashMap<>(); char[] array = s.toCharArray(); for (char c : array) { dic.put(c, !dic.containsKey(c)); } for (char c : array) { if (dic.get(c)) return c; } return ' '; } public static void main(String[] args) { System.out.println(firstUniqChar("leetcode")); } }
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句