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;
}