当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- [cloud native learning notes] learn about kubernetes' pod
- 为什么生命科学企业都在陆续上云?
- Role of wait function
- Realization of truth table assignment by discrete mathematical programming
- EditText 控制软键盘出现 搜索
- Address mapping of virtual memory paging mechanism
- Auto. JS to realize automatic unlocking screen
- PIXIV Gizmo
- Tdengine can read and write through dataX
- database/sql
猜你喜欢

Concepts of kubernetes components

Big factories go out to sea and lose "posture"

What does CTO (technical director) usually do?

The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich

Basic database syntax learning

Page replacement of virtual memory paging mechanism

Why are life science enterprises on the cloud in succession?

memcached全面剖析–2. 理解memcached的內存存儲

66 pitfalls in go programming language: pitfalls and common errors of golang developers

大厂出海,败于“姿态”
随机推荐
Dynamic routing protocol rip, OSPF
Role of wait function
ping: www.baidu. Com: unknown name or service
Big factories go out to sea and lose "posture"
Read all text from stdin to a string
Static routing experiment
Distributed basic concepts
Implementation of adjacency table storage array of graph
Memcached comprehensive analysis – 2 Understand memcached memory storage
装修首页自定义全屏视频播放效果gif动态图片制作视频教程播放代码操作设置全屏居中阿里巴巴国际站
Mysql优化查询速度
Foundations of Cryptography
Go coding specification
TCP_ Nodelay and TCP_ CORK
去掉录屏提醒(七牛云demo)
Station B takes goods to learn from New Oriental
Physical layer introduction
Pod lifecycle in kubernetes
Volcano成Spark默认batch调度器
66 pitfalls in go programming language: pitfalls and common errors of golang developers