题目如下:

解题思路:本题题目中有一点要求很关键,“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
};

【leetcode】838. Push Dominoes的更多相关文章

  1. 【LeetCode】838. Push Dominoes 解题报告(Python)

    [LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  3. 【LeetCode】面试题13. 机器人的运动范围

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  4. 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...

  5. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  6. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. 中国MOOC_零基础学Java语言_第3周 循环_2数字特征值

    2 数字特征值(5分) 题目内容: 对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值.对于一个整数,从个位开始对每一位数字编号,个位是1号,十位是2号,以此类推.这个整数在第n位上的数字记作 ...

  2. IntlliJ IDEA 注册码获取或离线破解

    JB 的软件还是挺好用的,建议有钱的话支持正版.. IntelliJ IDEA 有开源版,但是要想玩企业级开发,还是得用收费版. 不管哪种方式,使用前都需要把"0.0.0.0 account ...

  3. [ScreenOS] How to manually generate a new system self-signed certificate to replace the expired system self-signed certificate without resetting the firewall

    SUMMARY: This article provides information on how to manually generate a new system self-signed cert ...

  4. pandas DataFram的insert函数

    原文链接:https://blog.csdn.net/yanwucao/article/details/80211984 DataFrame.insert(loc, column, value, al ...

  5. if——while表达式详解

    ①while循环的表达式是循环进行的条件,用作循环条件的表达式中一般至少包括一个能够改变表达式的变量,这个变量称为循环变量 ②当表达式的值为真(非零)(非空)时,执行循环体:为假(0)时,则循环结束 ...

  6. c++ 十进制、十六进制和BCD的相互转换

    #include <stdio.h> #include <string.h> #include <iostream> using namespace std; // ...

  7. 红帽学习笔记[RHCSA] 第六课[进程、服务相关]

    第六课 进程 进程:已经启动的可执行程序的运行中的实例.每个进程都有自己的地址空间,并占用了一定的系统资源. 如何产生一个进程 执行程序或命令 计划任务 在终端中对进程管理 运行一个前台进程 [roo ...

  8. ContextLoaderListener错误

    在web.xml中添加如下配置 <context-param> <param-name>contextConfigLocation</param-name> < ...

  9. C#WebApi自动生成文档

    1.效果图 2.在webApi项目,打开Nuget,搜索WebApiTestClient,安装WebApiTestClient,注意是给HelpPage的 3.打开引入WebApiTestClient ...

  10. 类———用类定义对象———error:C++表达式必须包含类类型

    //原文参考https://blog.csdn.net/lanchunhui/article/details/52503332 你以为你定义了一个类的对象,其实在编译器看来你是声明了一个函数 clas ...