日拱一卒,月进一步(13)

简介: 500. 键盘行 - 力扣(LeetCode)好难啊!!!

500. 键盘行 - 力扣(LeetCode)

好难啊!!!

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char** findWords(char** words, int wordsSize, int* returnSize)
 {
    int map[26]={2,3,3,2,1,2,2,2,1,2,2,2,3,3,1,1,1,1,2,1,1,3,1,3,1,3};
    char**ret=(char**)malloc(sizeof(char*)*0);//初始化一个指向指针的指针,用来储存满足条件的单词的指针
    *returnSize=0;
    //遍历单词列表,检查该单词的所有字母是否在同一行,使用flag变量来储存第一个字母的行号,并比较后续子母的行号与其是否相同,如果不相同,则中断内部循环
    for(int i = 0; i < wordsSize; i++)
    {
        int j;//用于遍历单词中的每个字符
        int flag;//用于储存第一个字母的行号
        for(j = 0; j < strlen(words[i]); j++)
        {
            int temp;//用于临时储存当前字母所在的行号
            if(words[i][j] >= 'A' && words[i][j] <= 'Z')//如果是大写字母
            {
                temp = map[words[i][j] - 'A'];//计算当前字母在map数组中的索引,并获取其行号
                if(j == 0){//如果是第一个字母,设置其为flag
                    flag = temp;
                }
            }
            else
            {
                temp=map[words[i][j]-'a'];
                if(j==0)
                {
                    flag=temp;
                }
            }
                  if(flag!=temp)
            {
                break;
            }
        }
            if(j==strlen(words[i]))
            {
                (*returnSize)++;
                //重新分配单词的空间,以便存储新的单词指针
                ret=(char**)realloc(ret,(*returnSize)*sizeof(char*));
                //将当前单词的指针
                ret[(*returnSize)-1]=words[i];
            }
          
    
            }
            return ret;
}

506. 相对名次 - 力扣(LeetCode)

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int cmp(const void* a, const void* b) { return *(*int)a - *(*int)b; }
char** findRelativeRanks(int* score, int scoreSize, int* returnSize) {
    char** ret =
        (char**)malloc(scoreSize * sizeof(char*)); // 用于存储每个选手的相对名次
    (*returnSize) = scoreSize;
    for (int i = 0; i < scoreSize; i++) {
        ret[i] = (char*)malloc(
            sizeof(char) *
            13); // 为 ret
                 // 数组中的每个元素分配足够的空间来存储一个字符串(包括末尾的空字符)。这里分配了13个字符的空间,因为考虑到最大可能的排名(例如
                 // "123st")和奖牌名称("Gold Medal")
    }
    int* temp = (int*)malloc(sizeof(int) * scoreSize);
    memcpy(temo, score,
           sizeof(int) *
               scoreSize); // 分配一个整数数组 temp 并复制 score 数组的内容到
                           // temp,以便在不改变原始数组的情况下进行排序
    qsort(temp, scoreSize, sizeof(int), cmp);
    // 用于查找每个分数在排序后的 temp 数组中的位置,并为其分配相应的排名。
    for (int i = 0; i < scoreSize; i++) {
        for (int j = 0; j < scoreSize; j++) {
            if (score[i] == temp[j]) {
                if (j == scoreSize - 1) {
                    sprintf(ret[i], "%s", "Gold Medal");
                } else if (j == scoreSize - 2) {
                    sprintf(ret[i], "%s", "Silver Medal");
                } else if (j == scoreSize - 3) {
                    sprintf(ret[i], "%s", "Bronze Medal");
                } else {
                    sprintf(ret[i], "%d", scoreSize - j);
                }
            }
            return ret;
        }
相关文章
|
4天前
日拱一卒,月进一步(2)
那么,很快就来到了第二题的学习。哈哈~ 26. 删除有序数组中的重复项 - 力扣(LeetCode)
15 1
|
4天前
日拱一卒,月进一步(12)
485. 最大连续 1 的个数 - 力扣(LeetCode)
12 1
|
4天前
日拱一卒,月进一步(15)
598. 区间加法 II - 力扣(LeetCode) 首先明白题目的含义:mn表示的是一个矩阵,初始化为0。再依次在满足条件的矩形内+1,最后找出最大数字的个数。我们只需要找到最小的长和宽即可。
25 1
|
4天前
日拱一卒,月进一步(5)
88. 合并两个有序数组 - 力扣(LeetCode) 令我十分意外地是,这题竟然也曾经写过,但我却没有思路,罪该万死。
14 0
|
4天前
|
索引
日拱一卒,月进一步(11)
414. 第三大的数 - 力扣(LeetCode)
15 1
|
4天前
|
算法
日拱一卒,月进一步(8)
136. 只出现一次的数字 - 力扣(LeetCode) 这个题目一出现,我就立马有了思路。其实就是让每个数字互相异或,最后得出的数字就是只出现一次的数字。
19 1
|
4天前
日拱一卒,月进一步(9)
268. 丢失的数字 - 力扣(LeetCode)
14 1
|
4天前
日拱一卒,月进一步(14)
561. 数组拆分 - 力扣(LeetCode) 快排并从第一位开始隔位取数字
14 1
|
4天前
日拱一卒,月进一步(3)
27. 移除元素 - 力扣(LeetCode) 令人惊讶的是,这一题竟然答题答过,但我没有丝毫印象,啊,该死啊。
16 1
|
4天前
日拱一卒,月进一步(4)
66. 加一 - 力扣(LeetCode) 思路: 数字加法应该从前向后遍历,因此我们应该从数字末尾从后向前遍历。如果数字不为9,则直接在末尾+1。如果末尾为9,那么将其变为0,并且在下一位+1。如果一直遍历都为1,那么在数组第0位插入1。
13 1