zoukankan      html  css  js  c++  java
  • 959. Regions Cut By Slashes

    题目来源:959. Regions Cut By Slashes  

    https://leetcode.com/problems/regions-cut-by-slashes/

     自我感觉难度/真实难度:             写题时间时长:

     题意:

     分析:思路就是把每一个符号位置,扩展成3*3的小格子,然后把自己的边界设为1就可以了,最后使用数区域的方法就可以了,DFS一下,看有多少区域

     自己的代码:

    import numpy
    class Solution(object):
        def regionsBySlashes(self, g):
            """
            :type grid: List[str]
            :rtype: int
            """
            self.l=len(g)*3
            count=0
            b=numpy.zeros((self.l,self.l))
            for i in range(self.l/3):
                for j in range(self.l/3):
                    if g[i][j] == '/':
                        b[i * 3][j * 3 + 2] = b[i * 3 + 1][j * 3 + 1] = b[i * 3 + 2][j * 3] = 1 
                    if g[i][j] == '\':
                        b[i * 3][j * 3] = b[i * 3 + 1][j * 3 + 1] = b[i * 3 + 2][j * 3 + 2] = 1;
            for i in range(self.l):
                for j in range(self.l):
                    if b[i][j]==0:
                        count+=1
                        self.one(i,j,b)
            return count
        
        def one(self,x,y,a):
            left_x=x-1
            right_x=x+1
            up_y=y-1
            down_y=y+1
            if 0<=x<self.l and 0<=y<self.l and a[x,y]==0:
                a[x][y]=1
                self.one(left_x,y,a)
                self.one(right_x,y,a)
                self.one(x,up_y,a)
                self.one(x,down_y,a)
            else:
                return 
                
            
                

    代码还是要讲求简介

     

    代码效率/结果:

     优秀代码:

    class Solution(object):
        def regionsBySlashes(self, grid):
            """
            :type grid: List[str]
            :rtype: int
            """
            # colors = [down, right]
            N = len(grid)
            mapping = collections.defaultdict(set)
            colors = [[i,0] for i in xrange(N)]
            color = N-1
            for row in grid:
    #            print colors, row
                for i, ch in enumerate(row):
                    if i > 0:
                        right = colors[i-1][1] 
                    else:
                        color += 1
                        right = color
                    down = colors[i][0]
    #                print i, ch, right, down
                    if ch == ' ':
                        if right != down:
                            mapping[right].add(down)
                            mapping[ down].add(right)
                        colors[i] = [down, down]
                    elif ch == '/':
                        if right != down:
                            mapping[right].add(down)
                            mapping[ down].add(right)
                        color += 1
                        colors[i] = [color, color]
                    else:
                        colors[i] = [right, down]       
            
    #        print colors
    
            color += 1
    #        print color, mapping
            visited, count = [False] * color, 0
            for item in xrange(color):
                if visited[item]: continue
                count += 1
                level, visited[item] = [item], True
                while level:
                    co = level.pop()
                    for c in mapping[co]:
                        if not visited[c]:
                            level.append(c)
                            visited[c] = True
            
            return count
                

    44m

    但是目前还没有看懂这么写出来的

    代码效率/结果:

     自己优化后的代码:

     反思改进策略:

  • 相关阅读:
    2016华为实习编程题:停车管理系统
    lintcode:单词切分
    2016网易实习生编程题:数组中两个数的和等于sum
    2016网易实习生编程题:n个骰子的和等于m
    2016百度编程题:钓鱼比赛
    2016百度编程题:裁减网格纸
    2016百度编程题:罪犯转移
    JAVA面试基础
    扔硬币问题
    随机数生成随机数
  • 原文地址:https://www.cnblogs.com/captain-dl/p/10887778.html
Copyright © 2011-2022 走看看