LeetCode再战《剑指Offer》,链表和动态规划(二)

大家好,我是方圆

题号

  • 链表
    • 剑指 Offer 06. 从尾到头打印链表
    • 剑指 Offer 18. 删除链表的节点
    • 剑指 Offer 22. 链表中倒数第k个节点
    • 剑指 Offer 24. 反转链表
    • 剑指 Offer 35. 复杂链表的复制
    • 剑指 Offer 52. 两个链表的第一个公共节点
  • 动态规划
    • 剑指 Offer 12. 矩阵中的路径
    • 剑指 Offer 42. 连续子数组的最大和
    • 剑指 Offer 47. 礼物的最大价值
    • 剑指 Offer 63. 股票的最大利润


链表

剑指 Offer 06. 从尾到头打印链表

LeetCode再战《剑指Offer》,链表和动态规划(二)_第1张图片

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);
    }
}

剑指 Offer 18. 删除链表的节点

LeetCode再战《剑指Offer》,链表和动态规划(二)_第2张图片

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;
    }
}

剑指 Offer 22. 链表中倒数第k个节点

LeetCode再战《剑指Offer》,链表和动态规划(二)_第3张图片

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;
    }
}

剑指 Offer 24. 反转链表

LeetCode再战《剑指Offer》,链表和动态规划(二)_第4张图片

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;
    }
}

剑指 Offer 35. 复杂链表的复制

LeetCode再战《剑指Offer》,链表和动态规划(二)_第5张图片

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;
    }
}

剑指 Offer 52. 两个链表的第一个公共节点

LeetCode再战《剑指Offer》,链表和动态规划(二)_第6张图片

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;
    }
}

动态规划

剑指 Offer 12. 矩阵中的路径

LeetCode再战《剑指Offer》,链表和动态规划(二)_第7张图片

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;
    }
}

剑指 Offer 42. 连续子数组的最大和

LeetCode再战《剑指Offer》,链表和动态规划(二)_第8张图片

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;
    }
}

剑指 Offer 47. 礼物的最大价值

LeetCode再战《剑指Offer》,链表和动态规划(二)_第9张图片

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];
    }
}

剑指 Offer 63. 股票的最大利润

LeetCode再战《剑指Offer》,链表和动态规划(二)_第10张图片

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;
    }
}

加油!

你可能感兴趣的:(LeetCode,链表,payment,ext,opera,websphere)