当前位置:网站首页>力扣每日一题-第26天-496.下一个更大元素Ⅰ
力扣每日一题-第26天-496.下一个更大元素Ⅰ
2022-06-24 19:24:00 【重邮研究森】
2022.6.24今天你刷题了吗?
题目:
给你一个字符串数组 words ,只返回可以使用在 美式键盘 同一行的字母打印出来的单词。键盘如下图所示。
美式键盘 中:
第一行由字符 "qwertyuiop" 组成。
第二行由字符 "asdfghjkl" 组成。
第三行由字符 "zxcvbnm" 组成。
分析:
也就是说给你一些字符串,你需要判断是不是在同一行,如果是则返回,否则则不返回。
思路:利用暴力求解,先判断字符串第一个字母确定它处于哪一行,然后依次判断后面的字符是不是属于改行。
解析:
1.暴力求解
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string>vec;
int row1 = 0;
int flag = 0;
int successflag = 0;
for (auto i = 0; i < words.size(); i++)
{
for (auto& j : words[i])
{
if (flag == 0)
{
if (j == 'q' || j == 'w' || j == 'e' || j == 'r' || j == 't' || j == 'y' || j == 'u' || j == 'i' || j == 'o' || j == 'p' || j == 'Q' || j == 'W' || j == 'E' || j == 'R' || j == 'T' || j == 'Y' || j == 'U' || j == 'I' || j == 'O' || j == 'P')
{
row1 = 1;
flag = 1;
successflag = 1;
}
else if (j == 'a' || j == 's' || j == 'd' || j == 'f' || j == 'g' || j == 'h' || j == 'j' || j == 'k' || j == 'l' || j == 'A' || j == 'S' || j == 'D' || j == 'F' || j == 'G' || j == 'H' || j == 'J' || j == 'K' || j == 'L')
{
row1 = 2;
flag = 1;
successflag = 1;
}
else
{
row1 = 3;
flag = 1;
successflag = 1;
}
}
else
{
if (row1==1&&(j == 'q' || j == 'w' || j == 'e' || j == 'r' || j == 't' || j == 'y' || j == 'u' || j == 'i' || j == 'o' || j == 'p' || j == 'Q' || j == 'W' || j == 'E' || j == 'R' || j == 'T' || j == 'Y' || j == 'U' || j == 'I' || j == 'O' || j == 'P' ))
{
successflag = 1;
}
else if ( row1 == 2&&(j == 'a' || j == 's' || j == 'd' || j == 'f' || j == 'g' || j == 'h' || j == 'j' || j == 'k' || j == 'l' || j == 'A' || j == 'S' || j == 'D' || j == 'F' || j == 'G' || j == 'H' || j == 'J' || j == 'K' || j == 'L'))
{
successflag = 1;
}
else if ( row1 == 3&&(j == 'z' || j == 'x' || j == 'c' || j == 'v' || j == 'b' || j == 'n' || j == 'm' || j == 'Z' || j == 'X' || j == 'C' || j == 'V' || j == 'B' || j == 'N' || j == 'M' ))
{
successflag = 1;
}
else
{
successflag = 0;
break;
}
}
}
if (successflag == 1)
{
vec.emplace_back(words[i]);
flag = 0;
}
else
{
flag = 0;
}
}
return vec;
}
};
2.哈希表
先把每行的字符存进三个哈希表,然后我们判断每个字符串,是否存在与某行,这里利用了哈希表的键值对为1,我们看这个哈希表是否存在
class Solution {
public:
vector<string> findWords(vector<string>& words) {
string one = "qwertyuiopQWERTYUIOP";
string two = "asdfghjklASDFGHJKL";
string three = "zxcvbnmZXCVBNM";
unordered_map<char, int>m1, m2, m3;
for (auto q : one)
{
m1[q]++;
}
for (auto q : two)
{
m2[q]++;
}
for (auto q : three)
{
m3[q]++;
}
vector<string>vec;
for (auto word : words)
{
bool b1 = true;
bool b2 = true;
bool b3 = true;
for (auto &c : word)
{
b1 &= m1[c];
b2 &= m2[c];
b3 &= m3[c];
}
if (b1 || b2 || b3)
{
vec.emplace_back(word);
}
}
return vec;
}
};
边栏推荐
- Analysis of BBR congestion control state machine
- Geek University cloud native training camp
- Unity关于本地坐标和世界坐标之间的转换
- Pytest test framework II
- Introduction to interval DP
- Appium introduction and environment installation
- Wireshark packet capturing skills summarized by myself
- Rip/ospf protocol notes sorting
- Web project deployment
- 去掉录屏提醒(七牛云demo)
猜你喜欢
Memcached comprehensive analysis – 2 Understand memcached memory storage
Station B takes goods to learn from New Oriental
XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
(to be optimized and modified) vivado DDR4 SDRAM (MIG) (2.2) IP core learning record
自己总结的wireshark抓包技巧
JMeter implementation specifies concurrent loop testing
Analysis of BBR congestion control state machine
JMeter basic learning records
Antdb database online training has started! More flexible, professional and rich
Memcached comprehensive analysis – 5 Memcached applications and compatible programs
随机推荐
Graduation summary of phase 6 of the construction practice camp
Ebpf XDP mount point analysis
Return of missing persons
[Web Security Basics] some details
Advanced secret of xtransfer technology newcomers: the treasure you can't miss mentor
Golang reflection operation collation
B站带货当学新东方
使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北
Volcano becomes spark default batch scheduler
TCP specifies the source port
Three more days
[cloud native learning notes] kubernetes practice command
BBR bandwidth per second conversion logic
Wireshark packet capturing skills summarized by myself
XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor
Rewrite, maplocal and maplocal operations of Charles
The first day of handwritten RPC -- review of some basic knowledge
ping: www.baidu. Com: unknown name or service
Please open online PDF carefully
Jar package operation