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. 深入解读 ASP.NET Core 身份认证过程

    长话短说:上文我们讲了 ASP.NET Core 基于声明的访问控制到底是什么鬼? 今天我们乘胜追击:聊一聊ASP.NET Core 中的身份验证. 身份验证是确定用户身份的过程. 授权是确定用户是否 ...

  2. 题目:写出一条SQL语句,查询工资高于10000,且与他所在部门的经理年龄相同的职工姓名。

    create table Emp( eid char(20) primary key, ename char(20), age integer check (age > 0), did char ...

  3. 085 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 02 构造方法介绍 04 构造方法调用

    085 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 02 构造方法介绍 04 构造方法调用 本文知识点:构造方法调用 说明:因为时间紧张,本人写博客过程中只是 ...

  4. __declspec(dllexport)和__declspec(dllimport) (——declspec方法创建dll的方法已验证ok)

    转载:https://www.cnblogs.com/chengbing2011/p/4084125.html __declspec(dllimport)和__declspec(dllexport)经 ...

  5. 《C++primerplus》第9章练习题

    1.(未使用原书例题)练习多文件组织.在一个头文件中定义一种学生的结构体,存储姓名和年龄,声明三个函数分别用于询问有多少个学生,输入学生的信息和展示学生的信息.在另一个源文件中给出所有函数的定义.在主 ...

  6. P1297 单选错位 题解

    这是一道我们的考试题 前置芝士 期望 定义:试验中每次可能结果的概率乘以其结果的总和(来自百度某科 滑稽) 性质:\(E(ax+by)\) = \(xE(a)\) * \(yE(b)\) 计算式: \ ...

  7. JavaScript筛选数组

    要求: 从一个数组中,筛选出符合条件的元素,放到新数组中. 有一数组[1, 19, 2, 8, 9, 15, 11, 7, 6, 4, 18, 10],将超过10的元素删除. 代码实现: var ar ...

  8. 达梦数据库_DM8配置实时主备

    1.环境说明 准备三台机器DM_HD1.DM_HD11.DM_SH,DM_HD1和DM_HD11用来部署主备库,DM_SH用来部署确认监视器.其中DM_HD1和DM_HD11配置两块网卡,一块接入内部 ...

  9. 推荐算法之: DeepFM及使用DeepCTR测试

    算法介绍 左边deep network,右边FM,所以叫deepFM 包含两个部分: Part1: FM(Factorization machines),因子分解机部分 在传统的一阶线性回归之上,加了 ...

  10. rabbitmq 交换机模式 -主题模式 topic

    建立一个交换机 tpc 并且绑定了各自的路由到 Q1 Q2 <?php require_once "./vendor/autoload.php"; use PhpAmqpLi ...