반응형
치킨 쿠폰
class Solution {
public int solution(int chicken) {
int answer = 0;
int remain = 0;
while(chicken > 0){
remain += chicken % 10;
answer += chicken /= 10;
}
int remain2 = 0;
while(remain > 0){
remain2 += remain % 10;
answer += remain /= 10;
}
return answer + remain2 /10;
}
}
이진수 더하기
class Solution {
public String solution(String bin1, String bin2) {
int num1 = switchNum(bin1);
int num2 = switchNum(bin2);
int sum = num1 + num2;
StringBuilder sb = new StringBuilder();
while (sum > 0) {
sb.append(sum % 2);
sum /= 2;
}
if(sb.toString().isEmpty()){
return "0";
}
return sb.reverse().toString();
}
public static int switchNum(String s) {
int num = 0;
int n = 1;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '1') {
num += n;
}
n *= 2;
}
return num;
}
}
A로 B만들기
import java.util.*;
class Solution {
public int solution(String before, String after) {
StringBuilder sb = new StringBuilder();
HashMap<String, Integer> hm = new HashMap<>();
for(int i = 0 ; i< before.length();i++){
String s = before.substring(i,i+1);
if(!hm.containsKey(s)){
hm.put(s,1);
continue;
}
hm.put(s, hm.get(s) + 1);
}
for(int i = 0; i< after.length(); i++){
String s = after.substring(i,i+1);
if(!hm.containsKey(s)){
return 0;
}
hm.put(s, hm.get(s) - 1);
}
for(String s : hm.keySet()){
if(hm.get(s) != 0){
return 0;
}
}
return 1;
}
}
k의 개수
class Solution {
public int solution(int i, int j, int k) {
String strk = k +"";
int answer = 0;
for(int l = i; l <= j; l++){
int leng = (l+"").length();
int len = (l+"").replace(strk,"").length();
if(leng != len){
answer += (leng - len) ;
}
}
return answer;
}
}
728x90
반응형
'CodingTest' 카테고리의 다른 글
코딩 테스트 입문 마무리 (0) | 2025.05.17 |
---|---|
코딩테스트 입문 - Day 25 (0) | 2025.05.17 |
코딩테스트 입문 - Day 23 (0) | 2025.05.11 |
코딩테스트 입문 - Day 22 (0) | 2025.05.11 |
유클리드 호제법 - 최대공약수, 유한소수 판별하기 (0) | 2025.05.06 |