返回信息流把键盘键位分成三行,找出属于同一行的字符串。下面是我的解法,
改了很久找不出错在哪,求版内大神指教
char** findWords(char** words, int wordsSize, int* returnSize) {
int i, j, returnWordsIndex= 0;
char** returnWords = (char**)malloc(*returnSize);
for ( ; i < sizeof(words)/sizeof(words[1]); i++) {
bool isRow1, isRow2, isRow3 = false;
for ( ; j < strlen(words[i]); j++) {
if (toupper(words[i][j]) == 'Q' || toupper(words[i][j]) == 'W' || toupper(words[i][j]) == 'E' || toupper(words[i][j]) == 'R' || toupper(words[i][j]) == 'T' || toupper(words[i][j]) == 'Y' || toupper(words[i][j]) == 'U' || toupper(words[i][j]) == 'I'|| toupper(words[i][j]) == 'O'|| toupper(words[i][j]) == 'P') {
isRow1 = true;
if(isRow2 || isRow3)
break;
}
if (toupper(words[i][j]) == 'A' || toupper(words[i][j]) == 'S' || toupper(words[i][j]) == 'D' || toupper(words[i][j]) == 'F' || toupper(words[i][j]) == 'G' || toupper(words[i][j]) == 'H' || toupper(words[i][j]) == 'J' || toupper(words[i][j]) == 'K'|| toupper(words[i][j]) == 'L') {
isRow2 = true;
if(isRow1 || isRow3)
break;
}
if (toupper(words[i][j]) == 'Z' || toupper(words[i][j]) == 'X' || toupper(words[i][j]) == 'C' || toupper(words[i][j]) == 'V' || toupper(words[i][j]) == 'B' || toupper(words[i][j]) == 'N' || toupper(words[i][j]) == 'M') {
isRow3 = true;
if(isRow2 || isRow1)
break;
}
if (j == strlen(words[i]) - 1) {
returnWords[returnWordsIndex] = (char*) malloc(strlen(words[i]));
strcpy(returnWords[returnWordsIndex], words[i]);
++returnWordsIndex;
}
}
}
return returnWords;
}
感谢大家回帖,我试了试自己传参数进去
加上了下面的main函数
int main() {
char words[2][10] = {"dad","alaska"};
char *pwords = words;
int wordsSize = 10;
int *r = NULL;
*r = 40;
findWords (pwords, wordsSize, r);
}
编译过不去,显示错误如图
这是一条镜像帖。来源:北邮人论坛 / cpp / #94539同步于 2017/2/21
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
【问题】leetcode 500. Keyboard Row
bluminFlower
2017/2/21镜像同步10 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
public String[] findWords(String[] words) {
String[] line = {"qwertyuiopQWERTYUIOP","asdfghjklASDFGHJKL","zxcvbnmZXCVBNM"};
ArrayList<String> ans = new ArrayList<String>();
for (int i = 0; i < words.length; i++) { //循环所有单词
ArrayList<Integer> lineN = new ArrayList<Integer>();
for (int j = 0; j < words[i].length(); j++) { //循环单词里的字符
for (int k = 0; k < 3; k++) { //循环每一行键盘
if (line[k].contains("" + words[i].charAt(j))) { //如果该键盘行有这个字符
lineN.add(k); //加到一个ArrayList里
}
}
}
Collections.sort(lineN); //偷懒排序
if (lineN.get(0) == lineN.get(lineN.size() - 1)) { // 比较排序后第一个和最后一个键盘行是否一样
ans.add(words[i]);
}
}
return ans.toArray(new String[ans.size()]);
}
super偷懒的做法。。。。
我试了一下,char **的sizeof确实是8,只有指针的长度。
sizeof是在编译阶段完成的,所以你的sizeof(words)和sizeof(words[1])都是8,(而且第一个应该是words[0]),所以不要用除式去计算words的第二维。这里和二维数组不一样,C语言的指针是个坑。
你后面写的main其实也是这个问题,char ** 和char [][] 是不一样的,你把二维数组传递给char ** 的形参被编译器拒绝了,你要调用这个函数的话,可以在main中new 二维数组,然后传递给find函数,像这样:
一般都没人选C的,楼主换成C++吧。
按照题目的描述,应该是用wordsSize传递的字符串个数,返回的数组有效长度存在returnSize里。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** findWords(char** words, int wordsSize, int * returnSize) {
char rows1[100] = "QWERTYUIOPqwertyuiop";
char rows2[100] = "ASDFGHJKLasdfghjkl";
char rows3[100] = "ZXCVBNMzxcvbnm";
char ** ans = (char **)malloc(sizeof(char *) * (wordsSize));
int count = 0;
for(int i = 0; i < wordsSize; i++)
{
int line = 0;
if(strchr(rows1, words[i][0]) != NULL)
line = 1;
else if(strchr(rows2, words[i][0]) != NULL)
line = 2;
else if(strchr(rows3, words[i][0]) != NULL)
line = 3;
for(int pos = 0; words[i][pos] && line; pos ++)
{
switch(line)
{
case 1:
if(strchr(rows1, words[i][pos]) == NULL) line = 0;
break;
case 2:
if(strchr(rows2, words[i][pos]) == NULL) line = 0;
break;
case 3:
if(strchr(rows3, words[i][pos]) == NULL) line = 0;
break;
}
}
if(line)
{
ans[count] = (char *)malloc(sizeof(char) * (2 + strlen(words[i])));
strcpy(ans[count], words[i]);
count ++;
}
}
* returnSize = count;
return ans;
}
int main()
{
char ** s = (char **)malloc(sizeof(char *) * 10);
for(int i = 0; i < 10; i++)
s[i] = (char *)malloc(sizeof(char) * 100);
strcpy(s[0], "Hello");
strcpy(s[1], "Alaska");
strcpy(s[2], "Dad");
strcpy(s[3], "Peace");
int len;
char ** ans = findWords(s, 4, &len);
for(int i = 0; i < len; i++)
printf("%s\n", ans[i]);
return 0;
}
```
【 在 bluminFlower 的大作中提到: 】
: 把键盘键位分成三行,找出属于同一行的字符串。下面是我的解法,
: 改了很久找不出错在哪,求版内大神指教
:
: ...................
【 在 bluminFlower 的大作中提到: 】
: 把键盘键位分成三行,找出属于同一行的字符串。下面是我的解法,
: 改了很久找不出错在哪,求版内大神指教
:
: ...................
【 在 bluminFlower 的大作中提到: 】
: 把键盘键位分成三行,找出属于同一行的字符串。下面是我的解法,
: 改了很久找不出错在哪,求版内大神指教
:
: ...................
之前写的,LZ可以参考一下。
```cpp
class Solution {
public:
vector<string> findWords(vector<string>& words) {
string rows[3] = {"QWERTYUIOPqwertyuiop", "ASDFGHJKLasdfghjkl", "ZXCVBNMzxcvbnm"};
vector<string> res;
for(auto word : words){
if(word.size() < 1)
continue;
int row_num = -1;
bool flag = true;
for(auto c : word){
// cout << c << endl;
int i = 0;
for(;i<3;i++){
if(rows[i].find(c) != string::npos){
break;
}
}
if(row_num == -1 || i == row_num)
row_num = i;
else{
flag = false;
break;
}
}
if(flag)
res.push_back(word);
}
return res;
}
};
```