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:

  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     }

原文地址:https://www.cnblogs.com/jinjin-2018/p/9065147.html

时间: 2024-07-31 05:35:06

838. Push Dominoes —— weekly contest 85的相关文章

836. Rectangle Overlap ——weekly contest 85

Rectangle Overlap A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two rectangles overlap if the area of their intersection

Leetcode Weekly Contest 86

Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个由整数组成的 N × N 矩阵,其中有多少个 3 × 3 的 "幻方" 子矩阵?(每个子矩阵都是连续的). 直接模拟即可,本来是签到题,由于粗心,浪费了时间. 1 class Solution { 2 public: 3 int numMagicSquaresInside(vector&l

[Swift]LeetCode838. 推多米诺 | 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 adjac

LeetCode之Weekly Contest 93

第一题:二进制间距 问题: 给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . 在 22 的二进制表示中,有三个 1,组成两对连续的 1 . 第一对连续的 1 中,两个 1 之间的距离为 2 . 第二对连续的 1 中,两个 1 之间的距离为 1 . 答案取两个距离之中最大的,也就是 2 . 示例 2: 输入:5 输出:2 解释: 5 的二进制是 0

LeetCode之Weekly Contest 101

前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不出来的直接放弃. 第一题:问题 问题:900. RLE 迭代器 编写一个遍历游程编码序列的迭代器. 迭代器由 RLEIterator(int[] A) 初始化,其中 A 是某个序列的游程编码.更具体地,对于所有偶数i,A[i] 告诉我们在序列中重复非负整数值 A[i + 1] 的次数. 迭代器支持一

[LeetCode] 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 adjac

841. Keys and Rooms —— weekly contest 86

题目链接:https://leetcode.com/problems/keys-and-rooms/description/ 简单DFS time:9ms 1 class Solution { 2 public: 3 void DFS(int root,vector<int>& visited,vector<vector<int>>& rooms){ 4 visited[root] = 1; 5 for(auto x : rooms[root]){ 6

[Swift Weekly Contest 109]LeetCode934. 最短的桥 | Shortest Bridge

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.) Now, we may change 0s to 1s so as to connect the two islands together to form 1 island. Return the smallest nu

[Swift Weekly Contest 109]LeetCode936. 戳印序列 | Stamping The Sequence

You want to form a target string of lowercase letters. At the beginning, your sequence is target.length '?' marks.  You also have a stamp of lowercase letters. On each turn, you may place the stamp over the sequence, and replace every letter in the s