#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
vector<string> findWords(vector<string>& words);
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> res;
unordered_set<char> row1{ 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P' };
unordered_set<char> row2{ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L' };
unordered_set<char> row3{ 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'Z', 'X', 'C', 'V', 'B', 'N', 'M' };
for (string word : words) {
int a = 0;
int b = 0;
int c = 0;
for (char ch : word) {
if (row1.count(ch)) a = 1;
else if (row2.count(ch)) b = 1; //row2.count(ch)判断row2中有无ch数据,有返回1,无返回0。
else if (row3.count(ch)) c = 1;
if (a + b + c > 1) break;
}
if (a + b + c > 1)
cout << "不在同一键盘行" << endl;
if (a + b + c == 1)
{
res.push_back(word);
cout << "在同一键盘行" << endl;
}
}
return res;
}
};
void main()
{
vector<string> input,output;
string str;
Solution So;
cout << "输入字符串:" << endl;
cin >> str;
input.push_back(str);
So.findWords(input);
}
关于unordered_set:
unordered_set基于哈希表,是无序的。
set实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;另外,还得保证根节点左子树的高度与右子树高度相等。
平衡二叉检索树使用中序遍历算法,检索效率高于vector、deque和list等容器,另外使用中序遍历可将键值按照从小到大遍历出来。
unordered_map
存储机制是哈希表,,即unordered_map内部元素是无序的。
头文件:#include<unordered_map> #include<unordered_set>
什么叫hash表?