Java - HashMap 초깃값을 갖도록 초기화 하는 방법
10개이하
Map<String, Object> map = Map.of(
"name", "junho85",
"nickname", "June Kim"
);
10개이상
Map<String, Object> test = Map.ofEntries(
Map.entry("name", "Kim"),
Map.entry("nickname", "June Kim"),
Map.entry("a", "June Kim"),
Map.entry("b", "June Kim"),
Map.entry("c", "June Kim"),
Map.entry("d", "June Kim"),
Map.entry("e", "June Kim"),
Map.entry("f", "June Kim"),
Map.entry("g", "June Kim"),
Map.entry("h", "June Kim"),
Map.entry("i", "June Kim"),
Map.entry("j", "June Kim")
);
알고리즘 문제풀이
개미군단
class Solution {
public int solution(int hp) {
int answer = (hp/5)+((hp%5)/3)+((hp%5)%3);
return answer;
}
}
모스부호(1)
import java.util.Map;
class Solution {
public String solution(String letter) {
String answer ="";
Map<String,String> morse = Map.ofEntries(
Map.entry(".-","a"), Map.entry("-...","b"), Map.entry("-.-.","c"),
Map.entry("-..","d"), Map.entry(".","e"), Map.entry("..-.","f"),
Map.entry("--.","g"), Map.entry("....","h"), Map.entry("..","i"),
Map.entry(".---","j"), Map.entry("-.-","k"), Map.entry(".-..","l"),
Map.entry("--","m"), Map.entry("-.","n"), Map.entry("---","o"),
Map.entry(".--.","p"), Map.entry("--.-","q"), Map.entry(".-.","r"),
Map.entry("...","s"), Map.entry("-","t"), Map.entry("..-","u"),
Map.entry("...-","v"), Map.entry(".--","w"), Map.entry("-..-","x"),
Map.entry("-.--","y"), Map.entry("--..","z")
);
String[] letters = letter.split(" ");
for(String str : letters){
answer += morse.get(str);
}
return answer;
}
}
가위바위보
import java.util.Map;
class Solution {
public String solution(String rsp) {
String[] rsps = rsp.split("");
String answer ="";
Map<String,String> win = Map.ofEntries(
Map.entry("0","5"), Map.entry("2","0"), Map.entry("5","2")
);
for(String str : rsps){
answer += win.get(str);
}
return answer;
}
}
구술을나누는경우의수
import java.math.BigDecimal;
class Solution {
public int solution(int balls, int share) {
BigDecimal ball = new BigDecimal(factorial(balls));
BigDecimal ball_share = new BigDecimal(factorial(balls - share));
BigDecimal shar = new BigDecimal(factorial(share));
int answer = (ball.divide(ball_share)).divide(shar).intValue(); return answer;
}
private static String factorial(int num){
BigDecimal result = new BigDecimal("1");
for(int i = num; i>0;i--){
BigDecimal number = new BigDecimal(i);
result = result.multiply(number);
}
return result.toString();
}
}
class Solution {
public long solution(int balls, int share) {
share = Math.min(balls - share, share);
if (share == 0)
return 1;
long result = solution(balls - 1, share - 1);
result *= balls;
result /= share;
return result;
}
}