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

#include <leetcode.h>

Public 成员函数

unsigned int find (unsigned int x)
 
vector< int > groupStrings (vector< string > &words)
 
void insert (unsigned int num)
 
void to_union (unsigned int x1, unsigned int x2)
 

静态 Public 成员函数

static unsigned int compress (const string &word)
 

Private 属性

int groups = 0
 
unsigned int max_size = 1
 
unordered_map< unsigned int, unsigned int > parent = unordered_map<unsigned int, unsigned int>()
 
unordered_map< unsigned int, unsigned int > rank = unordered_map<unsigned int, unsigned int>()
 
unordered_map< unsigned int, unsigned int > size = unordered_map<unsigned int, unsigned int>()
 

详细描述

在文件 leetcode.h830 行定义.

成员函数说明

◆ compress()

unsigned int leetcode::groups_of_strings::Solution::compress ( const string &  word)
static

在文件 leetcode.cpp2037 行定义.

2037 {
2038 unsigned int ans = 0;
2039 for(const char ch: word) {
2040 ans |= 1 << ch - 'a';
2041 }
2042 return ans;
2043 }

被这些函数引用 groupStrings().

◆ find()

unsigned int leetcode::groups_of_strings::Solution::find ( unsigned int  x)

在文件 leetcode.cpp2057 行定义.

2057{ return x == parent[x] ? x : parent[x] = find(parent[x]); }
unordered_map< unsigned int, unsigned int > parent
Definition: leetcode.h:833
unsigned int find(unsigned int x)
Definition: leetcode.cpp:2057

引用了 find() , 以及 parent.

被这些函数引用 find() , 以及 to_union().

◆ groupStrings()

vector< int > leetcode::groups_of_strings::Solution::groupStrings ( vector< string > &  words)

在文件 leetcode.cpp2004 行定义.

2004 {
2005 auto ans = vector<int>();
2006 auto nums = vector<unsigned int>();
2007 for(const auto &word: words) {
2008 auto num = compress(word);
2009 nums.push_back(num);
2010 insert(num);
2011 }
2012 for(const auto num: nums) {
2013 for(int i = 0; i < 26; i++) {
2014 auto next = num ^ 1 << i;//添加或删除字符 i
2015 if(parent.contains(next)) {
2016 to_union(num, next);
2017 }
2018 if((num >> i & 1) == 1) {
2019 //存在字符i
2020 for(int j = 0; j < 26; j++) {
2021 if((num >> j & 1) == 0) {
2022 //不存在字符j
2023 auto next = num ^ 1 << i | 1 << j;
2024 if(parent.contains(next)) {
2025 to_union(num, num ^ 1 << i | 1 << j);// 替换字符 i 为 j
2026 }
2027 }
2028 }
2029 }
2030 }
2031 }
2032 ans.push_back(groups);
2033 ans.push_back(max_size);
2034 return ans;
2035 }
static unsigned int compress(const string &word)
Definition: leetcode.cpp:2037
void to_union(unsigned int x1, unsigned int x2)
Definition: leetcode.cpp:2059

引用了 compress(), groups, insert(), max_size, parent , 以及 to_union().

◆ insert()

void leetcode::groups_of_strings::Solution::insert ( unsigned int  num)

在文件 leetcode.cpp2045 行定义.

2045 {
2046 parent.insert(pair(num, num));
2047 rank.insert(pair(num, 0));
2048 if(!size.contains(num)) {
2049 size.insert(pair(num, 1));
2050 groups++;
2051 } else {
2052 size[num]++;
2053 max_size = size[num];
2054 }
2055 }
unordered_map< unsigned int, unsigned int > rank
Definition: leetcode.h:834
unordered_map< unsigned int, unsigned int > size
Definition: leetcode.h:835

引用了 groups, max_size, parent, rank , 以及 size.

被这些函数引用 groupStrings().

◆ to_union()

void leetcode::groups_of_strings::Solution::to_union ( unsigned int  x1,
unsigned int  x2 
)

在文件 leetcode.cpp2059 行定义.

2059 {
2060 const int f1 = find(x1);
2061 const int f2 = find(x2);
2062 if(f1 == f2) {
2063 return;
2064 }
2065 groups--;
2066 if(rank[f1] > rank[f2]) {
2067 parent[f2] = f1;
2068 size[f1] += size[f2];
2069 max_size = max(max_size, size[f1]);
2070 } else {
2071 parent[f1] = f2;
2072 size[f2] += size[f1];
2073 max_size = max(max_size, size[f2]);
2074 if(rank[f1] == rank[f2]) {
2075 ++rank[f2];
2076 }
2077 }
2078 }

引用了 find(), groups, max_size, parent, rank , 以及 size.

被这些函数引用 groupStrings().

类成员变量说明

◆ groups

int leetcode::groups_of_strings::Solution::groups = 0
private

在文件 leetcode.h831 行定义.

被这些函数引用 groupStrings(), insert() , 以及 to_union().

◆ max_size

unsigned int leetcode::groups_of_strings::Solution::max_size = 1
private

在文件 leetcode.h832 行定义.

被这些函数引用 groupStrings(), insert() , 以及 to_union().

◆ parent

unordered_map<unsigned int, unsigned int> leetcode::groups_of_strings::Solution::parent = unordered_map<unsigned int, unsigned int>()
private

在文件 leetcode.h833 行定义.

被这些函数引用 find(), groupStrings(), insert() , 以及 to_union().

◆ rank

unordered_map<unsigned int, unsigned int> leetcode::groups_of_strings::Solution::rank = unordered_map<unsigned int, unsigned int>()
private

在文件 leetcode.h834 行定义.

被这些函数引用 insert() , 以及 to_union().

◆ size

unordered_map<unsigned int, unsigned int> leetcode::groups_of_strings::Solution::size = unordered_map<unsigned int, unsigned int>()
private

在文件 leetcode.h835 行定义.

被这些函数引用 insert() , 以及 to_union().


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