๋ฐ์ํ
[ํ๋ก๊ทธ๋๋จธ์ค] ์คํ์ฑํ ๋ฐฉ
https://programmers.co.kr/learn/courses/30/lessons/42888
๋ฌธ์ ์ ๊ทผ
HashMap์ ์ด์ฉํด์ ์ ์ ์์ด๋์ ๋๋ค์์ ์ ์ฅํ๊ณ
list์ ์ ์ ์์ด๋์ Enter or Leave๋ฅผ ์ ์ฅํ ํ์ ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ ๋ ์ ์ ์์ด๋์ ๋๋ค์์ ๋งค์นญํด์ ๋ฉ์ธ์ง๋ฅผ ์์ฑํ์๋ค.
Code
import java.util.HashMap;
import java.util.ArrayList;
class Solution {
public String[] solution(String[] records) {
String[] answer;
HashMap<String, String> users = new HashMap<>();
ArrayList<String> messages = new ArrayList<>();
for(String record: records){
//record ์
๋ ฅ
String[] token = record.split(" ");
String command = token[0];
String uid = token[1];
if(command.equals("Enter")){
String nickname = token[2];
users.put(uid,nickname);
messages.add(uid + " Enter");
} else if(command.equals("Change")){
String nickname = token[2];
users.put(uid,nickname);
}
else if(command.equals("Leave")){
messages.add(uid + " Leave");
}
}
answer = new String[messages.size()];
// ๋ฉ์ธ์ง ์ถ๋ ฅ
for(int i = 0; i<messages.size(); i++){
String[] message = messages.get(i).split(" ");
String username = users.get(message[0]);
if(message[1].equals("Enter")){
answer[i] = username + "๋์ด ๋ค์ด์์ต๋๋ค.";
} else {
answer[i] = username + "๋์ด ๋๊ฐ์ต๋๋ค.";
}
}
return answer;
}
}
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Java][๋ฐฑ์ค] 12852๋ฒ 1๋ก ๋ง๋ค๊ธฐ 2 (0) | 2022.05.01 |
---|---|
[Algorithm/Java][ํ๋ก๊ทธ๋๋จธ์ค] ๋จ์ฒด์ฌ์ง ์ฐ๊ธฐ (0) | 2022.05.01 |
[Algorithm/Java][๋ฐฑ์ค] 2263 ํธ๋ฆฌ์ ์ํ (0) | 2022.04.11 |
[Algorithm/Java][ํ๋ก๊ทธ๋๋จธ์ค] ๋ฌธ์์ด ์์ถ (0) | 2022.04.10 |
[Algorithm/Java][๋ฐฑ์ค] 1918 ํ์ ํ๊ธฐ์ (0) | 2022.04.08 |