18、最大子数组和

一开始写错了(对和进行了跨越相加,而不是抛弃重新开始)
cpp
int maxSubArray(int* nums, int numsSize)
{
int sum = 0;//当前值
int if_have_sum = 0;//如果有的话的值
sum = nums[0];
if_have_sum = sum;
for(int i =1; i < numsSize; i++)
{
if(nums[i] >= sum)//且大于当前最大和
{
if(if_have_sum > 0)
{
sum = if_have_sum + nums[i];
if_have_sum = sum;
}
else
{
sum = nums[i];
if_have_sum = sum;
}
}
else if(sum <= nums[i]+if_have_sum)//和比之前加了负值大
{
sum = nums[i]+if_have_sum;
if_have_sum = sum;
}
else
{
if_have_sum = nums[i] + if_have_sum;
}
}
return sum;
}
后面加了一个globle参数进行历史记录,后边进行重新开始合计
cpp
int maxSubArray(int* nums, int numsSize)
{
int curren_sum = nums[0];
int globle_sum = curren_sum;
for(int i = 1; i < numsSize; i++)
{
curren_sum = (nums[i] > (curren_sum + nums[i])) ? nums[i] : curren_sum + nums[i];//注意需要更新和,不能直接跳过
globle_sum = (curren_sum > globle_sum) ? curren_sum : globle_sum;
}
return globle_sum;
}
19、环形链表二(142链表 双指针)

注意while(Fast->next != NULL && Fast != NULL) 判断顺序写反
cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head)
{
if(head == NULL)return NULL;
struct ListNode *Fast = head;
struct ListNode *Slow = head;
while(Fast != NULL && Fast->next != NULL )
{
Fast = Fast->next ->next;
Slow = Slow -> next;
if(Fast == Slow)
{
Slow = head;
while(Fast != Slow)
{
Slow = Slow -> next;
Fast = Fast -> next;
}
return Slow;
}
}
return NULL;
}
20、合并两个有序链表(21链表)

每次只比较最小值,使用一个中间变量记录头
cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
struct ListNode temp;
struct ListNode* head = &temp;
temp.next = NULL;
while(list1 != NULL && list2 != NULL)
{
if(list1->val <= list2->val)
{
head -> next = list1;
list1 = list1 -> next;
}
else
{
head -> next = list2;
list2 = list2 -> next;
}
head = head -> next;
}
if(list1 == NULL)
{
head -> next = list2;
}
else
{
head -> next = list1;
}
return temp.next;
}
21、不同路径(62动态规划阶乘)

注意要边乘边除以,不然溢出很难搞(longlong),还要注意除时的分数问题(float),
cpp
int uniquePaths(int m, int n)
{
long long result = 1;
if(m > n)
{
int temp = m;
m = n;
n = temp;
}
for(int i = n; i< m+n -1 ;i++)
{
result *= i;
result /= (i-n+1);
}
return (int)result;
}
22、二叉树直径(543递归)

递归到底层 每次+1 对比左右深度取max(需要初始化max = 0)
cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int max = 0;
int get_deepth(struct TreeNode* root)
{
if(root == NULL)
{
return 0;
}
int leftdeep = get_deepth(root -> left);
int rightdeep = get_deepth(root -> right);
max = max > leftdeep + rightdeep ? max : leftdeep + rightdeep;
leftdeep = leftdeep>rightdeep?leftdeep:rightdeep;
return leftdeep + 1;
}
int diameterOfBinaryTree(struct TreeNode* root)
{
max = 0;
get_deepth(root);
return max;
}
23、颜色分类(75技巧 三指针)

注意[2] [2,1,2]单个数值和首尾相同的情况
cpp
void sortColors(int* nums, int numsSize)
{
int pZero = 0;
int pRemove = 0;
int pTwo = numsSize - 1;
if(numsSize == 1)return;
for(; pRemove<numsSize; pRemove++)
{
if(nums[pRemove] == 2)
{
while(nums[pTwo] == 2)
{
pTwo--;
if(pTwo < pRemove)return;
}
int temp = nums[pTwo];
nums[pTwo] = 2;
pTwo--;
nums[pRemove] = temp;
}
if(nums[pRemove] == 0)
{
int temp = nums[pZero];
nums[pZero] = 0;
pZero++;
nums[pRemove] = temp;
}
if(pTwo <= pRemove)return;
}
}
24、寻找重复数(287 技巧 我用的哈希表)

cpp
int findDuplicate(int* nums, int numsSize)
{
int *hashmap = (int *)malloc(sizeof(int)*(numsSize));
int result = -1;
memset(hashmap,-1,sizeof(int)*(numsSize));
for(int i = 0; i<numsSize;i++)
{
if(hashmap[nums[i]] != -1)
{
result = nums[i];
break;
}
hashmap[nums[i]] = 1;
}
return result;
}
24、买股票的最佳时机(121 贪心)

cpp
int maxProfit(int* prices, int pricesSize)
{
int pLow = 0;
int pHigt = 0;
int max = 0;
for(int i = 0;i<pricesSize;i++)
{
if(prices[i] - prices[pLow] > max)
{
max = prices[i] - prices[pLow];
pHigt = i;
}
else if(prices[i] < prices[pLow])
{
pLow = i;
}
}
return max;
}