메모리: 75.2 MB, 시간: 11.50 ms
코딩테스트 연습 > 연습문제
class Solution {
public String solution(String[] seoul) {
String answer = "";
for(int i = 0; i < seoul.length; i++) {
if(seoul[i].equals("Kim")) {
answer = "김서방은 " + i + "에 있다";
break;
}
}
return answer;
}
}
메모리: 78.2 MB, 시간: 0.12 ms - Answer Code1(23.01.10) 메모리: 81.7 MB, 시간: 0.14 ms - Answer Code2(23.01.20)
코딩테스트 연습 > 연습문제
class Solution {
public long solution(int a, int b) {
long answer = 0;
if(a < b) {
for(int i = a; i <= b; i++) {
answer += i;
}
} else if(a > b){
for(int i = b; i <= a; i++) {
answer += i;
}
} else {
answer = a;
}
return answer;
}
}
class Solution {
public long solution(int a, int b) {
long answer = 0;
if(a!=b){
for(int i=Math.min(a,b);i<=Math.max(a,b);i++){
answer+=i;
}
}else{
answer=a;
}
return answer;
}
}
메모리: 72.8 MB, 시간: 0.33 ms
코딩테스트 연습 > 연습문제
import java.util.*;
class Divisible {
public int[] divisible(int[] arr, int divisor) {
int cnt = 0;
int num = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] % divisor == 0) {
cnt++;
}
}
if(cnt == 0) {
int[] answer = {-1};
return answer;
}
int[] answer = new int[cnt];
for(int i = 0; i < arr.length; i++) {
if(arr[i] % divisor == 0) {
answer[num] = arr[i];
num++;
}
}
Arrays.sort(answer);
return answer;
}
}
메모리: 96 MB, 시간: 8.00 ms - Answer Code1
메모리: 78.1 MB, 시간: 0.04 ms - Answer Code2
코딩테스트 연습 > 연습문제
class Solution {
public long[] solution(int x, int n) {
long[] answer = new long[n];
long ans = x;
for(int i = 0; i< n; i++) {
answer[i] = ans;
ans += x;
}
for(int i = 0; i < n; i++) {
System.out.print(answer[i]);
}
return answer;
}
}
import java.util.*;
class Solution {
public static long[] solution(int x, int n) {
long[] answer = new long[n];
answer[0] = x;
for (int i = 1; i < n; i++) {
answer[i] = answer[i - 1] + x;
}
return answer;
}
}
메모리: 82.8 MB, 시간: 0.02 ms - Answer Code1
메모리: 75.4 MB, 시간: 0.13 ms - Answer Code2
코딩테스트 연습 > 연습문제
import java.util.*;
class Solution {
public boolean solution(int x) {
boolean answer = true;
int sum = 0;
int number = x;
while(number > 0) {
int div = number % 10;
sum += div;
number /= 10;
}
if(x % sum == 0) {
answer = true;
} else {
answer = false;
}
return answer;
}
}
class Solution {
public boolean solution(int num) {
String[] temp = String.valueOf(num).split("");
int sum = 0;
for (String s : temp) {
sum += Integer.parseInt(s);
}
if (num % sum == 0) {
return true;
} else {
return false;
}
}
}