链表内指定区间反转&&求路径&&合并区间

177 阅读1分钟

NC21 链表内指定区间反转

题目链接

1、解题思路

保证在区间内的使用头插法逆转,最后插在链表中就行。

2、代码
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param m int整型 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        if (head == null) {
            return null;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        for (int i = 1; i < m; i++) {
            pre = pre.next;
        }
        ListNode dummy2 = new ListNode(0);
        ListNode temp = pre.next;
        for (int i = m; i <= n; i++) {
            ListNode node = temp.next;
            temp.next = dummy2.next;
            dummy2.next = temp;
            temp = node;
        }
        pre.next = dummy2.next;
        ListNode t1 = dummy.next;
        while (t1.next != null) {
            t1 = t1.next;
        }
        t1.next = temp;
        return dummy.next;
    }
}

NC34 求路径

题目链接

1、解题思路

基本入门dp

2、代码
import java.util.*;


public class Solution {
    /**
     * 
     * @param m int整型 
     * @param n int整型 
     * @return int整型
     */
    public int uniquePaths (int m, int n) {
        // write code here
        // write code here
        int[][] dp = new int[m][n];
        dp[0][0] = 1;
        for (int i = 1; i < n; i++) {
            dp[0][i] = 1;
        }
        for (int i = 1; i < m; i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }
}

NC37 合并区间

题目链接

1、解题思路

首先按照起始优先、终端次之,从小到大排序。然后模拟处理就好。

2、代码
import java.util.*;
/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
        ArrayList<Interval> list = new ArrayList<>();
        if(intervals.size() == 0){
            return list;
        }
        Collections.sort(intervals, (o1, o2) -> {
            if (o1.start == o2.start) {
                return o1.end - o2.end;
            } else {
                return o1.start - o2.start;
            }
        });
        int l = intervals.get(0).start;
        int r = intervals.get(0).end;
        int index = 1;
        while (index < intervals.size()) {
            int tempL = intervals.get(index).start;
            if (tempL >= l && tempL <= r) {
                r = Math.max(intervals.get(index).end, r);
            } else {
                list.add(new Interval(l, r));
                l = tempL;
                r = intervals.get(index).end;
            }
            index++;
        }
        list.add(new Interval(l, r));
        return list;
    }
}