前言

本文隶属于专栏《LeetCode 刷题汇总》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!

本专栏目录结构请见LeetCode 刷题汇总

正文

幕布

在这里插入图片描述

幕布链接

56. 合并区间

题解

Above solution with modified input

cur,prev

class Solution {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
        List<int[]> ret = new ArrayList<>();
        int[] prev = null;
        for (int[] inter : intervals) {
            //if prev is null or curr.start > prev.end, add the interval
            if (prev==null || inter[0] > prev[1]) {
                ret.add(inter);
                prev = inter;
            } else if (inter[1] > prev[1]) {
                // curr.end > prev.end, modify the element already in list
                prev[1] = inter[1];
            }
        }
        return ret.toArray(new int[ret.size()][2]);
    }
}

57. 插入区间

题解

Almost the same compared with problem 56.

List,pre[1] < cur[0],pre[0] > cur[1]

class Solution {
    public int[][] insert(int[][] intervals, int[] newInterval) {
        List<int[]> list = new ArrayList<>();
        int[] pre = newInterval;
        for(int[] cur : intervals){
            if(pre[1] < cur[0]){
                list.add(pre);
                pre = cur;
            }else if(pre[0] > cur[1]){
                list.add(cur);
            }else{
                pre[0] = Math.min(pre[0], cur[0]);
                pre[1] = Math.max(pre[1], cur[1]);
            }
        }
        list.add(pre);
        return list.toArray(new int[0][0]);
    }
}

58. 最后一个单词的长度

题解

Java 0ms | 100% | 100% Single Reverse loop with explanation

← 遍历

class Solution {
    // O(s.length()) time, O(1) space, beats 100%.
    public int lengthOfLastWord(String s) {
        if (s == null || s.length() == 0) return 0;
        int result = 0, i = s.length() - 1;
        while (i >= 0 && s.charAt(i) == ' ') i--;   // Skip all whitespaces at the end of s.
        while (i >= 0 && s.charAt(i) != ' ') {      // Count the number of consecutive non-whitespace characters.
            result++;
            i--;
        }
        return result;
    }
}

lastIndexOf

class Solution {
    public int lengthOfLastWord(String s) {
        return s.trim().length() - s.trim().lastIndexOf(" ") - 1;
    }
}

59. 螺旋矩阵 II

题解

My Super Simple Solution. Can be used for both Spiral Matrix I and II

→↓←↑

class Solution {
    public static int[][] generateMatrix(int n) {
        int[][] ret = new int[n][n];
        int left = 0, top = 0;
        int right = n -1, down = n - 1;
        int count = 1;
        while (left <= right) {
            for (int j = left; j <= right; j++) {
                ret[top][j] = count++;
            }
            top++;
            for (int i = top; i <= down; i++) {
                ret[i][right] = count++;
            }
            right--;
            for (int j = right; j >= left; j--) {
                ret[down][j] = count++;
            }
            down--;
            for (int i = down; i >= top; i--) {
                ret[i][left] = count++;
            }
            left++;
        }
        return ret;
    }
}

60. 排列序列

题解

“Explain-like-I’m-five” Java Solution in O(n)

sb+list

public class Solution {
    public String getPermutation(int n, int k) {
        StringBuilder sb = new StringBuilder();
        List<Integer> num = new ArrayList<>();
        int fact = 1;
        for (int i = 1; i <= n; i++) {
            fact *= i;
            num.add(i);
        }
        for (int i = 0, l = k - 1; i < n; i++) {
            fact /= (n - i);
            int index = (l / fact);
            sb.append(num.remove(index));
            l -= index * fact;
        }
        return sb.toString();
    }
}
上一篇 下一篇