문제 설명
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
나의 풀이
class Solution {
public int solution(String[][] board, int h, int w) {
int answer = 0;
final int n = board.length;
final int m = board[0].length;
if(0 <= h - 1){
if(board[h - 1][w].equals(board[h][w])){
answer++;
}
}
if(h + 1 < n){
if(board[h + 1][w].equals(board[h][w])){
answer++;
}
}
if(0 <= w - 1){
if(board[h][w - 1].equals(board[h][w])){
answer++;
}
}
if(w + 1 < m){
if(board[h][w + 1].equals(board[h][w])){
answer++;
}
}
return answer;
}
}
다른 풀이
class Solution {
public int solution(String[][] board, int h, int w) {
int n = board.length;
int count = 0;
int[] dh = { 0, 1, -1, 0}; int[] dw = {1,0,0,-1};
for(int i = 0; i < 4; i++){
int h_check = h + dh[i];
int w_check = w + dw[i];
if(h_check >= 0 && h_check < n && w_check >= 0 && w_check < n){
if(board[h][w].equals(board[h_check][w_check])){
count++;
}
}
}
return count;
}
}
'👨💻 Coding Test > Programers' 카테고리의 다른 글
[Programmers/Java/Lv.1/시뮬레이션] 81.가장 많이 받은 선물 (1) | 2025.07.24 |
---|---|
[Programmers/Java/Lv.1/시뮬레이션] 80.[PCCP 기출문제] 1번 / 붕대 감기 (0) | 2025.07.23 |
[Programmers/Java/Lv.1/정렬] 78.[PCCE 기출문제] 10번 / 데이터 분석 (1) | 2025.07.23 |
[Programmers/Java/Lv.1/문자열] 77.달리기 경주 (0) | 2025.07.22 |
[Programmers/Java/Lv.1/문자열] 76.추억 점수 (0) | 2025.07.22 |