自学内容网 自学内容网

代码随想录四刷day2

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


链表操作中,可以使用原链表来直接进行删除操作,也可以设置一个虚拟头结点再进行删除操作,接下来看一看哪种方式更方便。

一、力扣59. 螺旋矩阵 II

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] arr = new int[n][n];
        int left = 0, right = n-1, top = 0, low = n-1;
        for(int a = 1; a <= n * n; ){
            for(int i = left; i <= right && a <= n*n; i ++){
                arr[top][i] = a ++;
            }
            top ++;
            for(int i = top; i <= low && a <= n*n; i ++){
                arr[i][right] = a ++;
            }
            right --;
            for(int i = right; i >= left && a <= n*n; i --){
                arr[low][i] = a ++;
            }
            low --;
            for(int i = low; i >= top && a <= n*n; i --){
                arr[i][left] = a ++;
            }
            left ++;
        }
        return arr;
    }
}

二、力扣54. 螺旋矩阵

class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        List<Integer> res = new ArrayList<>();
        int left = 0, right = n-1, top = 0, low = m-1;
        int count = m * n;
        for(int a = 1; a <= count;){
            for(int i = left; i <= right && a <= count; i ++){
                res.add(matrix[top][i]);
                a ++;
            }
            top ++;
            for(int i = top; i <= low && a <= count; i ++){
                res.add(matrix[i][right]);
                a ++;
            }
            right --;
            for(int i = right; i >= left && a <= count; i --){
                res.add(matrix[low][i]);
                a ++;
            }
            low --;
            for(int i = low; i >= top && a <= count; i --){
                res.add(matrix[i][left]);
                a ++;
            }
            left ++;
        }
        
        return res;
    }
}

三、力扣LCR 146. 螺旋遍历二维数组

class Solution {
    public int[] spiralArray(int[][] array) {
        if(array.length == 0){
            return new int[0];
        }
        int m = array.length, n = array[0].length;
        int[] res = new int[m*n];
        int left = 0, right = n-1, top = 0, low = m-1;
        for(int a = 0; a < m*n; ){
            for(int i = left; i <= right && a < m*n; i ++){
                res[a ++] = array[top][i]; 
            }
            top ++;
            for(int i = top; i <= low && a < m*n; i ++){
                res[a ++] = array[i][right];
            }
            right --;
            for(int i = right; i >= left && a < m*n; i --){
                res[a ++] = array[low][i];
            }
            low --;
            for(int i = low; i >= top && a < m*n; i --){
                res[a ++] = array[i][left];
            }
            left ++;
        }
        return res;
    }
}

四、力扣203. 移除链表元素

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode res = new ListNode(-1,head);
        if(head == null){
            return res.next;
        }
        ListNode p = head, pre = res;
        while(p != null){
            if(p.val == val){
                pre.next = p.next;
                p = p.next;
            }else{
                p = p.next;
                pre = pre.next;
            }
        }
        return res.next;
    }
}

原文地址:https://blog.csdn.net/ResNet156/article/details/140264547

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!