problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::push_dominoes::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static string pushDominoes (string dominoes)
 

详细描述

在文件 leetcode.h1313 行定义.

成员函数说明

◆ pushDominoes()

string leetcode::push_dominoes::Solution::pushDominoes ( string  dominoes)
static

在文件 leetcode.cpp3329 行定义.

3329 {
3330 const int n = dominoes.length();
3331 auto *left_to_r = new int[n];
3332 auto *right_to_l = new int[n];
3333 auto *left_is = new char[n];
3334 auto *right_is = new char[n];
3335 int l2r = 0;
3336 int r2l = 0;
3337 char ch = '.';
3338 for(int i = 0; i < n; i++) {
3339 if(dominoes[i] != '.') {
3340 ch = dominoes[i];
3341 }
3342 left_is[i] = ch;
3343 }
3344 ch = '.';
3345 for(int i = n - 1; i >= 0; i--) {
3346 if(dominoes[i] != '.') {
3347 ch = dominoes[i];
3348 }
3349 right_is[i] = ch;
3350 }
3351 for(int i = 0; i < n; i++) {
3352 if(dominoes[i] == 'R') {
3353 l2r = 0;
3354 } else {
3355 l2r++;
3356 }
3357 left_to_r[i] = l2r;
3358 }
3359 for(int i = n - 1; i >= 0; i--) {
3360 if(dominoes[i] == 'L') {
3361 r2l = 0;
3362 } else {
3363 r2l++;
3364 }
3365 right_to_l[i] = r2l;
3366 }
3367 for(int i = 0; i < n; i++) {
3368 if(dominoes[i] == '.') {
3369 if(left_is[i] == 'R' && right_is[i] == 'L') {
3370 if(right_to_l[i] > left_to_r[i]) {
3371 dominoes[i] = 'R';
3372 } else if(left_to_r[i] > right_to_l[i]) {
3373 dominoes[i] = 'L';
3374 }
3375 } else if(right_is[i] == 'L') {
3376 dominoes[i] = 'L';
3377 } else if(left_is[i] == 'R') {
3378 dominoes[i] = 'R';
3379 }
3380 }
3381 }
3382 delete[] left_to_r;
3383 delete[] right_to_l;
3384 delete[] left_is;
3385 delete[] right_is;
3386 return dominoes;
3387 }

被这些函数引用 leetcode::push_dominoes::TEST().


该类的文档由以下文件生成: