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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- git pull和git fetch命令
git pull和git fetch命令 git pull git pull命令的作用是取回远程主机某个分支的更新,在与本地指定分支合并,格式如下: $ git pull <远程主机名>& ...
- .NET获得上传图片宽高
代码: //图片大小限制--------------------------- System.Drawing.Image txtStoryimage = System.Drawing.Image.Fr ...
- hive的用户和用户权限
HiverServer2支持远程多客户端的并发和认证,支持通过JDBC.Beeline等连接操作.hive默认的Derby数据库,由于是内嵌的文件数据库,只支持一个用户的操作访问,支持多用户需用mys ...
- cocos2d接安卓facebook插件(已测cocos-x 3.7 3.8版本)
1 控制台创建新工程: a 控制台 进入cocos2d文件夹下面,如cocos2d-x-3.7.1,执行setup.py,未设置NDK SDK ANT 路径的设置路径,需要改路径的 explore ...
- redmine2.6.5 邮件配置
打开configuration.xml (路径:apps/redmine/htdocs/config/) production: email_delivery: delivery_method: :s ...
- 创建WRAPPER时, SQL20076N 未对指定的操作启用数据库的实例。
您可以通过运行DB2 UPDATE DBM CFG USING FEDERATED YES来设置这个参数.修改这个参数后,必须重新启动实例才会生效(DB2STOP/DB2START).所以你会出现你的 ...
- Ckeditor上传图片返回的JS直接显示出来,未执行!!!
Ckeditor上传图片网上有很多教程. 下面是我今天下午遇到的一个坑...自己挖的坑. 在conotroller里 我开始习惯性的 response.setContentType("app ...
- Dice 7 ==> dice 5
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- 2018.08.17 洛谷[POI2010]GRA-The Minima Game(线性dp)
传送门 短代码神奇dp. 自己yy的思路居然1A了好高兴啊! 不难想到每个人选择的时候一定是取连续的最大的那一段数,自然需要先排序. 然后可以用dp[i]表示当前最大数是a[i]的时候先手可以获得的最 ...
- mysql报错排查总结
mysql报错: [root@zabbix ~]# mysql ERROR 2002 (HY000): Can't connect to local MySQL server through sock ...