力扣刷题记录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;
}
相关推荐
晓蛋3 天前
c语言指的是什么意思
c语言·编译器·编程开发·集成开发环境·程序实例
倒头就睡的小比特3 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
傲世仙尊3 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
牵猫散步的鱼儿3 天前
重载、重写(覆盖)、重定义区别
c语言
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
phltxy3 天前
C 语言指针:从内存地址到灵活的数据访问
c语言
phltxy3 天前
C 语言中的数据存储:从类型到二进制位
c语言
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark3 天前
大规模并行计算中的负载均衡算法研究4
算法