zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    解决ie下vue列表数据不能即时刷新的问题
    redis的几个知识点
    oracle查找某个字符在字符串中的个数的技巧
    程序员必看书籍
    eclipse彻底去除validation(彻底解决编辑js文件的卡顿问题)
    eclipse强行停止buliding workspace
    [Ljava.lang.String是一个字符串数组的字节码表示
    javascript合并两个数组
    css隐藏页面元素的方法
    配置IIS使用Python
  • 原文地址:https://www.cnblogs.com/jinjin-2018/p/9065147.html
Copyright © 2011-2022 走看看