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

#include <leetcode.h>

静态 Public 成员函数

static long long numberOfWays (string s)
 

详细描述

在文件 leetcode.h2017 行定义.

成员函数说明

◆ numberOfWays()

long long leetcode::number_of_ways_to_select_buildings::Solution::numberOfWays ( string  s)
static

在文件 leetcode.cpp5460 行定义.

5460 {
5461 const unsigned int n = s.length();
5462 vector<unsigned int> prev0(n, 0);
5463 vector<unsigned int> prev1(n, 0);
5464 vector<unsigned int> post0(n, 0);
5465 vector<unsigned int> post1(n, 0);
5466 unsigned current0 = 0;
5467 unsigned current1 = 0;
5468 for(int i = 0; i < n; i++) {
5469 prev0[i] = current0;
5470 prev1[i] = current1;
5471 if(s[i] == '0') {
5472 current0++;
5473 } else {
5474 current1++;
5475 }
5476 }
5477 current0 = 0;
5478 current1 = 0;
5479 for(int i = n - 1; i >= 0; i--) {
5480 post0[i] = current0;
5481 post1[i] = current1;
5482 if(s[i] == '0') {
5483 current0++;
5484 } else {
5485 current1++;
5486 }
5487 }
5488 long long ans = 0;
5489 for(unsigned i = 0; i < n; i++) {
5490 if(s[i] == '0') {
5491 ans += static_cast<long long>(prev1[i]) * post1[i];
5492 } else {
5493 ans += static_cast<long long>(prev0[i]) * post0[i];
5494 }
5495 }
5496 return ans;
5497 }

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