【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 ...
随机推荐
- ActionList及Action使用
ActionList及Action使用 https://blog.csdn.net/adamrao/article/details/7450889 2012年04月11日 19:09:27 阅读数:1 ...
- TiDB配置HAProxy负载均衡
1.简介 HAProxy是一个C语言编写的免费的负载均衡软件,可以运行于大部分主流的Linux操作系统上. HAProxy提供了L4(TCP)和L7(HTTP)两种负载均衡能力,具备丰富的功能. 2. ...
- MySQL单列索引和组合索引的创建及区别介绍
MySQL单列索引是我们使用MySQL数据库中经常会见到的,MySQL单列索引和组合索引的区别可能有很多人还不是十分的了解,下面就为您分析两者的主要区别,供您参考学习. 为了形象地对比两者,再建一个表 ...
- Java ——集合框架 list lambda set map 遍历方法 数据结构
本节重点思维导图 集合框架 有序无序:元素放入的顺序与取出的顺序是否一致,一致即为有序,不一致即无序. List:允许重复.有序 ArrayList:长度可变的数组,遍历速度快 LinkedList: ...
- Java多线程学习——wait方法(信号灯法/生产者消费者模式)
信号灯法:以一个标志位来判断是否执行还是等待 public class TV { private String voice; //内容 private boolean flag=false; //信号 ...
- 编译中出现的undefined reference to XXX
主要是在链接时发现找不到某个函数的实现文件.可以查找是否在makefile里没有增加待编译的.c文件,或者静态库没有引用
- Java简易实现记事本的打开与保存
记事本的打开与保存 一些总结 * Swing中有时方法不显示,需要把方setVisible(true)放到最后执行 * AWT中的TextArea默认是中间布局 * fileDialog对话框Load ...
- 迁移数据库mysql
文档 <http://site.clairvoyantsoft.com/migrate-cloudera-scm-database-from-postgresql-to-mysql/> h ...
- offsetWidth clientWidth scrollWidth 的区别
了解 offsetWidth clientWidth scrollWidth 的区别 最近需要清除区分开元素的width,height及相应的坐标等,当前这篇用来区分offsetWidth clien ...
- 小程序-调用公共js对象方法/ app.js
在小程序中,如果在子页面想调用共公js的方法,需先在子页面js中先实例化app:具体过程如下 子页面js: 1 2 3 4 5 6 7 8 //调用公共js对象以便调用其方法 var app = ge ...