838. Push Dominoes
There are
N
dominoes in a line, and we place each domino vertically upright.In the beginning, we simultaneously push some of the dominoes either to the left or to the right.
After each second, each domino that is falling to the left pushes the adjacent domino on the left.
Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
Given a string "S" representing the initial state.
S[i] = 'L'
, if the i-th domino has been pushed to the left;S[i] = 'R'
, if the i-th domino has been pushed to the right;S[i] = '.'
, if thei
-th domino has not been pushed.Return a string representing the final state.
Example 1:
Input: ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."Example 2:
Input: "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.
Note:
0 <= N <= 10^5
- String
dominoes
contains only'L
','R'
and'.'
Approach #1: Two pointer. [C++]
class Solution {
public:
string pushDominoes(string dominoes) {
dominoes = 'L' + dominoes + 'R';
int len = dominoes.length();
string res = ""; for (int i = 0, j = 1; j < len; ++j) {
if (dominoes[j] == '.') continue;
int mid = j - i - 1;
if (i > 0) res += dominoes[i];
if (dominoes[i] == dominoes[j])
res += string(mid, dominoes[i]);
else if (dominoes[i] == 'L' && dominoes[j] == 'R')
res += string(mid, '.');
else
res += string(mid/2, 'R') + string(mid%2, '.') + string(mid/2, 'L');
i = j;
} return res;
}
};
Analysis:
we define two pointers which represent the last and the current dominoes we pushed. There are four situations:
1. L.....L: all dominoes will be pushed to left.
2. R....R: all dominoes will be pushed to right.
3. L....R: all dominoes won't be pushed to right or left.
4. R....L: the left half dominoes will be pushed to right and the right half will pushed to left. if the number of dominoes is odd, the middle position will be a '.'.
Reference:
https://leetcode.com/problems/push-dominoes/discuss/132332/C%2B%2BJavaPython-Two-Pointers
838. Push Dominoes的更多相关文章
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 838. Push Dominoes —— weekly contest 85
Push Dominoes There are N dominoes in a line, and we place each domino vertically upright. In the be ...
- LeetCode 838. Push Dominoes
原题链接在这里:https://leetcode.com/problems/push-dominoes/ 题目: There are N dominoes in a line, and we plac ...
- 【leetcode】838. Push Dominoes
题目如下: 解题思路:本题题目中有一点要求很关键,“we will consider that a falling domino expends no additional force to a fa ...
- [LeetCode] Push Dominoes 推多米诺骨牌
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- [Swift]LeetCode838. 推多米诺 | Push Dominoes
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- 算法与数据结构基础 - 双指针(Two Pointers)
双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- linux用户和组
1.用户隶属于用户组的. 2.用户与用户组配置文件 1)用户组配置文件 /etc/group 第一列:用户组的组名 第二列:组密码(真正的密码存储在了gshadow中) 第三列:用户组组ID,用户组唯 ...
- CreateThread
CreateThread(NULL,0,ReportResultThread,this,0,&g_dwThreadId) 2. 参数说明: 第一个参数 lpThreadAttributes 表 ...
- Jmeter通过BeanShell Sampler获取Jmeter的Bin路径,并存入变量供后面的脚本调用
Jmeter的Bin路径是其运行路径,当把自动化测试的脚本放在Bin目录下时,为了将存储CSV的数据文件以及脚本的路径都设置成相对路径,我们需要获取到Jmeter的运行路径: 通过BeanShell ...
- Java 8 接口中的默认方法与静态方法
Java 8 接口中的默认方法与静态方法 1. 接口中的默认方法 允许接口中包含具有具体实现的方法,该方法称"默认方法",默认方法使用用 default 关键字修饰. public ...
- ubuntu and centos各种上网代理设置
1.Ubuntu 一. apt-get 代理设置 由于公司通过代理上网,firefox的代理设置很容易就搞定了,但是通过apt-get安装软件还是不行,于是,查阅了很多资料,最多的方法就是网上流传的三 ...
- MySQL相关知识总结
1. 显示所有表 show tables; 还有information_schema数据库里面有tables表,记录了所有表信息 use information_schema; select * fr ...
- part1:15-安装Linux系统到开发板
1.Qtopia简介 Qtopia是Trolltech公司为采用嵌入式Linux操作系统的消费电子设备而开发的综合应用平台,Qtopia包含完整的应用层.灵活的界面用户.窗口操作系统.应用程序启动程序 ...
- socketpair初识
#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/typ ...
- Linux shell脚本的字符串截取
http://blog.csdn.net/gumanren/article/details/5601544 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.ha ...
- mysql 查询表 的所有字段名称
select COLUMN_NAME from information_schema.COLUMNS where table_name = 'your_table_name' and table_sc ...