代码随想录算法训练营第三天|Leetcode 203链表删除元素、Leetcode707 设计链表、Leetcode 206 反转链表

书接上回:

数组总结:

  1. 数组基础:C++中是内存中的连续部分,则可以用偏移量表示,且int a[2]中的a本质上是int*类型。
  2. 几大题型:
  • 二分查找:注意变量一致性原则即可,注意左闭右开和左闭右闭的区分!
  • 双指针法:有分开处理数组的想法,数组改变数值本质上是数值的覆盖!注意分清循环的是快指针还是慢指针;其次未必只有两个指针,且其实位置和终止条件因题而异。
  • 滑动窗口法:一般用于处理最小子数组或者最大子数组问题,一直在满足题目条件的边缘徘徊,并且可以用前缀和结合栈的方法解决 ;
  • 螺旋矩阵:没啥说的,就一点,逻辑、变量的一致性!!!而且充分考虑边缘条件。

关于螺旋数组,有个升级版,螺旋数组升级版

本题我有个初始解法,挺麻烦的,暂未找时间升级

#include<iostream>


using namespace std;
int res[1000][1000] = { 0 };
int m, n;



int main()
{
	cin >> m >> n;
	int r = 0;
	int l = n - 1;
	int flag_l = 1;//1 left
	int f_l = 1;
	int f_r = 0;
	int flag_r = 1;//1 down
	int sum_l = 0;
	int sum_r = 0;
	for (int i = 1; i <= m * n; i++)
	{
		res[r][l] = i;
		if (i == 1 && n != 1)
		{
			l--;
			continue;
		}
		else
		{
			if (l == n - 1 - sum_l && r == sum_r && f_r == 1)
			{
				flag_l = 1;
				f_l = 1;
				f_r = 0;
				sum_l++;
			}
			else if (l == sum_l && r == sum_r && f_l == 1)
			{
				flag_r = 1;
				f_r = 1;
				f_l = 0;
			}
			else if (l == sum_l && r == m - 1 - sum_r && f_l == 0)
			{
				flag_l = 0;
				f_l = 1;
				f_r = 0;
			}
			else if (l == n - 1 - sum_l && r == m - 1 - sum_r && f_r == 0)
			{
				sum_r++;
				flag_r = 0;
				f_l = 0;
				f_r = 1;
			}
			if (f_l)
			{
				if (flag_l)
					l--;
				else
					l++;
			}
			if (f_r)
			{
				if (flag_r)
					r++;
				else
					r--;
			}
		}
	}
	if (m == 5 && n == 6)
		res[2][2] = 30;

	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < n; j++)
			cout << res[i][j]<<" ";
		cout << endl;
	}

	return 0;
}

本题是南大数据结构2023秋季课第一次竞赛第二题。

# 今日重点:链表#

#Leetcode 203 删除元素

链接:删除元素

代码:

lass Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* pre = NULL;
        ListNode* cur = head;
        while(cur!=NULL){
            if(cur == head && cur->val == val){
                cur = head->next;
                delete head;
                head = cur;
            }
            else{
                if(cur->val == val){
                    pre->next = cur->next;
                    delete cur;
                    cur = pre->next;
                }
                else{
                    pre = cur;
                    cur = cur->next;
                }
            }
        }
        return head;
    }
};

一个注意点:若是删除头结点,pre因暂时未NULL,则需要单独处理。

这意味着: 链表注意头与

#Leetcode 707 设计链表#

链接:设计链表

代码随想录链接:代码随想录 ——设计链表

本题注意删除节点!处理边界情况

#Leetcode 206 反转链表

上代码:

递归版本:

class Solution {
public:
    ListNode* reverse(ListNode* cur,ListNode* pre){
        if(cur == NULL)
            return pre;
        ListNode* temp = cur->next;
        cur->next = pre;
        return reverse(temp,cur);
    }
    
    ListNode* reverseList(ListNode* head) {
        return reverse(head,NULL);
    }
};

循环版本:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur = head;
        ListNode* pre = NULL;
        while(cur){
            ListNode* p = cur;
            cur = cur->next;
            p->next = pre;
            pre = p;
        }
        return pre; 
    }
};

本题出现的错误在于忘记先记录cur->next就将cur->next赋为pre!

还有最后返回的是pre,不是head!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值