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:

  1. 0 <= N <= 10^5
  2. String dominoes contains 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的更多相关文章

  1. 【LeetCode】838. Push Dominoes 解题报告(Python)

    [LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. 838. Push Dominoes

    There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...

  3. LeetCode 838. Push Dominoes

    原题链接在这里:https://leetcode.com/problems/push-dominoes/ 题目: There are N dominoes in a line, and we plac ...

  4. 【leetcode】838. Push Dominoes

    题目如下: 解题思路:本题题目中有一点要求很关键,“we will consider that a falling domino expends no additional force to a fa ...

  5. 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 ...

  6. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  7. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  8. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  9. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

随机推荐

  1. C++赋值兼容原则理解

    –赋值兼容原则(派生类对象是基类对象,反之不成立)–基类指针强制转换成派生类指针–派生类中重定义基类成员(同名覆盖) 假设, 一个基类 "普通人", 一个派生类 "超人& ...

  2. STM32之旅6——WWDG

    WWDG是stm32f103的窗口看门狗,使用的时钟是APB1的时钟,在使用wwdg是被一个小问题困扰了很久--没有打开中断,无法喂狗,一直复位. 初始化完之后需要使能中断: __HAL_WWDG_E ...

  3. LiteOS-任务篇-源码分析-删除任务函数

    目录 前言 笔录草稿 源码分析 LOS_TaskDelete函数源码分析 完整源码 参考 链接 前言 20201009 LiteOS 2018 需要会通用链表 笔录草稿 源码分析 LOS_TaskDe ...

  4. shell-变量的数值运算符-计算双括号(())的使用

    1. 变量的数值计算 变量的数值计算常见的如下几个命令: (()).let.expr.bc.$[]  1) (())用法:(此法很常用,且效率高) 执行简单的整数运算,只需将特定的算术表达式用 &qu ...

  5. mysql物理优化器代价模型分析【原创】

    1. 引言 mysql的sql server在根据where condition检索数据的时候,一般会有多种数据检索的方法,其会根据各种数据检索方法代价的大小,选择代价最小的那个数据检索方法. 比如说 ...

  6. 教你两步快速使用华为HMS沙盒(沙箱)测试

    沙盒(沙箱)测试允许在开发者在接入华为应用内支付IAP联调过程中无需真实支付即可完成端到端的测试. 第一步:添加测试账号 在AppGallery Connect中的"用户与访问"添 ...

  7. 多测师_python基本介绍001

    python 一.python的介绍 python 是一门面向对象,解释型,动态类型语言 面向对象:在python中 一切皆为对象 解释型语言:边解释,边执行, 动态类型:就是检查是在运行才做的. 动 ...

  8. MeteoInfoLab脚本示例:格点数据散点图

    绘制格点数据的散点图,用scaterm函数. 脚本程序: f = addfile('D:/Temp/GrADS/model.ctl') ps = f['PS'][0,(10,60),(60,140)] ...

  9. Python错误:AssertionError: group argument must be None for now

    运行多线程出现的错误 调试了很久,最后发先 __init__ 写错了,修改后后,运行正确.

  10. STM32时钟和定时器

    时钟源 STM32包含了5个时钟源,分别为HSI.HSE.LSI.LSE.PLL. HSI是高速内部时钟.RC振荡器,频率为8MHz: HSE是高速外部时钟,即晶振,可接石英/陶瓷谐振器或接外部时钟源 ...