题目描述 LeetCode 500
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.Example 1:
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.
解题思路
- 依次取出
words
数字中的字符串,和keyboard
中的每个字符串进行单个字符的比较。 - 例如,判断
Hello
是否在同一行,依次与keyboard
中的字符串进行比较,如与QWERTYUIOP
依次逐字符进行比较,设置temp
计数变量,如H
在QWERTYUIOP
中则计数加1
,依次循环之后,如果temp
等于Hello
的长度,
则Hello
在同一行。
Code
# include<stdio.h>
# include<string.h>
int vv(char *word)
{
int i, k, m;
int flag = 0;
int xx, yy, temp;
char *keyboard[3] = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
xx = strlen(word);
for (k = 0; k < 3; k ++)
{
yy = strlen(*(keyboard + k));
temp = 0;
for (m = 0; m < yy; m ++)
{
for (i = 0; i < xx; i++)
{
if ( (word[i] == *(*(keyboard + k) + m)) || (word[i] == (*(*(keyboard + k) + m) + 32)))
{
temp += 1;
}
}
if (temp == xx)
{
flag = 1;
break;
}
}
}
if(flag == 1){
return 1;
}
else{
return 0;
}
}
char** findWords(char** words, int wordsSize, int* returnSize)
{
int i ;
static char *word[100];
char** return_words;
return_words = word;
for (i = 0; i < wordsSize; i ++)
{
if(vv( *(words + i)))
{
// printf("%s\n", *(words + i));
*(return_words + (*returnSize)) = *(words + i);
(*returnSize) ++;
}
}
return return_words;
}
int main()
{
int i ;
int wordsSize = 4;
int returnSize = 0;
char *words[4] = {"Hello", "Alaska", "Dad", "Peace"};
char** doo;
doo = findWords(words, wordsSize, &returnSize);
for (i = 0; i < returnSize; i ++){
printf("%s\n", *(doo + i));
}
}