题目如下:

解题思路:本题题目中有一点要求很关键,“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. What is the difference between a URI, a URL and a URN?

    资料: URI: URL: URN:

  2. 【Appium遇到的坑】环境配置无误,路径无中文,无空格,提示error: Logcat capture failed: spawn ENOENT

    代码如下,提示error: Logcat capture failed: spawn ENOENT from appium import webdriver from time import slee ...

  3. paramiko远程连接linux服务器进行上传下载文件

    花了不少时间来研究paramiko中sftpclient的文件传输,一顿操作猛如虎,最后就一直卡在了路径报错问题,疯狂查阅资料借鉴大佬们的心得,还是搞不好,睡了个午觉醒来,仔细一看原来是指定路径的文件 ...

  4. 应用安全 - Java Web 应用 - Confluence - 漏洞汇总

    CVE-2019-3395 Date: -- 类型: SSRF 影响范围: Confluence 1.*.*.*.*.3.*.*.4.*.*.5.*.* Confluence 6.0.*.1.*.6. ...

  5. 【监控笔记】【1.1】监控事件系列——SQL Server Profiler

    声明:本系列是书目<sql server监控与诊断读书笔记> 联机丛书监控:https://docs.microsoft.com/en-us/sql/relational-database ...

  6. springboot无法找到mapper😵

    今天在学习springboot的过程中遇到mapper无法找到的问题,困扰了很久

  7. Scrapy 教程(五)-分页策略

    scrapy 爬取分页网站的策略 1. 检测当前页是否存在“下一页” 2. 如果存在,把“下一页”的链接交给本方法或者其他方法 3. 如果不存在,结束 图示 示例代码 def parse(self, ...

  8. dsu on tree 与长链剖分

    dsu on tree 对于树进行轻重链剖分,对于节点 $x$ ,递归所有轻儿子后消除其影响,递归重儿子,不消除其影响. 然后对于所有轻儿子的子树暴力,从而得到 $x$ 的答案. 对于要消除暴力消除即 ...

  9. Vue 变量,成员,属性监听

    Vue变量 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...

  10. vue.js-vuex深入浅出

    1:正确的创建目录 2:action.js 异步请求数据 3:index.js 文件声名 4:mutation.js 同步事务 修改state中的值 5:state.js 申明和保存变量或对象 6:如 ...