力扣刷题记录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;
}
相关推荐
七七肆十九6 分钟前
PTA 习题9-1 时间换算
c语言·算法
XW010599910 分钟前
5-6统计工龄
数据结构·python·算法
EQUINOX116 分钟前
倍增优化dp,P10976 统计重复个数
算法·数学建模·动态规划
样例过了就是过了20 分钟前
LeetCode热题100 电话号码的字母组合
数据结构·c++·算法·leetcode·dfs
nervermore99021 分钟前
1.10 面试经典150题-多数元素
算法
c++逐梦人22 分钟前
二分查找模版及二分答案例题
算法·蓝桥杯
biubiuibiu30 分钟前
选择适合的硬盘:固态与机械硬盘的对比与推荐
c++·算法
big_rabbit050235 分钟前
[算法][力扣226]翻转一颗二叉树
数据结构·算法·leetcode
TracyCoder12338 分钟前
LeetCode Hot100(65/100)——64. 最小路径和
算法·leetcode·职场和发展
z2014z39 分钟前
Deflate 算法详解
网络·算法