C#LeetCode刷题之#500-键盘行(Keyboard Row)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3796 访问。
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]
注意:
你可以重复使用键盘上同一字符。
你可以假设输入的字符串将只包含字母。
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.
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:
You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3796 访问。
public class Program {
public static void Main(string[] args) {
var words = new string[] { "Hello", "Alaska", "Dad", "Peace" };
var res = FindWords(words);
ShowArray(res);
Console.ReadKey();
}
private static void ShowArray(string[] array) {
foreach(var num in array) {
Console.Write($"{num} ");
}
Console.WriteLine();
}
public static string[] FindWords(string[] words) {
var res = new List<string>();
var line = new List<List<char>>() {
new List<char> { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' } ,
new List<char>{ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l' },
new List<char>{ 'z', 'x', 'c', 'v', 'b', 'n', 'm' }};
foreach(var word in words) {
var list = new HashSet<int>();
foreach(var item in word) {
for(var i = 0; i < 3; i++) {
if(line[i].Contains(item)) list.Add(i);
}
if(list.Count > 1) break;
}
if(list.Count == 1) {
res.Add(word);
}
}
return res.ToArray();
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3796 访问。
Alaska Dad
分析:
该题的时间复杂度和单词的长度有关,设最长的单词长度为m,单词的数量为n,那么该题的时间复杂度为: 。
C#LeetCode刷题之#500-键盘行(Keyboard Row)的更多相关文章
- [Swift]LeetCode500. 键盘行 | Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
随机推荐
- Ubuntu18.04安装Docker并部署(编译、发布、构建镜像)Asp.NetCore项目全过程笔记
环境准备:阿里云Ubuntu18.04 全新安装 一.安装Docker 1.删除旧版本并更新包索引: sudo apt-get remove docker docker-engine dock ...
- 在VS2017中创建C++的代码块模板
在VS2017中创建C++的代码块模板 有任何问题,请留言!!! 在VS2017中有工具–>代码片段管理器,方便我们使用固有的代码块模板,同时我们也可以自定义模板. 在VS2017中代码片段的模 ...
- jmeter 及测试(转载)
负载测试:在一定的工作负荷下,给系统造成的负荷及系统响应的时间. 压力测试:在一定的负荷条件下,长时间连续运行系统给系统性能造成的影响. 1.性能测试(Performance Test):通常收集 ...
- centos7中防火墙转为iptables
1.关闭firewall systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止fir ...
- 从LocalDateTime序列化探讨全局一致性序列化
日拱一卒无有尽,功不唐捐终入海. 楔子 前两周发了三篇SpringSecurity和一篇征文,这周打算写点简单有用易上手的文章,换换脑子,休息一下. 今天要写的是这篇:从LocalDateTime序列 ...
- [spring] -- AOP、IOC、DI篇
AOP(面向切面编程) 怎么理解这个切面编程的概念及优点? 概念: 横切关注点与业务逻辑相分离,切面能帮助我们模块化横切关注点: 优点: 现在每个关注点都集中于一个地方,而不是分散到多处代码中: 服务 ...
- 一个startforresult的例子
https://blog.csdn.net/qq_32521313/article/details/52451364
- python---filecmp 实现文件,目录,遍历子目录的差异对比功能。
python---filecmp ilecmp可以实现文件,目录,遍历子目录的差异对比功能. 自带filecmp模块,无需安装. 常用方法说明 filecmp提供3个操作方法,cmp(单文件对比),c ...
- iframe和DataForm
一.iframe使用 iframe在一个页面中,相当于整个window窗口的子窗口,可通过页面的元素结构查看. <div> <p>学习iframe</p> < ...
- JavaScript高级程序设计(第三版) 7/25
第七章 函数表达式 1.定义函数的方式有两种,一种是函数声明,一种是函数表达式. //函数声明 function fuc (a){ } //函数表达式 var fuc = function(a){ } ...