【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 ...
随机推荐
- ES6标准入门 第五章:数值的扩展
1.二进制和八进制数值表示法 二进制前缀:0b(或0B): 八进制前缀:0o(或0O). 注意:ES5的严格模式 和 ES6中不再允许使用 0 表示八进制. 将二进制和八进制数值转换为十进制数值 ...
- Matlab——数值计算——单个代数方程 代数方程组
方程求解 求解单个代数方程 MATLAB具有求解符号表达式的工具,如果表达式不是一个方程式(不含等 号),则在求解之前函数solve将表达式置成等于0. >> syms a syms b ...
- 【MM系列】SAP MM物料账在制品承担差异功能及配置
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM物料账在制品承担差异功能 ...
- Maximum Depth of Binary Tree(二叉树最大深度)
来源:https://leetcode.com/problems/maximum-depth-of-binary-tree Given a binary tree, find its maximum ...
- 虚拟机上首次安装Ubuntu后 root密码设置
虚拟机下安装ubuntu后root密码设置 问题描述: 在虚拟机下安装了ubuntu中要输入用户名,一般情况下大家都会输入一个自己的网名或绰号之类的,密码也在这时设置过了. 但是当安装成功之后,使用命 ...
- 在SSIS包中使用 Checkpoint从失败处重新启动包[转]
使用SSIS做ETL的过程中会遇到各种各样的错误,对于一些大数据量的Job失败以后我们不希望重新运行,因为重新运行的时间开销是非常大的,我们只希望从失败的部分开始运行,这样可以省去很多的时间. SSI ...
- Nginx 1.相关介绍
转 https://www.cnblogs.com/wcwnina/p/8728391.html Nginx的产生 没有听过Nginx?那么一定听过它的"同行"Apache吧!Ng ...
- The order of a Tree
The order of a Tree Problem Description As we know,the shape of a binary search tree is greatly rela ...
- ARM编程模式和7钟工作模式
一. ARM的基本设定 1.1. ARM 采用的是32位架构 1.2. ARM约定: a. Byte : 8 bits b. Halfword :16 bits (2 byte) c. Word : ...
- C++ cin相关函数总结
输入原理: 程序的输入都建有一个缓冲区,即输入缓冲区.一次输入过程是这样的,当一次键盘输入结束时会将输入的数据存入输入缓冲区,而cin函数直接从输入缓冲区中取数据.正因为cin函数是直接从缓冲区取数据 ...