【leetcode】838. Push Dominoes

题目如下:

 

 

解题思路:本题题目中有一点要求很关键,“we will consider that a falling domino expends no additional force to a falling or already fallen domino.”,正好对应题目中的例子2,要好好理解一下。因为输入的最大dominoes是10^5,所以要考虑性能问题。dominoes有三种状态:'R','L','.'。在最终状态里面,R和L是不会变的,只有'.'的状态可能会变成三种状态的任意一种。我的思路是把所有连续的'.'当做一个子序列,然后判断这个子序列左边和右边的dominoes是R还是L,这里分这么几种情况:

a.左右的dominoes方向相同,那么子序列所有的'.'的方向和左右方向相同;

b.左边的dominoes向右,右边的dominoes向左,如下图,那么要考虑子序列长度是奇偶性来决定最中间的'.'的取值。如下图,

c.子序列出现要头尾要单独考虑;

d.左边的dominoes向左,右边的dominoes向右,那么子序列所有的'.'的方向保持不变,还是为'.';

最后,出现一个很奇怪的问题,按照我的思路写出的python代码会TEL,但是js代码确能AC,不知道是什么原因。

代码如下:

Python -> 

class Solution(object):
    def pushDominoes(self, dominoes):
        """
        :type dominoes: str
        :rtype: str
        """
        dl = '#' + dominoes + '#'
        start = end = None
        res = ''
        for i in xrange(len(dl)):
            if dl[i] !=  '.': #first opmitize
                if start != None:
                    end = i - 1
                else:
                    if dl[i] != '#':
                        res += dl[i]
                if start != None and end != None:
                    if dl[start-1] == dl[end+1] and dl[start-1] != '#':
                        res += (end-start+1)*dl[start-1]
                    elif dl[start-1] == 'R' and dl[end+1] == 'L':
                        if (end - start) % 2 != 0:
                            mid = (end - start + 1) / 2
                            res += 'R'*mid
                            res += 'L'*mid
                        else:
                            mid = (end - start + 1) / 2
                            res += 'R' * mid
                            res += '.'
                            res += 'L' * mid
                    elif dl[start-1] == '#' and dl[end+1] == 'L':
                        res += 'L'*(end-start+1)
                    elif dl[end+1] == '#' and dl[start-1] == 'R':
                        res += 'R' * (end - start + 1)
                    else:
                        res += '.' * (end - start + 1)
                    if dl[i] != '#':
                        res += dl[i]
                    start = end = None
            else:
                if start == None:
                    start = i
        return res

js ->

var pushDominoes = function(dominoes) {
    var dl = '#' + dominoes + '#'
    var start = end = undefined
    var res = ''
    for(var i = 0;i < dl.length;i++){
        if(dl[i] != '.'){
            if (start != undefined){
                end = i - 1
            }
            else{
                if (dl[i] != '#'){
                    res += dl[i]
                }
            }
            if (start != undefined && end != undefined){
                if (dl[start-1] == dl[end+1] && dl[start-1] != '#'){
                    //res += (end-start+1)*dl[start-1]
                    res += dl[start-1].repeat(end-start+1)
                }
                else if (dl[start-1] == 'R' && dl[end+1] == 'L'){
                    if ((end - start) % 2 != 0){
                        mid = (end - start + 1) / 2
                        //res += 'R'*mid
                        //res += 'L'*mid

                        res += 'R'.repeat(mid)
                        res += 'L'.repeat(mid)
                    }
                    else{
                        mid = (end - start + 1) / 2
                        //res += 'R' * mid
                        //res += 'L' * mid

                        res += 'R'.repeat(mid)
                        res += '.'
                        res += 'L'.repeat(mid)
                    }
                }
                else if(dl[start-1] == '#' && dl[end+1] == 'L'){
                    //res += 'L'*(end-start+1)
                    res += 'L'.repeat(end-start+1)
                }
                else if(dl[end+1] == '#' && dl[start-1] == 'R'){
                    //res += 'R' * (end - start + 1)
                    res += 'R'.repeat(end-start+1)
                }
                else{
                    //res += '.' * (end - start + 1)
                    res += '.'.repeat(end-start+1)
                }
                if (dl[i] != '#'){
                    res += dl[i]
                }
                start = end = undefined
            }
        }
        else{
            if (start == undefined){
                start = i
            }
        }
    }
    return res
};

 

posted @ 2018-05-22 11:19  seyjs  阅读(1162)  评论(0编辑  收藏  举报