【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
};
【leetcode】838. Push Dominoes的更多相关文章
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【LeetCode】面试题13. 机器人的运动范围
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】1162. 地图分析 As Far from Land as Possible(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 这个题想考察什么? 剩下的任务就是套模板! 日期 题目 ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_5 Mybatis中使用Dao实现类的执行过程分析-查询方法1
继续运行testFindAll方法.把其他类的断点都删除掉. 只在这里加了断点,所以直接就跳转到这里了.RoutingStatementHandler里面的query方法 继续往下走,断点又回到了这里 ...
- CentOS安Elasticsearch
工作中有需求用到es做数据分析和日志搜索的,整理记录一下安装部署过程.ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful we ...
- cocos2dx基础篇(9) 滑块控件CCControlSlider
[3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)CCControlEvent 改为强枚举 Control::EventType (4)CCControlEvent ...
- 【Linux开发】如何查看Linux kernel的内置模块驱动列表和进程ID
[Linux开发]如何查看Linux kernel的内置模块驱动列表和进程ID 标签:[Linux开发] 命令: cat /lib/modules/$(uname -r)/modules.builti ...
- 关于Logcat
1 android logcat api Log.i(String tag, String msg) info,普通信息 Log.d(String tag, String msg) debug,调试信 ...
- [19/09/19-星期四] Python中的字典和集合
一.字典 # 字典 # 使用 {} 来创建字典 d = {} # 创建了一个空字典 # 创建一个保护有数据的字典 # 语法: # {key:value,key:value,key:value} # 字 ...
- 右键windows terminal here无法进入当前目录
很久没写水笔了,简单记一水 使用windows terminal的基本上都自己改过注册表,添加到右键windows terminal here吧,用着很方便,哪里不会点哪里. 我起初删除掉starti ...
- MyBatis框架 课程笔记
MyBatis框架 课程笔记 第1章 MyBatis简介 1.1 MyBatis历史 1)MyBatis是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Softw ...
- P1233木棍加工
这个题被算法标签标为DP,但其实可能只是用dp求子序列,,(n方) 给出l与w,只要是l与w同时满足小于一个l与w,那么这个木棍不需要时间,反之需要1.看到这个题,首先想到了二维背包,然后发现没有最大 ...
- 嗨翻C语言--这里没有蠢问题(一)
问:card_name[0]是什么意思?答:它是用户输入的第一个字符.如果用户输入了10,那么card_name[0]就将是1.问:总是得用/*和*/写注释吗?答:如果你的编译器支持C99标准,就可以 ...