본문 바로가기
문제풀이

Leetcode First Unique Character in a String JAVA

by AndoneKwon 2020. 12. 25.
class Solution {
    public int firstUniqChar(String s) {
        char[] allChar = s.toCharArray();
        Map<Character,Integer> map = new HashMap<>();
        char uniqueChar;
        int answer=-1;
        if(allChar.length==1){
            return 0;
        }
        for(int i=0;i<allChar.length;i++){
            uniqueChar=allChar[i];
            answer = i;
            if(map.get(uniqueChar)!=null){
                answer = -1;
                continue;
            }
            for(int j=i+1;j<allChar.length;j++){
                if(uniqueChar==allChar[j]){
                    map.put(uniqueChar,1);
                    answer=-1;
                    break;
                }
            }
            if(answer!=-1){
                break;
            }
        }
        return answer;
    }
}

'문제풀이' 카테고리의 다른 글

Leetcode Intersection of Two Arrays II JAVA  (0) 2020.12.28
LeetCode Missing Number JAVA  (0) 2020.12.26
Leetcode Pascal's Triangle II JAVA  (0) 2020.12.24
Leetcode Merge Two Sorted Lists  (0) 2020.12.23
백준 2042 구간합 JAVA  (0) 2020.12.22