力扣刷题记录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;
}
相关推荐
追随者永远是胜利者4 小时前
(LeetCode-Hot100)253. 会议室 II
java·算法·leetcode·go
会周易的程序员4 小时前
cNetgate物联网网关内存数据表和数据视图模块架构
c语言·c++·物联网·架构·lua·iot
Jason_Honey24 小时前
【平安Agent算法岗面试-二面】
人工智能·算法·面试
爱编码的小八嘎4 小时前
第3章 Windows运行机理-3.1 内核分析(6)
c语言
宇木灵4 小时前
C语言基础-十、文件操作
c语言·开发语言·学习
程序员酥皮蛋4 小时前
hot 100 第三十五题 35.二叉树的中序遍历
数据结构·算法·leetcode
追随者永远是胜利者4 小时前
(LeetCode-Hot100)207. 课程表
java·算法·leetcode·go
仰泳的熊猫5 小时前
题目1535:蓝桥杯算法提高VIP-最小乘积(提高型)
数据结构·c++·算法·蓝桥杯
那起舞的日子6 小时前
动态规划-Dynamic Programing-DP
算法·动态规划
闻缺陷则喜何志丹6 小时前
【前后缀分解】P9255 [PA 2022] Podwyżki|普及+
数据结构·c++·算法·前后缀分解