LeetCode vuelve a luchar contra la "oferta de Sword Finger", la lista vinculada y la programación dinámica (2)

Hola a todos, soy方圆


Lista enlazada

Sword se refiere a la Oferta 06. Imprima la lista enlazada de principio a fin

Inserte la descripción de la imagen aquí

class Solution {
    
    
    ArrayList<Integer> temp = new ArrayList<>();

    public int[] reversePrint(ListNode head) {
    
    
        recur(head);

        int[] res = new int[temp.size()];
        for(int i = 0;i < temp.size();i++)
            res[i] = temp.get(i);
        
        return res;
    }

    private void recur(ListNode head){
    
    
        if(head == null) return;

        recur(head.next);
        temp.add(head.val);
    }
}

Sword se refiere a la Oferta 18. Elimina el nodo de la lista vinculada

Inserte la descripción de la imagen aquí

class Solution {
    
    
    public ListNode deleteNode(ListNode head, int val) {
    
    
        if(head.val == val) return head.next;

        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null){
    
    
            if(cur.val == val){
    
    
                pre.next = cur.next;
                return head;
            }
            pre = cur;
            cur = cur.next;
        }

        return head;
    }
}

Sword se refiere a la Oferta 22. El k-ésimo nodo de la parte inferior de la lista vinculada

Inserte la descripción de la imagen aquí

class Solution {
    
    
    public ListNode getKthFromEnd(ListNode head, int k) {
    
    
        ListNode temp = head;
        while(k != 0){
    
    
            temp = temp.next;
            k--;
        }

        while(temp != null){
    
    
            head = head.next;
            temp = temp.next;
        }

        return head;
    }
}

Sword se refiere a la Oferta 24. Lista enlazada inversa

Inserte la descripción de la imagen aquí

class Solution {
    
    
    public ListNode reverseList(ListNode head) {
    
    
        ListNode cur = head;
        ListNode pre = null;
        ListNode temp = null;

        while(cur != null){
    
    
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }

        return pre;
    }
}

Sword se refiere a la Oferta 35. Copia de lista compleja vinculada

Inserte la descripción de la imagen aquí

class Solution {
    
    
    HashMap<Node,Node> visited = new HashMap<>();

    public Node copyRandomList(Node head) {
    
    
        if(head == null) return null;

        if(visited.containsKey(head))
            return visited.get(head);
        Node node = new Node(head.val);
        visited.put(head,node);

        node.next = copyRandomList(head.next);
        node.random = copyRandomList(head.random);

        return node;
    }
}

Sword se refiere a la Oferta 52. El primer nodo común de dos listas vinculadas

Inserte la descripción de la imagen aquí

public class Solution {
    
    
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    
    
        int lengthA = getLength(headA);
        int lengthB = getLength(headB);

        if(lengthA > lengthB){
    
    
            for(int i = 0;i < lengthA - lengthB;i++)
                headA = headA.next;
        }else{
    
    
            for(int j = 0;j < lengthB - lengthA;j++)
                headB = headB.next;
        }

        while(headA != headB){
    
    
            headA = headA.next;
            headB = headB.next;
        }

        return headA;
    }

    private int getLength(ListNode head){
    
    
        int length = 0;
        while(head != null){
    
    
            head = head.next;
            length++;
        }
        return length;
    }
}

Programación dinámica

La espada se refiere a la Oferta 12. El camino en la matriz

Inserte la descripción de la imagen aquí

class Solution {
    
    
    public boolean exist(char[][] board, String word) {
    
    
        char[] words = word.toCharArray();

        for(int i = 0;i < board.length;i++){
    
    
            for(int j = 0;j < board[0].length;j++){
    
    
                if(dfs(board,words,i,j,0))
                    return true;
            }
        }

        return false;
    }

    private boolean dfs(char[][] board,char[] words,int i,int j,int k){
    
    
        if(i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != words[k])
            return false;
        if(k == words.length - 1)
            return true;
        
        char temp = board[i][j];
        board[i][j] = ' ';

        boolean res = (dfs(board,words,i + 1,j,k + 1) || dfs(board,words,i - 1,j,k + 1)
                    || dfs(board,words,i,j + 1,k + 1) || dfs(board,words,i,j - 1,k + 1));
        
        board[i][j] = temp;

        return res;
    }
}

Sword se refiere a la Oferta 42. Suma máxima de submatrices consecutivas

Inserte la descripción de la imagen aquí

class Solution {
    
    
    public int maxSubArray(int[] nums) {
    
    
        int res = nums[0];
        int sum = 0;

        for(int num : nums){
    
    
            if(sum > 0){
    
    
                sum += num;
            }else{
    
    
                sum = num;
            }
            res = Math.max(sum,res);
        }

        return res;
    }
}

Espada se refiere a la Oferta 47. El mayor valor de los regalos

Inserte la descripción de la imagen aquí

class Solution {
    
    
    public int maxValue(int[][] grid) {
    
    
        int row = grid.length,col = grid[0].length;

        //初始化第一行
        for(int i = 1;i < col;i++)
            grid[0][i] += grid[0][i - 1];
        //初始化第一列
        for(int j = 1;j < row;j++)
            grid[j][0] += grid[j - 1][0];

        //原地变换
        for(int i = 1;i < row;i++){
    
    
            for(int j = 1;j < col;j++){
    
    
                grid[i][j] += Math.max(grid[i][j - 1],grid[i - 1][j]);
            }
        }

        return grid[row - 1][col - 1];
    }
}

La espada se refiere a la Oferta 63. El beneficio máximo de la acción

Inserte la descripción de la imagen aquí

class Solution {
    
    
    public int maxProfit(int[] prices) {
    
    
        int minPrice = Integer.MAX_VALUE;
        int res = 0;

        for(int i = 0;i < prices.length;i++){
    
    
            if(minPrice > prices[i])
                minPrice = prices[i];
            else
                res = Math.max(res,prices[i] - minPrice);
        }

        return res;
    }
}

¡Venga!

Supongo que te gusta

Origin blog.csdn.net/qq_46225886/article/details/107616420
Recomendado
Clasificación