博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
838. Push Dominoes —— weekly contest 85
阅读量:5031 次
发布时间:2019-06-12

本文共 2989 字,大约阅读时间需要 9 分钟。

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

你可能感兴趣的文章
Leetcode总结之Backtracking
查看>>
Android开发学习之路-图片颜色获取器开发(1)
查看>>
StackExchange.Redis 官方文档(一) Basics
查看>>
nupkg 之破解 nodejs+electron-packager 打包exe的解包
查看>>
Objective-C 使用 C++类
查看>>
浅谈之高级查询over(partition by)
查看>>
Notes: CRM Analytics–BI from a CRM perspective (2)
查看>>
graphite custom functions
查看>>
列出所有的属性键
查看>>
js获取请求地址后面带的参数
查看>>
[原创]使用java批量修改文件编码(ANSI-->UTF-8)
查看>>
设计模式のCompositePattern(组合模式)----结构模式
查看>>
二进制集合枚举子集
查看>>
磁盘管理
查看>>
SAS学习经验总结分享:篇二—input语句
查看>>
UIImage与UIColor互转
查看>>
RotateAnimation详解
查看>>
系统管理玩玩Windows Azure
查看>>
c#匿名方法
查看>>
如何判断链表是否有环
查看>>