[简单] 387. 字符串中的第一个唯一字符

https://leetcode-cn.com/problems/first-unique-character-in-a-string/

 

 一直都写着没有灵魂的代码

class Solution {
    public int firstUniqChar(String s) {
        if(s.length() == 1) {
            return 0;
        }

        int ret = -1;
        Set<String> st = new HashSet<>();
        for (int i = 0; i < s.length(); i++) {
            String ch = String.valueOf(s.charAt(i));
            if(!st.contains(ch)) {
                System.out.println("check : " + ch);
                int a = s.indexOf(ch, i + 1);
                if(a == -1 || (i == s.length() - 1)) {
                    ret = i;
                    break;
                }
                st.add(ch);
            }
        }
        st.clear();
        return ret;
    }
}
View Code

 

posted @ 2022-04-04 19:23  v大喵  阅读(24)  评论(0编辑  收藏  举报