500. Keyboard Row
来源:互联网 发布:红米手机怎么清除数据 编辑:程序博客网 时间:2024/06/05 10:42
一、题目:
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
二、解题
class Solution { /* 从第一个单词开始找,先找出该单词首字母在哪行, 然后看那个单词是否都在那行。 若在,给添加到result中。 */public: vector<string> findWords(vector<string>& words) { unordered_set<char> row1{ 'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p' }; unordered_set<char> row2{ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' }; unordered_set<char> row3{ 'z', 'x', 'c', 'v', 'b' ,'n', 'm' }; vector<unordered_set<char>> rows{ row1, row2, row3 }; vector<string> validWords; for (int i = 0; i<words.size(); ++i) { int row = 0; for (int k = 0; k<3; ++k) { if (rows[k].count((char)tolower(words[i][0])) > 0) row = k; } for (int j = 0; j<words[i].size(); ++j) { if (rows[row].count((char)tolower(words[i][j])) == 0) break; if (j == words[i].size() - 1) validWords.push_back(words[i]); } } return validWords; }};
阅读全文
0 0
- Leetcode-500. Keyboard Row
- 【Leetcode】500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row
- 【LeetCode】500. Keyboard Row
- 500. Keyboard Row
- LeetCode 500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row
- LeetCode 500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row (E)
- 500. Keyboard Row
- 500. Keyboard Row
- leetcode 500. Keyboard Row
- 500. Keyboard Row
- [LeetCode]500. Keyboard Row
- 500. Keyboard Row
- HDU 6108 小C的倍数问题 (数论 2017百度之星初赛A第1题)
- xcode 删除复制某一行 快捷键
- 《高质量 C++/C 编程指南 》阅读笔记(三)
- Tomcat出现Unloading class sun.reflect.GeneratedMethodAccessor216]解决方案
- 关于STL的一些笔记
- 500. Keyboard Row
- Address类型和Mapping 类型
- 解决apache配置php7时出现的Cannot load ../php-5.6.13/php5apache2_4.dll into server:
- 背包九讲第一讲-简单的0/1背包问题有感1.3
- 1526:宗教信仰(4.3算法之图论)
- JavaScript趣味编程--仿微信飞机大战游戏--1.画飞机
- 蔡勒公式计算星期+枚举
- 第一个Java Web项目
- python,格式化字符串函数.format()的用法