当前位置:网站首页>Li Kou daily question - day 26 -496 Next larger element I
Li Kou daily question - day 26 -496 Next larger element I
2022-06-24 21:45:00 【Chongyou research Sen】
2022.6.24 Did you brush the questions today ?
subject :
Here's an array of strings words , Returns only those that can be used in American keyboard Words printed with letters on the same line . The keyboard is shown in the figure below .
American keyboard in :
The first line consists of the characters "qwertyuiop" form .
The second line consists of characters "asdfghjkl" form .
The third line consists of the characters "zxcvbnm" form .

analysis :
That is to say, I will give you some strings , You need to decide if you are in the same line , If so, return , Otherwise, do not return .
Ideas : Use violence to solve , First determine the first letter of the string to determine which line it is on , Then judge whether the following characters belong to line change .
analysis :
1. Violent solution
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. Hashtable
First save the characters of each line into three hash tables , Then we judge each string , Whether there is a relation with a line , The key value pair of the hash table is used here 1, Let's see if the hash table exists
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;
}
};边栏推荐
- 基于C语言实现的足球信息查询系统 课程报告+项目源码+演示PPT+项目截图
- 123. the best time to buy and sell shares III
- Auto. JS to automatically authorize screen capture permission
- memcached完全剖析–1. memcached的基础
- Slider controls the playback progress of animator animation
- Li Kou daily question - day 25 -496 Next larger element I
- Tso hardware sharding is a header copy problem
- #国企央企结构化面试#国企就业#墨斗互动就业服务管家
- TDengine可通过数据同步工具 DataX读写
- TypeScript快速入门
猜你喜欢

Blender FAQs

memcached全面剖析–2. 理解memcached的内存存储

Pattern recognition - 1 Bayesian decision theory_ P1

【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter

Antdb database online training has started! More flexible, professional and rich

memcached完全剖析–1. memcached的基础
![[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture](/img/b5/23e3aed317ca262ebd8ff4579a41a9.png)
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture

Football information query system based on C language course report + project source code + demo ppt+ project screenshot

VirtualBox virtual machine installation win10 Enterprise Edition

memcached全面剖析–3. memcached的删除机制和发展方向
随机推荐
Return of missing persons
Mysql优化查询速度
力扣每日一题-第25天-496.下一个更大元素Ⅰ
JMeter implementation specifies concurrent loop testing
去掉录屏提醒(七牛云demo)
#国企央企结构化面试#国企就业#墨斗互动就业服务管家
ping: www.baidu. Com: unknown name or service
Understanding openstack network
VirtualBox虚拟机安装Win10企业版
VSCode无网环境快速迁移开发环境(VIP典藏版)
Slider控制Animator动画播放进度
OSI and tcp/ip model
Blender's landscape
CondaValueError: The target prefix is the base prefix. Aborting.
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
[camera Foundation (I)] working principle and overall structure of camera
Tso hardware sharding is a header copy problem
Memcached comprehensive analysis – 2 Understand memcached memory storage
Make tea and talk about heroes! Leaders of Fujian Provincial Development and Reform Commission and Fujian municipal business office visited Yurun Health Division for exchange and guidance
Multi view function in blender