[PS] 99ํด๋ฝ ์ฝํ ์คํฐ๋ 14์ผ์ฐจ TIL (์ซ์์นด๋2)
ํ๊ทธ: 99ํด๋ฝ, PS, TIL, ์ด๋ถํ์, ์ฝ๋ฉํ ์คํธ์ค๋น, ํญํด99
์นดํ ๊ณ ๋ฆฌ: PS
๋ฌธ์
ํ์ด
HashMap
์ getOrDefault()
ํจ์๋ฅผ ์ด์ฉํด ๊ฐ ๋ฒํธ๊ฐ ๋ช๋ฒ์ฉ ๋ฑ์ฅํ๋์ง ์ ์ฅํ ํ ์
๋ ฅ๋๋ ์ซ์๊ฐ ํด์๋งต์ ๋ช๊ฐ ์๋์ง ๋ฐํํ๋ค.
1
2
// 6 3 2 10 10 10 -10 -10 7 3 ์
๋ ฅ์ด ์ฃผ์ด์ง๋
map = {2=1, 3=2, 6=1, 7=1, -10=2, 10=3}
ํด์๋งต์ key ๊ฐ์ผ๋ก ํด๋น ์ซ์๋ฅผ ์ ์ฅํ๊ณ , value ๊ฐ์ผ๋ก ๋ฑ์ฅ ํ์๋ฅผ ์ ์ฅํ๋ค.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class Main {
static FastReader scan = new FastReader();
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
input();
}
static void input() {
int N = scan.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < N; i++) {
int key = scan.nextInt();
map.put(key, map.getOrDefault(key, 0) + 1);
}
int M = scan.nextInt();
for (int i = 0; i < M; i++) {
sb.append(map.getOrDefault(scan.nextInt(), 0)).append(" ");
}
System.out.println(sb);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
๋๊ธ๋จ๊ธฐ๊ธฐ