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

#include <leetcode.h>

静态 Public 成员函数

static int countCollisions (const string &directions)
 

详细描述

在文件 leetcode.h1841 行定义.

成员函数说明

◆ countCollisions()

int leetcode::count_collisions_on_a_road::Solution::countCollisions ( const string &  directions)
static

在文件 leetcode.cpp4783 行定义.

4783 {
4784 int ans = 0;
4785 vector<char> status;
4786 vector<int> count;
4787 int current = 0;
4788 for(char ch: directions) {
4789 if(status.empty()) {
4790 status.push_back(ch);
4791 current++;
4792 } else {
4793 if(status.back() == ch) {
4794 current++;
4795 } else {
4796 status.push_back(ch);
4797 count.push_back(current);
4798 current = 1;
4799 }
4800 }
4801 }
4802 count.push_back(current);
4803 for(int i = 0; i + 1 < status.size(); i++) {
4804 if(status[i] == 'R' && status[i + 1] == 'L') {
4805 ans += count[i] + count[i + 1];
4806 } else if(status[i] == 'R' && status[i + 1] == 'S') {
4807 ans += count[i];
4808 } else if(status[i] == 'S' && status[i + 1] == 'L') {
4809 ans += count[i + 1];
4810 }
4811 }
4812 return ans;
4813 }

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


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