JAVA_LeetCode 419_Battleships in a Board 풀이 class Solution { public int countBattleships(char[][] board) { int m = board.length, n = board[0].length, count = 0; // one pass scan for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(board[i][j] != 'X') continue; // 전함이 아닌 경우 스킵 if(i > 0 && board[i - 1][j] == 'X') continue; // 위에 X가 있으면 이미 세어진 전함 if(j > 0 && board[i][j - 1] == 'X') continue; // 왼쪽에 X가 있으면 이미 세어진 전함 count++; // 시작점 체크 } } return count; } } 시작점 체크, 원패스 스캔, O(1) 공간 복잡도를 매칭하는 문제 ...