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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 750A New Year and Hurry
A. New Year and Hurry time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 766A Mahmoud and Longest Uncommon Subsequence
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- svn.SvnX
1. 使用SvnX的入门 http://www.divvun.no/doc/tools/docu-svn-user-svnx.html 2. SvnX的代码 https://code.google.c ...
- This page contains the following error
解决办法:将header头注释掉 header("content-type:text/xml; charset=UTF-8");
- Debian use sudo
刚安装好的Debian默认还没有sudo功能.1.安装sudo# apt-get install sudo2.编辑 /etc/sudoers ,添加如下行# visudoroot ALL=(ALL:A ...
- maven使用感受
第一次接触的时候,什么都不懂,感觉好复杂. 后来系统地看了一个使用教程: 简单评价一下: 自动帮我们下载jar架包,还有就是可以执行命令自己部署到远程服务器上面去. 缺点: 学习成本.一般人不了解.第 ...
- 如何使用css来让图片居中不变形 微信小程序和web端适用
图片变形很多人祭奠出了妖魔鬼怪般的各种大法,比如使用jq来写,或者使用css表达式来写.今天我总结的是使用css3来写,唯一最大缺点就是对一些浏览器版本不够兼容.下面就是关于如何使用css来让图片居中 ...
- 20155333 2016-2017-2 《Java程序设计》第八周学习总结
20155333 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 认识NIO NIO(New IO)-from JDK1.4 Channel: 衔接数据节点( ...
- hdu-1698(线段树,区间修改)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 注意:用位运算会更快,不然超时. #include<iostream> #inclu ...
- vs2010点调试,显示系统找不到指定的文件
首先,查看“项目”-“属性”-“链接器”-“常规”-“输出文件”,路劲是否是“bin/xxx.exe”,如果是请继续看我的解答,否则请忽略下面的内容.原因是用VS2010加载调试以前的VC6.0下的程 ...
