838. Push Dominoes —— weekly contest 85
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 the i-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'.'
1 class Solution {
2 public:
3 string pushDominoes(string dominoes) {
4 string res;
5 res = dominoes;
6 int n = dominoes.size();
7 for(int i = 0; i < n; i++){
8 if(dominoes[i] == '.'){
9 char left = '.';
10 char right = '.';
11 int j,k;
12 for(j = i - 1; j >= 0;j--){
13 if(dominoes[j]!='.'){
14 left = dominoes[j];
15 break;
16 }
17 }
18 for(k = i + 1; k < n; k++){
19 if(dominoes[k]!='.'){
20 right = dominoes[k];
21 break;
22 }
23 }
24 if(left=='R'&&right=='L'){
25 if(i-j!=k-i){
26 res[i] = i-j < k-i?left:right;
27 }
28 }else if(left == 'R'){
29 res[i] = left;
30 }else if(right == 'L'){
31 res[i] = right;
32 }
33
34 }
35 }
36 return res;
37 }
38 };
Another easier solution:
1 string pushDominoes(string d) {
2 d = 'L' + d + 'R';
3 string res = "";
4 for (int i = 0, j = 1; j < d.length(); ++j) {
5 if (d[j] == '.') continue;
6 int middle = j - i - 1;
7 if (i > 0) res += d[i];
8 if (d[i] == d[j]) res += string(middle, d[i]);
9 else if (d[i] == 'L' && d[j] == 'R') res += string(middle, '.');
10 else res += string(middle / 2, 'R') + string(middle % 2,'.') + string(middle / 2, 'L');
11 i = j;
12 }
13 return res;
14 }
838. Push Dominoes —— weekly contest 85的更多相关文章
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 838. Push Dominoes
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- 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 ...
- 836. Rectangle Overlap ——weekly contest 85
Rectangle Overlap A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coor ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
随机推荐
- GUI应用编程初体验
不同平台的GUI实现原理是一样的. 本实验基于 windos平台. 先捋一捋概念 什么是消息队列(Message Queue)假 设一个场景:系统正在处理WM_PAINT消息,就在这时用户在键盘上敲击 ...
- three.js学习5_渲染器
THREE.WebGLRenderer WebGL Render 用WebGL渲染出你精心制作的场景 介绍 在之前的介绍中, 已经介绍过场景, 相机, 光源, 有了这些后, 就可以形成一个可观的三维展 ...
- Java知识系统回顾整理01基础04操作符04位操作符
一.位操作符 位操作符在实际工作中用的并不常见,但是我比较纠结这些位操作.所以实际练习位操作符的每一个操作符的操作实例来理解其具体含义. 建议: 如果确实感兴趣,就看看,个人建议跳过这个章节. 真正工 ...
- I2C总线的Arduino库函数
I2C总线的Arduino库函数 I2C即Inter-Integrated Circuit串行总线的缩写,是PHILIPS公司推出的芯片间串行传输总线.它以1根串行数据线(SDA)和1根串行时钟线(S ...
- MATLAB textread函数
实际应用中经常要读取txt文件,这个时候就需要用到强大的textread函数.它的基本语法是:[A,B,C,...] = textread(filename,format)[A,B,C,...] = ...
- The comparison between object and constructor
1.相似的地方 1.举个栗子:public struct Student{ string name; int age;}public class bike{ int weight; ...
- 2020年java全套教程,此套java涵盖了pdf,java源码,项目案例,完整视频约3000G的资源
疫情期间,百无聊赖,是不是需要充电一下,让自己更有竞争力呢?学习java一定要快呦! 废话不多说了,网盘已经爆炸了,把2006年-2020年的全部资料都发给爱学习的你吧, 希望可以改变你的命运,或者是 ...
- 用Pycharm创建指定的Django版本
最近在学习胡阳老师(the5fire)的<Django企业级开发实战>,想要使用pycharm创建django项目时,在使用virtualenv创建虚拟环境后,在pycharm内,无论如何 ...
- day29 Pyhton 面向对象 多态 封装
# coding:utf-8 # py2中的经典类 # class D:#没有继承object是经典类# pass # # def func(self): # # print('d') # class ...
- 【UR #13】Yist
UOJ小清新题表 题目摘要 UOJ链接 给出一个排列 \(A\) 以及它的一个非空子序列 \(B\),给出一个 \(x\) 并进行若干次操作,每一次操作需要在 \(A\) 中选择一个长度恰好为 \(x ...