力扣刷题记录3(无算法背景,纯C语言)

13、找字符串中所有字母异位词

cpp 复制代码
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* findAnagrams(char* s, char* p, int* returnSize) {
    * returnSize = 0;
    int pSlow = 0;
    int pFast = 0;
    int slen = strlen(s);
    int plen = strlen(p);
    int sHash[26] = {0};//原始字母的哈希表
    int pHash[26] = {0};//要找字母的哈希表
    int *returnsz = (int *)malloc(sizeof(int)*slen);
    //存入哈希表方便寻找
    for(int i = 0;i<plen;i++)
    {
        pHash[p[i]-'a']++;
    }
    for(pFast = 0; pFast < slen ;pFast++)
    {
        sHash[s[pFast]-'a']++;
        if(pFast - pSlow + 1 == plen)
        {
            int isAnagram  = 1;
            for(int i = 0;i<26;i++)
            {
                if(sHash[i] != pHash[i])
                {
                    isAnagram = 0;
                    break; 
                }
            }
            if(isAnagram)
            {
                returnsz[*returnSize] = pSlow;
                (*returnSize)++;
            }
            pSlow++;
            sHash[s[pFast-plen+1]-'a']--;
        }
    }
    return returnsz;
}

14、爬楼梯(70动态规划 记忆递归)

cpp 复制代码
int climbStairs(int n) 
{
    static int read[46] = {0};
    if(read[n] != 0)
    {
        return read[n];
    }
    if(n <= 2)return n;
    int a = climbStairs(n-1) + climbStairs(n-2);
    read[n] = a;
    return a;      
}

15、杨辉三角(118 动态规划,正常解)

cpp 复制代码
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** generate(int numRows, int* returnSize, int** returnColumnSizes) 
{
    * returnSize = numRows;
    int **returnarrays = (int **)malloc(sizeof(int*)*numRows);
    *returnColumnSizes = (int *)malloc(sizeof(int)*numRows);
    returnarrays[0] = (int*) malloc (sizeof(int)*1);
    returnarrays[0][0] = 1;
    returnColumnSizes[0][0] = 1;
    for(int i = 1; i< numRows; i++)
    {
        returnColumnSizes[0][i] = i + 1;
        returnarrays[i] = (int*) malloc (sizeof(int)*(i+1));
        returnarrays[i][0] = 1;
        for(int j = 1; j < i; j++)
        {
            returnarrays[i][j] = returnarrays[i-1][j] + returnarrays[i-1][j-1];
        }
        returnarrays[i][i] = 1;
    }
    return returnarrays;
}

15、只出现一次的数字(136技巧 异或)

cpp 复制代码
int singleNumber(int* nums, int numsSize) 
{
    int result = 0;
    for(int i = 0; i < numsSize ;i++)
    {
        result ^=  nums[i];
    }    
    return result;
}

16、多数元素(169技巧 投票法)

cpp 复制代码
int majorityElement(int* nums, int numsSize) 
{
    int count = 0;
    int val = 0;
    for(int i = 0; i < numsSize; i++)
    {
        if(count == 0)
        {
            val = nums[i];
            count++;
        }
        else if(val == nums[i])count++;
        else count--;
    }
    return val;
}
相关推荐
ValhallaCoder3 小时前
hot100-矩阵
数据结构·python·算法·矩阵
散峰而望3 小时前
【基础算法】穷举的艺术:在可能性森林中寻找答案
开发语言·数据结构·c++·算法·随机森林·github·动态规划
心.c4 小时前
Vue3+Node.js实现文件上传分片上传和断点续传【详细教程】
前端·javascript·vue.js·算法·node.js·哈希算法
散峰而望4 小时前
【基础算法】算法的“预谋”:前缀和如何改变游戏规则
开发语言·数据结构·c++·算法·github·动态规划·推荐算法
We་ct4 小时前
LeetCode 48. 旋转图像:原地旋转最优解法
前端·算法·leetcode·typescript
爱尔兰极光4 小时前
LeetCode--长度最小的子数组
算法·leetcode·职场和发展
仰泳的熊猫4 小时前
题目1432:蓝桥杯2013年第四届真题-剪格子
数据结构·c++·算法·蓝桥杯·深度优先·图论
有一个好名字4 小时前
力扣-电话号码组合
算法·leetcode·职场和发展
鱼跃鹰飞4 小时前
Leetcode会员尊享面试100题:1086:前五科的均分
算法·leetcode·职场和发展