838. Push Dominoes
There are
Ndominoes 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
dominoescontains 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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 测试rar/bz2/tar.gz/gz压缩文档完整性
#gz文件gzip -t *.gz#bz2文件tar jtvf archive.tar.bz2#tar.gz文件tar jtvf archive.tar.gz#rar文件unrar t 1.rar
- 品味性能之道<六>:图形化SQL分析工具
在上一章里,重点分享了命令行SQL分析工具的使用方法.在本章将重点分享PL/SQL的SQL分析工具. 一.如何打开PL/SQL执行计划 开启PL/SQL这工具,推荐如下方法: 点击 ...
- 2018.10.15 bzoj3564: [SHOI2014]信号增幅仪(坐标处理+最小圆覆盖)
传送门 省选考最小圆覆盖? 亦可赛艇(你们什么都没看见) 在大佬的引领下成功做了出来. 就是旋转坐标使椭圆的横轴跟xxx轴平行. 然后压缩横坐标使得其变成一个圆. 然后跑最小覆盖圆就可以了. 注意题目 ...
- 42 Can stress be Avoided ? 压力能够回避吗 ?
Can stress be Avoided ? 压力能够回避吗 ? ①In the 1960s, medical researchers Thomas Holmes and Richard Rahe ...
- UESTC 486 Good Morning (水题+坑!)
题意:给你一行字符串,让你找其中蕴含的“good morning"的次数. 析:看起来很水么,多简单,只有统计一下其中字母的出现的次数,然后除以相应的个数. 但是很不幸的是WA,而且是在te ...
- 关于FIR的modelsim
(1)FIR ip核仿真 (2)FIR 多通道应用 (3)多通道fir ip核需要注意的复位问题 =================================================== ...
- Android draw Rect 坐标图示
前两天在博客发了在例子 android Canvas类介绍 http://byandby.javaeye.com/blog/825330 建议大家 点进去 看一看 不然下边没办法 继续啊. 我还是把这 ...
- @media screen
参考地址: http://www.swordair.com/blog/2010/08/431/ http://ashaochangfu.blog.163.com/blog/static/1042517 ...
- (线段树模板)A Simple Problem with Integers --POJ--3468
链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...
- Hdu1361&&Poj1068 Parencodings 2017-01-18 17:17 45人阅读 评论(0) 收藏
Parencodings Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
