原题
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.
American keyboard
Example 1:
Input: [“Hello”, “Alaska”, “Dad”, “Peace”]
Output: [“Alaska”, “Dad”]
代码实现
public string[] FindWords(string[] words)
{
int[] hash = new int[123];
char[] chs1 = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
foreach (var ch in chs1)
{
hash[Convert.ToInt32(ch)] = 1; //q
hash[Convert.ToInt32(ch)-32] = 1; //Q
}
chs1 = new[] {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
foreach (var ch in chs1)
{
hash[Convert.ToInt32(ch)] = 2;
hash[Convert.ToInt32(ch) - 32] = 2;
}
chs1 = new[] {'z', 'x', 'c', 'v', 'b', 'n', 'm'};
foreach (var ch in chs1)
{
hash[Convert.ToInt32(ch)] = 3;
hash[Convert.ToInt32(ch) - 32] = 3;
}
List<string> rtn = new List<string>();
foreach (var word in words)
{
if(string.IsNullOrEmpty(word)) continue;
int row = hash[word[0]];
bool allflag = true;
foreach (var ch in word)
{
if (hash[ch] != row)
{
allflag = false;
break;
}
}
if(allflag==true) rtn.Add(word);
}
return rtn.ToArray();
}
leetcode-solution库
leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp