当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- What does CTO (technical director) usually do?
- 基于STM32的物联网下智能化养鱼鱼缸控制控制系统
- Subnet partition operation
- CondaValueError: The target prefix is the base prefix. Aborting.
- Dijkstra seeking secondary short circuit (easy to understand)
- Mysql优化查询速度
- Realization of truth table assignment by discrete mathematical programming
- Markdown use
- Interpretation of ebpf sockops code
- Geek University cloud native training camp
猜你喜欢

Wireshark packet capturing skills summarized by myself

大厂出海,败于“姿态”

Summary of message protocol problems

Memcached comprehensive analysis – 5 Memcached applications and compatible programs

JMeter implementation specifies concurrent loop testing

Return of missing persons

Static routing experiment

ping: www.baidu. Com: unknown name or service

Blender's landscape

The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich
随机推荐
188. 买卖股票的最佳时机 IV
Functional analysis of ebpf tracepoint
Go coding specification
Memcached full profiling – 1 Fundamentals of memcached
AntDB数据库在线培训开课啦!更灵活、更专业、更丰富
PHP script calls command to get real-time output
Splicing audio files with ffmpeg-4.3
Understanding openstack network
66 pitfalls in go programming language: pitfalls and common errors of golang developers
TCP Jprobe utilization problem location
Axi DMA IP core operation process
Return of missing persons
Different WordPress pages display different gadgets
虚拟货币7个月蒸发2万亿美元,“马斯克们”终结15万人暴富梦
Dynamic routing protocol rip, OSPF
装修首页自定义全屏视频播放效果gif动态图片制作视频教程播放代码操作设置全屏居中阿里巴巴国际站
188. the best time to buy and sell stocks IV
Wireshark packet capturing skills summarized by myself
JMeter basic learning records
Handwritten RPC the next day -- review of some knowledge