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:
0 <= N <= 10^5
- 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的更多相关文章
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 838. Push Dominoes
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- LeetCode 838. Push Dominoes
原题链接在这里:https://leetcode.com/problems/push-dominoes/ 题目: There are N dominoes in a line, and we plac ...
- 【leetcode】838. Push Dominoes
题目如下: 解题思路:本题题目中有一点要求很关键,“we will consider that a falling domino expends no additional force to a fa ...
- 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 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
随机推荐
- Python练习题 005:三个数字由大到小排序输出
[Python练习题 005]输入三个整数x,y,z,请把这三个数由小到大输出. ----------------------------------------------------------- ...
- 使用模拟退火算法优化 Hash 函数
背景 现有个处理股票行情消息的系统,其架构如下: 由于数据量巨大,系统中启动了 15 个线程来消费行情消息.消息分配的策略较为简单:对 symbol 的 hashCode 取模,将消息分配给其中一个线 ...
- spring-boot-route(五)整合Swagger生成接口文档
目前,大多数公司都采用了前后端分离的开发模式,为了解决前后端人员的沟通问题,后端人员在开发接口的时候会选择使用swagger2来生成对应的接口文档,swagger2提供了强大的页面调试功能,这样可以有 ...
- 浅谈BSGS
用于求解形如\(a^x≡b\mod p\)的最小非负整数解\(x\). 由欧拉定理\(a^{\phi(p)}≡1\mod p\)可以知道,我们找的解如果有解则一定在\(\phi(p)\)范围内,而最大 ...
- Python+Appium自动化测试(12)-通过坐标定位元素
在使用appium做app自动化测试的过程中,可能会遇到元素的属性值不是唯一的情况,导致不能通过find_element_bi_xx()方法定位元素,这个时候我们就可以通过坐标来定位元素. 1,通过绝 ...
- 手写一个HTTP框架:两个类实现基本的IoC功能
jsoncat: 仿 Spring Boot 但不同于 Spring Boot 的一个轻量级的 HTTP 框架 国庆节的时候,我就已经把 jsoncat 的 IoC 功能给写了,具体可以看这篇文章&l ...
- 【7】进大厂必须掌握的面试题-Java面试-Jsp
1. jsp的生命周期方法是什么? 方法 描述 公共无效的jspInit() 与servlet的init方法相同,仅被调用一次. 公共无效_jspService(ServletRequest requ ...
- Python3.7有什么新变化
https://docs.python.org/zh-cn/3/whatsnew/3.7.html
- 【水】怎么在 HZOI 上水到更高的分
前言 这些东西在联赛并用不了 预编译优化 40行优化 #define _CRT_SECURE_NO_WARNINGS #pragma GCC optimize(2) #pragma GCC optim ...
- java流程控制之习题
经过近段时间的学习,差不多也掌握了java的流程控制以及基本知识,下面就来一起练练习题吧,看能做出来几道. 第一道题:假设小明有100块钱,这时候小明去超市需要换零钱,超市提供的零钱有1元面值,2元 ...