[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํํ
1. ๋ฌธ์ : Link
ํํ์ ๋ฐ๋ณตํด์ ๋ง๋ ๋ฌธ์์ด์ input์ผ๋ก ์ฃผ๊ณ
ํด๋น ๋ฌธ์์ด์ ํด์ํด์ ๋ค์ ํํ๋ก returnํด์ผํ๋ ๋ฌธ์
2. ํ์ด
๋ฌธ์์ด์ ๋ด๊ธด ์ซ์๋ค์ ๋น๋์๋ฅผ ํ์ธํด ๋น๋์๋๋ก ์ ๋ ฌํ์ฌ key๊ฐ ๋ฐฐ์ด์ ๋ฃ์ด์ return
3. ์ฝ๋
from collections import Counter
def solution(s):
answer = []
tmp = ""
num = []
for i in range(len(s)-2):
if s[i].isdigit():
tmp += s[i]
if s[i+1] == ',' and s[i+2].isdigit() or s[i+1] == '}':
num.append(int(tmp))
tmp = ""
for d, c in Counter(num).most_common():
answer.append(d)
return answer
import java.util.*;
class Solution {
public int[] solution(String s) {
String newString = s.replaceAll("[\\{\\}]", "");
String newString2 = newString.replaceAll("[,]", " ");
String strArr[] = newString.split(",");
HashMap<String, Integer> hmap = new HashMap<String, Integer>();
for(int i=0; i<strArr.length; i++){
String tempStr = strArr[i];
hmap.put(tempStr, hmap.getOrDefault(tempStr, 0)+1);
}
// Map.Entry ๋ฆฌ์คํธ ์์ฑ
List<Map.Entry<String, Integer>> entList = new ArrayList<Map.Entry<String, Integer>>(hmap.entrySet());
// ๋น๊ตํจ์ Comparator๋ฅผ ์ฌ์ฉํ์ฌ ๋ด๋ฆผ ์ฐจ์์ผ๋ก ์ ๋ ฌ
Collections.sort(entList, new Comparator<Map.Entry<String, Integer>>() {
// compare๋ก ๊ฐ์ ๋น๊ต
public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2)
{
// ๋ด๋ฆผ ์ฐจ์์ผ๋ก ์ ๋ ฌ
return obj2.getValue().compareTo(obj1.getValue());
}
});
int[] answer = new int[entList.size()];
int id = 0;
// ๊ฒฐ๊ณผ
for(Map.Entry<String, Integer> entry : entList) {
answer[id] = Integer.parseInt(entry.getKey());
id++;
}
return answer;
}
}
'๐ป Coding Problems Solving > Array | String | Loop' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ผ๊ฐ ๋ฌํฝ์ด (0) | 2022.04.19 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 2๊ฐ ์ดํ๋ก ๋ค๋ฅธ ๋นํธ (0) | 2022.04.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 H-Index (0) | 2022.04.11 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์์ด ๋๋ง์๊ธฐ (0) | 2022.04.08 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๊ฐ์ฅ ํฐ ์ (0) | 2022.04.07 |
์ต๊ทผ๋๊ธ