过年了,好几天没刷了
31、除了自身以外数组的乘积(238 前缀和 数组)

cpp
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* productExceptSelf(int* nums, int numsSize, int* returnSize)
{
int *res = (int *)malloc(sizeof(int)*numsSize);
returnSize[0] = numsSize;
res[0] = 1;
for(int i = 1; i < numsSize; i++)
{
res[i] = res[i - 1] * nums[i - 1];
}
int temp = 1;
for(int i = numsSize - 1; i >= 0; i--)
{
res[i] *= temp;
temp *= nums[i];
}
return res;
}
32、岛屿数量(200 图论)

cpp
void waterFlood(char** grid, int i, int j, int gridSize, int* gridColSize)
{
if(i >= gridSize || i < 0 || j >= gridColSize[i] || j < 0)
{
return;
}
if(grid[i][j] != '1')
{
return;
}
grid[i][j] = '0';
waterFlood(grid, i-1, j, gridSize, gridColSize);
waterFlood(grid, i+1, j, gridSize, gridColSize);
waterFlood(grid, i, j-1, gridSize, gridColSize);
waterFlood(grid, i, j+1, gridSize, gridColSize);
}
int numIslands(char** grid, int gridSize, int* gridColSize)
{
int count = 0;
for(int i = 0; i<gridSize; i++)
{
for(int j = 0; j < gridColSize[i]; j++)
{
if(grid[i][j] == '1')
{
count++;
waterFlood(grid, i, j, gridSize, gridColSize);
}
}
}
return count;
}
33、两数相加(2链表)


cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
struct ListNode* temp = (struct ListNode*)malloc(sizeof(struct ListNode));
temp -> next= NULL;
temp -> val = 0;
struct ListNode* l3 = temp;
int flag = 0;
while(l1 != NULL || l2 != NULL || flag != 0)
{
int val1 = l1 != NULL ? l1 -> val : 0;
int val2 = l2 != NULL ? l2 -> val : 0;
int sum = val1 + val2 + flag;
flag = sum / 10;
l3 -> next = (struct ListNode*)malloc(sizeof(struct ListNode));
l3 -> next -> val = sum % 10;
l3 -> next -> next = NULL;
if(l1 != NULL)l1 = l1 -> next;
if(l2 != NULL)l2 = l2 -> next;
l3 = l3 -> next;
}
struct ListNode* result = temp->next;
return result;
}
34、删除链表倒数第N个节点(19链表)

cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeNthFromEnd(struct ListNode* head, int n)
{
int count = 1;
struct ListNode* result = head;
struct ListNode* temp = (struct ListNode *)malloc(sizeof(struct ListNode));
temp -> val = 0;
temp -> next = head;
while(head -> next != NULL)
{
head = head -> next;
count++;
if(count > n)
{
temp = temp -> next;
}
}
//head被删除的情况,返回原来表头的下一位
if(count == n)
{
return result -> next;
}
//链表最后一个元素的情况
if(temp -> next == NULL || temp -> next -> next == NULL)
{
temp -> next = NULL;
}
else
{
temp -> next = temp -> next -> next;
}
//正常返回原来表头
return result;
}
35、两两交换链表中的节点(24 链表递归)

cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
void swap(struct ListNode* head)
{
if(head != NULL && head -> next != NULL)
{
struct ListNode* headn = head -> next;
if(headn -> next != NULL)
{
struct ListNode* headnn = headn -> next;
if(headnn -> next != NULL)
{
struct ListNode* headnnn = headnn -> next;
headn -> next = head;
head -> next = headnnn;
swap(headnn);
return;
}
headn -> next = head;
head -> next = headnn;
return;
}
headn -> next = head;
head -> next = NULL;
return;
}
return;
}
struct ListNode* swapPairs(struct ListNode* head)
{
if(head != NULL && head -> next != NULL)
{
struct ListNode* headn = head -> next;
swap(head);
return headn;
}
return head;
}
简洁解
cpp
struct ListNode* swapPairs(struct ListNode* head) {
// 1. 终止条件:如果没有节点或只有一个节点,无需交换
if (head == NULL || head->next == NULL) {
return head;
}
// 2. 设定要交换的两个节点
struct ListNode* firstNode = head;
struct ListNode* secondNode = head->next;
// 3. 递归处理后续链表,并连接到当前 pair 的末尾
// firstNode 交换后会变成第二个,所以它的 next 指向后面交换完的结果
firstNode->next = swapPairs(secondNode->next);
// 4. 完成当前的交换
secondNode->next = firstNode;
// 5. 返回交换后的新头节点(即原本的第二个节点)
return secondNode;
}
36、二叉树层序遍历(102 队列)

cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 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** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes)
{
*returnSize = 0;
if(root == NULL)return NULL;
int** result = (int**) malloc(sizeof(int*)*2000);
*returnColumnSizes = (int*) malloc(sizeof(int)*2000);
int head = 0;
int tail = 0;
struct TreeNode *queue[2000];
queue[head++] = root;
while(head > tail)
{
int levelSize = head - tail;
result[*returnSize] = (int*) malloc(sizeof(int)*levelSize);
(*returnColumnSizes)[*returnSize] = levelSize;
for(int i = 0; i < levelSize; i++)
{
(result)[*returnSize][i] = queue[tail] -> val;
if(queue[tail] -> left)queue[head++] = queue[tail] -> left;
if(queue[tail] -> right)queue[head++] = queue[tail] -> right;
tail++;
}
(*returnSize)++;
}
return result;
}