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 |
Tags
- Method
- database
- java
- 구현
- string
- dynamic programming
- Stack
- Binary Search
- sorting
- 파이썬
- implement
- 코테
- Number Theory
- Class
- Tree
- array
- two pointers
- Binary Tree
- Matrix
- 자바
- bit manipulation
- Counting
- greedy
- Data Structure
- hash table
- geometry
- SQL
- simulation
- 코딩테스트
- Math
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1748. Sum of Unique Elements 본문
1. Input
1) int[] nums
2. Output
1) nums의 요소 중 1개만 있는 요소들의 합을 반환
3. Constraint
1) 1 <= nums.length <= 100
2) 1 <= nums[i] <= 100
4. Example
Input: nums = [1,2,3,2] -> Output: 4
설명: 1번만 나타나는 요소는 [1,3]이므로 합인 4를 반환한다.
5. Code
1) 첫 코드(2023/04/17)
HashMap<Integer,Integer> m = new HashMap<>();
for(int i : nums)
m.put(i, m.getOrDefault(i,0)+1);
int answer = 0;
Iterator it = m.entrySet().iterator();
while(it.hasNext()){
Map.Entry e = (Map.Entry)it.next();
if((int)e.getValue()==1)
answer += (int)e.getKey();
}
return answer;
2) 메모리 사용량을 줄여보고자 시도했던 코드(2023/04/17)
HashMap<Integer,Integer> m = new HashMap<>();
for(int i : nums)
m.put(i, m.getOrDefault(i,0)+1);
int answer = 0;
for(int i=0 ; i<nums.length ; i++)
if(m.containsKey(nums[i]) && m.get(nums[i])==1){
answer += nums[i]; m.remove(nums[i]);
}
return answer;
- 몇 mb 차이 나지 않는데 훨씬 좋아진 걸로 나왔다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 25206. 너의 평점은 (0) | 2023.04.17 |
---|---|
[백준 온라인 저지] 2941. 크로아티아 알파벳 (0) | 2023.04.17 |
[LeetCode/Easy] 1736. Latest Time by Replacing Hidden Digits (0) | 2023.04.17 |
[LeetCode/Easy] 1694. Reformat Phone Number (0) | 2023.04.16 |
[LeetCode/Easy] 1652. Defuse the Bomb (0) | 2023.04.15 |