[LeetCode]130. Surrounded Regions
2016-04-13 15:00
387 查看
Problem Description
[]https://leetcode.com/problems/surrounded-regions/]Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.
A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region.
思路
就是围棋啊~~每个子的气是和周围子的气相同的。如果周围有活棋,该子就是活的。。。自从不在自己的IDE测试后,AC率大大下降啊不开心!!!
Code
package q130; public class Solution { public int[][] helper(char[][] board) { int m = board.length; int n = board[0].length; int[][] qi = new int[m + 1][n + 1]; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { qi[i][j] = 0; } } for (int i = 0; i < m; i++) { qi[i][n - 1] = board[i][n - 1] == 'X' ? 0 : 1; qi[i][0] = board[i][0] == 'X' ? 0 : 1; } for (int i = 0; i < n; i++) { qi[m - 1][i] = board[m - 1][i] == 'X' ? 0 : 1; qi[0][i] = board[0][i] == 'X' ? 0 : 1; } System.out.println(); for (int k = 0; k < m; k++) { for (int i = 1; i < m - 1; i++) { for (int j = 1; j < n - 1; j++) { qi[i][j] = (board[i][j] == 'X') ? 0 : qi[i - 1][j] | qi[i][j - 1] | qi[i][j + 1] | qi[i + 1][j]; } } } return qi; } public void solve(char[][] board) { int m, n; if (board == null || board.length == 0 || board[0].length == 0) return; m = board.length; n = board[0].length; int[][] qi = helper(board); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (qi[i][j] == 0) board[i][j] = 'X'; } } } // // public static void main(String[] args) { // Solution s = new Solution(); // char[][] c = { "OXOOOX".toCharArray(), "OOXXXO".toCharArray(), // "XXXXXO".toCharArray(), "OOOOXX".toCharArray(), // "XXOOXO".toCharArray(), "OOXXXX".toCharArray() }; // int[][] p = s.helper(c); // for (int i = 0; i < c.length; i++) { // for (int j = 0; j < c[0].length; j++) { // System.out.print(c[i][j] + " "); // } // System.out.println(); // // } // System.out.println(); // for (int i = 0; i < c.length; i++) { // for (int j = 0; j < c[0].length; j++) { // System.out.print(p[i][j] + " "); // } // System.out.println(); // // } // System.out.println(); // // } }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解