算法复习(day1,2,3):复习数组、链表(1)

复习(day1,2,3)

1.二分查找

2.快慢指针、对撞指针、滑动窗口

3.螺旋矩阵I和Ⅱ

看了一种非常好记好写好检查错误的方法,核心思想是通过更新l,r,u,b,实现“删除已经遍历的行/列”
54. 螺旋矩阵,遍历:

class Solution {
    //已经使用过了,可以将其从图中删去,体现在代码中就是重新定义
    public List<Integer> spiralOrder(int[][] matrix) {
        int l = 0;
        int r = matrix[0].length - 1;
        int u = 0;
        int b = matrix.length - 1;
        List<Integer> res = new LinkedList<>();
        while (true){
            for(int i = l; i <= r; i++) res.add(matrix[u][i]);
            u++;
            if(u > b) break;
            for(int i = u; i <= b; i++ ) res.add(matrix[i][r]);
            r--;
            if(l > r) break;
            for(int i = r; i >= l; i--) res.add(matrix[b][i]);
            b--;
            if(u > b) break;
            for(int i = b; i >= u; i--) res.add(matrix[i][l]);
            l++;
            if(l > r) break;
        }
        return res;
    }
}

赋值:

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];
        int l = 0;
        int r = n -1;
        int u = 0;
        int b = n - 1;
        int num = 1;
        while (true) {
            for (int i = l; i <= r; i++) {
                res[u][i] = num;
                num ++;
            }
            u++;
            if (u > b) break;
            for (int i = u; i <= b; i ++) {
                res[i][r] = num;
                num ++;
            }
            r--;
            if(l > r) break;
            for (int i = r; i >= l; i --) {
                res[b][i] = num;
                num++;
            }
            b--;
            if(u > b) break;
            for(int i = b; i >= u; i --) {
                res[i][l] = num;
                num ++;
            }
            l++;
            if(l > r) break;
        } 
        return res;
    }
}

4.两两交换链表中的节点——隔一个箭头换,完成一次转换仍然是连接着

在这里插入图片描述

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(-1, head);
        ListNode cur = dummy;
        //交换两个一组进行,影响四个节点
        //怎么解决null的问题呢&& cur.next.next != null,通过下面的情况设置
        while (cur.next != null && cur.next.next != null) {
            ListNode tmp1 = cur.next;
            ListNode tmp2 = cur.next.next.next;

            cur.next = cur.next.next;
            cur.next.next = tmp1;
            cur.next.next.next = tmp2;

            //更新
            cur = cur.next.next;
        }
        return dummy.next;
    }
}

5.反转链表:递归和迭代法——每一个箭头换,完成一次转换是断开的状态(两个链),需要双指针

在这里插入图片描述
迭代法:完成一次转换是断开的状态(两个链)

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode pre = null;
        while (cur != null) {
            ListNode tmp = cur.next;

            cur.next = pre;
            pre = cur;
            cur = tmp;
            //完成一次转换是断开的状态(两个链)
        }
        return pre;//返回的是pre,cur此时是null
    }
}

递归法:
cur每次递归复制不变,是最后一个节点
从后到前反转

class Solution {
	public ListNode reverseList(ListNode head) {
		//递归终止条件是当前为空,或者下一个节点为空
		if(head==null || head.next==null) {
			return head;
		}
        
		//这里的cur就是最后一个节点
		ListNode cur = reverseList(head.next);

        //核心
            //反转指针
		head.next.next = head;
		    //防止链表循环,需要将head.next设置为空
		head.next = null;

		//每层递归函数都返回cur,也就是最后一个节点——只是为了传递
		return cur;
	}
}

6.K个一组反转链表

迭代法反转链表双指针

class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;

    ListNode pre = dummy;

    ListNode end = dummy;

    while (end.next != null) {
        //k个一组计算end
        for (int i = 0; i < k && end != null; i++) end = end.next;
        if (end == null) break;

        ListNode start = pre.next;
        ListNode next = end.next;

        //断链
        end.next = null;

        //此时start被改变了
        pre.next = reverse(start);

        //重新连上
        start.next = next;

        //更新pre和end
        pre = start;
        end = pre;
    }
    return dummy.next;
}

private ListNode reverse(ListNode head) {
    ListNode pre = null;
    ListNode curr = head;
    while (curr != null) {
        ListNode next = curr.next;
        curr.next = pre;
        pre = curr;
        curr = next;
    }
    return pre;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值