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

#include <leetcode.h>

静态 Public 成员函数

static pair< int, int > dfs (string s, int start, int end)
 
static string longestNiceSubstring (const string &s)
 

详细描述

在文件 leetcode.h856 行定义.

成员函数说明

◆ dfs()

pair< int, int > leetcode::longest_nice_substring::Solution::dfs ( string  s,
int  start,
int  end 
)
static

在文件 leetcode.cpp2108 行定义.

2108 {
2109 if(start == end) {
2110 return {start, 0};
2111 }
2112 int lower = 0;
2113 int upper = 0;
2114 int max_start = 0;
2115 int max_len = 0;
2116 for(int i = start; i <= end; i++) {
2117 const char ch = s[i];
2118 if(islower(ch) != 0) {
2119 lower |= 1 << ch - 'a';
2120 } else {
2121 //isupper
2122 upper |= 1 << ch - 'A';
2123 }
2124 }
2125 if(lower == upper) {
2126 //是美好字符串
2127 return {start, end - start + 1};
2128 }
2129 //不是美好字符串
2130 const int not_nice = lower ^ upper;//无法构成美好字符串的字符
2131 int i = start;
2132 while(i <= end) {
2133 if((not_nice >> tolower(s[i]) - 'a' & 1) == 1) {
2134 //在not_nice中
2135 i++;
2136 continue;
2137 }
2138 int j = i + 1;
2139 while(j <= end && (not_nice >> tolower(s[j]) - 'a' & 1) != 1) {
2140 j++;
2141 }
2142 auto [next_start, next_len] = dfs(s, i, j - 1);
2143 if(max_len < next_len) {
2144 max_len = next_len;
2145 max_start = next_start;
2146 }
2147 i = j;
2148 }
2149 return {max_start, max_len};
2150 }
static pair< int, int > dfs(string s, int start, int end)
Definition: leetcode.cpp:2108

引用了 dfs().

被这些函数引用 dfs() , 以及 longestNiceSubstring().

◆ longestNiceSubstring()

string leetcode::longest_nice_substring::Solution::longestNiceSubstring ( const string &  s)
static

在文件 leetcode.cpp2100 行定义.

2100 {
2101 auto [max_start, max_len] = dfs(s, 0, s.length() - 1);
2102 if(max_len == 0) {
2103 return "";
2104 }
2105 return s.substr(max_start, max_len);
2106 }

引用了 dfs().

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


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