
프로그래머스 코딩테스트 입문 Java로 Day 21 문자열 사칙연산 시뮬레이션 2차원배열 수학 배열 풀기
문자열 my_string이 매개변수로 주어집니다. my_string은 소문자, 대문자, 자연수로만 구성되어있습니다. my_string안의 자연수들의 합을 return하도록 solution 함수를 완성해주세요.
제한사항
my_string의 길이 ≤ 1,000my_string 안의 자연수 ≤ 1000입출력 예
| my_string | result |
|---|---|
| "aAb1B2cC34oOp" | 37 |
| "1a2b3c4d123Z" | 133 |
class Solution {
public int solution(String my_string) {
int answer = 0, sum = 0, str_len = my_string.length();
for (int i = 0; i < str_len; i++) {
int ch = (int)my_string.charAt(i) - 48;
if (ch >= 0 && ch <= 9) {
if (str_len - 1 != i) {
int ch_1 = (int)my_string.charAt(i+1) - 48;
if (ch_1 >= 0 && ch_1 <= 9) {
sum *= 10;
sum += ch;
}
else {
answer += sum * 10 + ch;
sum = 0;
}
} else answer += sum * 10 + ch;
}
}
return answer;
}
}
다음 그림과 같이 지뢰가 있는 지역과 지뢰에 인접한 위, 아래, 좌, 우 대각선 칸을 모두 위험지역으로 분류합니다.

지뢰는 2차원 배열 board에 1로 표시되어 있고 board에는 지뢰가 매설 된 지역 1과, 지뢰가 없는 지역 0만 존재합니다.
지뢰가 매설된 지역의 지도 board가 매개변수로 주어질 때, 안전한 지역의 칸 수를 return하도록 solution 함수를 완성해주세요.
제한사항
board는 n * n 배열입니다.board에는 지뢰가 있는 지역 1과 지뢰가 없는 지역 0만 존재합니다.입출력 예
| board | result |
|---|---|
| [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]] | 16 |
| [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0]] | 13 |
| [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]] | 0 |
class Solution {
public int solution(int[][] board) {
int answer = 0;
int[][] result = new int[board.length][board[0].length];
x: for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 1) {
result[i][j] = 1;
int row_end = board.length - 1;
int col_end = board[0].length - 1;
if (row_end == 0 && col_end == 0) break x;
if (i == 0 && j == 0) { // 0, 0
result[i+1][j] = 1;
result[i][j+1] = 1;
result[i+1][j+1] = 1;
} else if (i == row_end && j == 0) { // 끝, 0
result[i-1][j] = 1;
result[i][j+1] = 1;
result[i-1][j+1] = 1;
} else if (i == row_end && j == col_end) { // 끝, 끝
result[i-1][j] = 1;
result[i][j-1] = 1;
result[i-1][j-1] = 1;
} else if (i == 0 && j == col_end) { // 0, 끝
result[i][j-1] = 1;
result[i+1][j] = 1;
result[i+1][j-1] = 1;
} else if (i == 0 && j != 0 && j != col_end) { // 가로만 0
result[i][j-1] = 1;
result[i][j+1] = 1;
result[i+1][j-1] = 1;
result[i+1][j] = 1;
result[i+1][j+1] = 1;
} else if (i == row_end && j != 0 && j != col_end) { // 가로만 끝
result[i][j-1] = 1;
result[i][j+1] = 1;
result[i-1][j-1] = 1;
result[i-1][j] = 1;
result[i-1][j+1] = 1;
} else if (i != 0 && i != row_end && j == 0) { // 세로만 0
result[i-1][j] = 1;
result[i+1][j] = 1;
result[i-1][j+1] = 1;
result[i][j+1] = 1;
result[i+1][j+1] = 1;
} else if (i != 0 && i != row_end && j == col_end) { // 세로만 끝
result[i+1][j] = 1;
result[i-1][j] = 1;
result[i+1][j-1] = 1;
result[i][j-1] = 1;
result[i-1][j-1] = 1;
} else {
result[i+1][j] = 1;
result[i-1][j] = 1;
result[i+1][j-1] = 1;
result[i][j-1] = 1;
result[i-1][j-1] = 1;
result[i+1][j+1] = 1;
result[i][j+1] = 1;
result[i-1][j+1] = 1;
}
}
}
}
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
if (result[i][j] == 0) answer++;
}
}
return answer;
}
}
선분 세 개로 삼각형을 만들기 위해서는 다음과 같은 조건을 만족해야 합니다.
삼각형의 두 변의 길이가 담긴 배열 sides이 매개변수로 주어집니다. 나머지 한 변이 될 수 있는 정수의 개수를 return하도록 solution 함수를 완성해주세요.
제한사항
sides의 원소는 자연수입니다.sides의 길이는 2입니다.sides의 원소 ≤ 1,000입출력 예
| sides | result |
|---|---|
| [1, 2] | 1 |
| [3, 6] | 5 |
| [11, 7] | 13 |
import java.util.*;
class Solution {
public int solution(int[] sides) {
int answer = 0;
int max = Math.max(sides[0], sides[1]);
int min = Math.min(sides[0], sides[1]);
int lowLimit = max - min;
int highLimit = max + min;
answer = highLimit - lowLimit - 1;
return answer;
}
}
import java.util.*;
class Solution {
public int solution(int[] sides) {
int answer = 0;
int max = Math.max(sides[0], sides[1]);
int min = Math.min(sides[0], sides[1]);
for (int i = 1; i <= 2001; i++) {
if (i > max) {
if (min + max > i) answer++;
} else if (min + i > max) answer++;
}
return answer;
}
}
원래는 아래 방법으로 했는데 i > max 조건을 안넣어서 위 방법으로 했음. 참고 링크
[위 방법]
lowLimit와 highLimit
lowLimit: max - min
lowLimit은 max가 세 변 중 가장 길 때의 조건을 이용함.
EX) max = 11, min = 7
구하고자 하는 i와 min을 더한 값이 11보다 커야 하며 i는 max를 넘으면 안됨.
5, 6, 7, 8, 9, 10, 11
이 때 max - min은 4가 됨.
highLimit: max + min
highLimit은 찾고자하는 변이 max보다 길 때의 조건을 이용함.
EX) max = 11, min = 7
구하고자 하는 i는 11보다 커야함. + 11 + 7(18)보다는 작아야 함.
그러면 i의 값은 12, 13, 14, 15, 16, 17이 됨.
이 때 max + min은 18이 됨.
→ 구하고자 하는 값을 보면 5 ~ 17까지의 정수를 더하는 것인데 이는 highLimit - LowLimit - 1로 해결 가능함.
PROGRAMMERS-962 행성에 불시착한 우주비행사 머쓱이는 외계행성의 언어를 공부하려고 합니다. 알파벳이 담긴 배열 spell과 외계어 사전 dic이 매개변수로 주어집니다. spell에 담긴 알파벳을 한번씩만 모두 사용한 단어가 dic에 존재한다면 1, 존재하지 않는다면 2를 return하도록 solution 함수를 완성해주세요.
제한사항
spell과 dic의 원소는 알파벳 소문자로만 이루어져있습니다.spell의 크기 ≤ 10spell의 원소의 길이는 1입니다.dic의 크기 ≤ 10dic의 원소의 길이 ≤ 10spell의 원소를 모두 사용해 단어를 만들어야 합니다.spell의 원소를 모두 사용해 만들 수 있는 단어는 dic에 두 개 이상 존재하지 않습니다.dic과 spell 모두 중복된 원소를 갖지 않습니다.입출력 예
| spell | dic | result |
|---|---|---|
| ["p", "o", "s"] | ["sod", "eocd", "qixm", "adio", "soo"] | 2 |
| ["z", "d", "x"] | ["def", "dww", "dzx", "loveaw"] | 1 |
| ["s", "o", "m", "d"] | ["moos", "dzx", "smm", "sunmmo", "som"] | 2 |
class Solution {
public int solution(String[] spell, String[] dic) {
int answer = 0;
for (String dict : dic) {
int count = 0;
for (String str : spell) {
if (dict.indexOf(str) != -1) {
dict = dict.replaceFirst(str, "");
count++;
}
}
int dic_len = dict.length();
if (dic_len == 0 && count == spell.length) {
answer = 1;
break;
} else answer = 2;
}
return answer;
}
}
1) 조건이 많은 문제는 풀기가 어렵기보다는 길어지다보니 가독성이 떨어지는 듯하다.
2) 기록표
