力扣笔试题系列(一)

1、给你两个字符串 word1word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。

objectivec 复制代码
char * mergeAlternately(char * word1, char * word2){
    int len1 = strlen(word1);
    int len2 = strlen(word2);
    char *merge_word = malloc(len1+len2+1);

    int i , j = 0;
    if(len1 <= len2){
      
        for(i = 0; i < len1; i++){
            merge_word[j++] = word1[i];
            merge_word[j++] = word2[i];
        }

        if(len1 < len2){
            for(i = len1; i < len2; i++)
            merge_word[j++] = word2[i];
        }

    }
    else{
        for(i = 0; i < len2; i++){
            merge_word[j++] = word1[i];
            merge_word[j++] = word2[i];
        }

        for(i = len2; i < len1; i++){
            merge_word[j++] = word1[i];
        }
     
    }
    
    merge_word[len1+len2] = '\0';
    return merge_word;
}

给你一个数组 candies 和一个整数 extraCandies ,其中 candies[i] 代表第 i 个孩子拥有的糖果数目。

对每一个孩子,检查是否存在一种方案,将额外的 extraCandies 个糖果分配给孩子们之后,此孩子有 最多 的糖果。注意,允许有多个孩子同时拥有 最多 的糖果数目。

objectivec 复制代码
bool* kidsWithCandies(int* candies, int candiesSize, int extraCandies, int* returnSize) {
    int max_candies = 0;
    int i = 0;
    
    for(i = 0; i < candiesSize; i++){
        if(max_candies < candies[i]){
            max_candies = candies[i];
        }
    }

    bool *candies_res = malloc(sizeof(bool)*candiesSize);
    *returnSize = candiesSize;

    for(i = 0 ; i < candiesSize; i++){
        if(candies[i] + extraCandies >= max_candies){
            candies_res[i] = true;
        }
        else{
            candies_res[i] = false;
        }
    }

    return candies_res;
}

假设有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花不能种植在相邻的地块上,它们会争夺水源,两者都会死去。

给你一个整数数组 flowerbed 表示花坛,由若干 01 组成,其中 0 表示没种植花,1 表示种植了花。另有一个数 n,能否在不打破种植规则的情况下种入 n朵花?能则返回 true ,不能则返回 false

示例 1:

复制代码
输入:flowerbed = [1,0,0,0,1], n = 1
输出:true
objectivec 复制代码
bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n) {
    int unflower_count = 0;
    int i , j = 0;

    if(flowerbedSize ==1){
        if(!flowerbed[0]){
            j++;
        }
    }
    else{
        for(i = 0; i < flowerbedSize; i++){
            if(i == 0 && !flowerbed[i] && !flowerbed[i+1] ){
                j++;
                continue;
            }

            if(!flowerbed[i]){
                unflower_count ++;
            }
            else{
                unflower_count = 0;
            }

            if(unflower_count == 3){
                j++;
                i--;
                unflower_count = 0;
            }

            if(i == flowerbedSize-2 && flowerbed[i] == flowerbed[i+1] ){
                j++;
                break;
            }
        }
    }

    if(j >= n){
        return true;
    }
    return false;
}

给你一个字符串 s ,仅反转字符串中的所有元音字母,并返回结果字符串。

元音字母包括 'a''e''i''o''u',且可能以大小写两种形式出现不止一次。

示例 1:

复制代码
输入:s = "hello"
输出:"holle"
objectivec 复制代码
char* reverseVowels(char* s) {
    int len = strlen(s);
    char * word = malloc(len*sizeof(char)+1);
    unsigned i , j, count= 0;
   
    //memset(word_location, -1, len*sizeof(int));
    unsigned  * word_location = malloc(len*sizeof(unsigned));
    if(NULL == word_location){
        free(word);
        return NULL;
    }
    strcpy(word, s);
    for(i = 0 ; i < len; i ++){
        char lower_c = tolower(s[i]);
        if (lower_c == 'a' || lower_c == 'e' || lower_c == 'i' || lower_c == 'o' || lower_c == 'u') {
            word_location[count++] = i;
        }    
    } 

    if(count == 1){
        word[word_location[0]] = s[word_location[0]];
    }
    else{
        for(i = 0 ; i < count/2; i++){
            j = count -1-i;
            word[word_location[i]] = s[word_location[j]];
            word[word_location[j]] = s[word_location[i]];
        }
    }

    free(word_location);

    return word;
}
相关推荐
AlenTech34 分钟前
152. 乘积最大子数组 - 力扣(LeetCode)
算法·leetcode·职场和发展
橘颂TA2 小时前
【剑斩OFFER】算法的暴力美学——LeetCode 200 题:岛屿数量
算法·leetcode·职场和发展
苦藤新鸡2 小时前
14.合并区间(1,3)(2,5)=(1,5)
c++·算法·leetcode·动态规划
程序员-King.2 小时前
day145—递归—二叉树的右视图(LeetCode-199)
算法·leetcode·二叉树·递归
漫随流水2 小时前
leetcode算法(112.路径总和)
数据结构·算法·leetcode·二叉树
算法狗23 小时前
大模型中哪些模型用到的pre-norm和post-norm技术的?
人工智能·深度学习·机器学习·语言模型·面试题
程序员-King.4 小时前
day140—前后指针—删除排序链表中的重复元素Ⅱ(LeetCode-82)
数据结构·算法·leetcode·链表
Remember_9934 小时前
【JavaSE】一站式掌握Java面向对象编程:从类与对象到继承、多态、抽象与接口
java·开发语言·数据结构·ide·git·leetcode·eclipse
苦藤新鸡4 小时前
16.求数组除了当前元素的所有乘积
算法·leetcode·动态规划
Tisfy4 小时前
LeetCode 1895.最大的幻方:暴力中来点前缀和优化
算法·leetcode·前缀和·矩阵·题解·暴力