【leetcode】com/problems/surrounded-regions/

dfs 栈溢出,bfs超时,用dfs非递归就不溢出了,前后写了1一个星期class node
{
    int i;
    int j;

    public node(int i1,int j1)
    {
        i=i1;
        j=j1;

    }
}

public class Solution {
    public void solve(char[][] board) {

        int len1=board.length;
        if(len1<=0)return;
        int len2=board[0].length;
        if(len1<=2||len2<=2) return;
        for(int i=0;i<len1;i++)
        {
            if(board[i][0]==‘O‘) dfs(board,i,0);
            if(board[i][len2-1]==‘O‘) dfs(board,i,len2-1);

        }
        for(int i=0;i<len2;i++)
        {

            if(board[0][i]==‘O‘)dfs(board,0,i);
            if(board[len1-1][i]==‘O‘)dfs(board,len1-1,i);
        }
        for(int i=0;i<len1;i++)
        {
            for(int j=0;j<len2;j++)
            {
                if(board[i][j]==‘h‘) board[i][j]=‘O‘;
                else  board[i][j]=‘X‘;
            }
        }

    }
    public boolean is(int i,int j,int len1,int len2)
    {
        if(i>=0&&i<len1&&j>=0&&j<len2) return true;
        return false;

    }
    public void dfs(char b[][],int i,int j)
    {
        int len1=b.length;
        int len2=b[0].length;
       Stack<node> s=new Stack<node>();
       b[i][j]=‘h‘;
       s.push(new node(i,j));

    }
}

  

【leetcode】com/problems/surrounded-regions/,布布扣,bubuko.com

时间: 2024-08-10 17:17:38

【leetcode】com/problems/surrounded-regions/的相关文章

【leetcode】118. Pascal&#39;s Triangle

@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) /************************ * @description: simple. * @time_complexity: O(n) * @space_complexity: O(n) ****

【Leetcode】Pascal&#39;s Triangle II

题目链接:https://leetcode.com/problems/pascals-triangle-ii/ 题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路: 要求空间复杂度为O

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

【leetcode】657. Robot Return to Origin

Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin/ 1)problem There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0,

【leetcode】893. Groups of Special-Equivalent Strings

Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-of-special-equivalent-strings/ 1)problem You are given an array A of strings. Two strings S and T are special-equivalent if after any number of moves,

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n