Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Counting
- Tree
- sorting
- two pointers
- Stack
- bit manipulation
- SQL
- greedy
- 파이썬
- 구현
- 자바
- Matrix
- 코딩테스트
- 코테
- implement
- array
- string
- database
- java
- hash table
- Number Theory
- simulation
- Class
- Data Structure
- dynamic programming
- Method
- Binary Search
- geometry
- Math
- Binary Tree
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 387. First Unique Character in a String 본문
1. Input
1) String s
2. Output
1) s의 문자 중 반복되지 않는 첫번째 문자열의 인덱스를 반환
2) 만약 그런 문자가 없다면 -1을 반환
3. Constraint
1) 1 <= s.length <= 10^5
2) s는 영어 소문자로 이루어져 있다.
4. Example
Input: s = "leetcode" -> Output: 0
Input: s = "aabb" -> Output: -1
설명:
- 반복되지 않는 문자는 l, t, c, o, d이며, 이 중 첫번째로 나오는 문자는 l이므로 l의 인덱스인 0을 반환한다.
- 반복되지 않는 문자가 없으므로 -1을 반환한다.
5. Code
1) 첫 코드(2023/05/18)
class Solution {
public int firstUniqChar(String s) {
int ans = -1;
HashMap<Character,Integer> m = new HashMap<Character,Integer>();
for(int i=0 ; i<s.length() ; i++)
m.put(s.charAt(i), m.getOrDefault(s.charAt(i),0)+1);
for(int i=0 ; i<s.length() && ans==-1 ; i++)
if(m.get(s.charAt(i))==1)
ans = i;
return ans;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 303. Range Sum Query - Immutable (0) | 2023.05.18 |
---|---|
[LeetCode/Easy] 389. Find the Difference (0) | 2023.05.18 |
[LeetCode/Easy] 349. Intersection of Two Arrays (0) | 2023.05.18 |
[백준 온라인 저지] 11653. 소인수분해 (0) | 2023.05.16 |
[LeetCode/Easy] 206. Reverse Linked List (0) | 2023.05.15 |