翻转链表
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* prev = NULL;
struct ListNode* curr = head;
while (curr != NULL) {
struct ListNode* nextTemp = curr->next;//nextTemp的作用是为了记录,以便再反转后可以
curr->next = prev; // //找到下一个结点
prev = curr;
curr = nextTemp;
}
return prev;
}
struct ListNode* midfind(struct ListNode* head) { //用快慢指针找中间节点
struct ListNode* slow = head;
struct ListNode* fast = head;
while (fast->next!= NULL && fast->next->next != NULL) { // 修改了循环条件
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
struct ListNode* reverseList(struct ListNode* head) { //翻转链表
struct ListNode* pre = NULL;
struct ListNode* cur = head;
while (cur != NULL) {
struct ListNode* temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
struct ListNode* mergeList(struct ListNode* l1, struct ListNode* l2) { //合并链表
struct ListNode dummy;
struct ListNode* tail = &dummy;
while (l1 != NULL && l2 != NULL) {
tail->next = l1;
l1 = l1->next;
tail = tail->next;
tail->next = l2;
l2 = l2->next;
tail = tail->next;
}
if (l1 != NULL) {
tail->next = l1;
} else {
tail->next = l2;
}
return dummy.next;
}
void reorderList(struct ListNode* head) {
if (head == NULL)
return;
struct ListNode* mid = midfind(head);
struct ListNode* l1 = head;
struct ListNode* l2 = mid->next;
mid->next = NULL;
l2 = reverseList(l2);
head = mergeList(l1, l2); // 修改了函数返回的用法
}
//暴力求解(时间超限)
int minSubArrayLen(int s, int* nums, int numsSize) {
if (numsSize == 0) {
return 0;
}
int ans = INT_MAX;
for (int i = 0; i < numsSize; i++) {
int sum = 0;
for (int j = i; j < numsSize; j++) {
sum += nums[j];
if (sum >= s) {
ans = fmin(ans, j - i + 1);
break;
}
}
}
return ans == INT_MAX ? 0 : ans;
}
//滑动窗口
int minSubArrayLen(int target, int* nums, int numsSize) {
int result=INT_MAX;
int ans=0;int sum=0;int i=0;
for(int j=0;j<numsSize;j++)
{
sum+=nums[j];
while(sum>=target)
{
ans=j-i+1;
result=fmin(result,ans);
sum=sum-nums[i];
i++;
}
}
return result==INT_MAX?0:result;
}
滑动窗口讲解视频参考:【拿下滑动窗口! | LeetCode 209 长度最小的子数组-哔哩哔哩】 https://b23.tv/q4YearN
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int t = 0; // top
int b = n-1; // bottom
int l = 0; // left
int r = n-1; // right
vector<vector<int>> ans(n,vector<int>(n));
int k=1;
while(k<=n*n){
for(int i=l;i<=r;++i,++k) ans[t][i] = k;
++t;
for(int i=t;i<=b;++i,++k) ans[i][r] = k;
--r;
for(int i=r;i>=l;--i,++k) ans[b][i] = k;
--b;
for(int i=b;i>=t;--i,++k) ans[i][l] = k;
++l;
}
return ans;
}
};
int** ans = (int**)malloc(sizeof(int*) * n);
for (int i = 0; i < n; ++i) {
ans[i] = (int*)malloc(sizeof(int) * n);
}
这段代码是为了在 C 中动态分配内存来创建一个二维数组 ans
,其大小为 n x n
。
首先,通过 malloc
分配了一个指向指针的内存块,大小为 n * sizeof(int*)
,这表示 ans
的每一行都是一个指向整数的指针。
然后,使用 for
循环遍历 n
行,对于每一行,再次使用 malloc
来分配大小为 n * sizeof(int)
的内存块,这表示每一行有 n
个整数。
最终,ans
就是一个指向指针的指针,每个指针指向一个大小为 n
的整数数组,形成了一个 n x n
的二维数组。
这样做的结果是,我们得到了一个动态分配的二维数组,可以使用它来存储矩阵中的元素。
struct ListNode* removeElements(struct ListNode* head, int val) {
struct ListNode* dummyHead = malloc(sizeof(struct ListNode));
dummyHead->next = head;
struct ListNode* temp = dummyHead;
while (temp->next != NULL) {
if (temp->next->val == val) {
temp->next = temp->next->next;
} else {
temp = temp->next;
}
}
return dummyHead->next;
}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
typedef struct {
struct ListNode *head;
int size;
} MyLinkedList;
struct ListNode *ListNodeCreat(int val) {
struct ListNode * node = (struct ListNode *)malloc(sizeof(struct ListNode));
node->val = val;
node->next = NULL;
return node;
}
MyLinkedList* myLinkedListCreate() {
MyLinkedList * obj = (MyLinkedList *)malloc(sizeof(MyLinkedList));
obj->head = ListNodeCreat(0);
obj->size = 0;
return obj;
}
int myLinkedListGet(MyLinkedList* obj, int index) {
if (index < 0 || index >= obj->size) {
return -1;
}
struct ListNode *cur = obj->head;
for (int i = 0; i <= index; i++) {
cur = cur->next;
}
return cur->val;
}
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
if (index > obj->size) {
return;
}
index = MAX(0, index);
obj->size++;
struct ListNode *pred = obj->head;
for (int i = 0; i < index; i++) {
pred = pred->next;
}
struct ListNode *toAdd = ListNodeCreat(val);
toAdd->next = pred->next;
pred->next = toAdd;
}
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
myLinkedListAddAtIndex(obj, 0, val);
}
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
myLinkedListAddAtIndex(obj, obj->size, val);
}
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
if (index < 0 || index >= obj->size) {
return;
}
obj->size--;
struct ListNode *pred = obj->head;
for (int i = 0; i < index; i++) {
pred = pred->next;
}
struct ListNode *p = pred->next;
pred->next = pred->next->next;
free(p);
}
void myLinkedListFree(MyLinkedList* obj) {
struct ListNode *cur = NULL, *tmp = NULL;
for (cur = obj->head; cur;) {
tmp = cur;
cur = cur->next;
free(tmp);
}
free(obj);
}