「力扣」第 30 场双周赛代码(前 3 题)

第 1 题:转变日期格式

Java 代码:

import java.util.HashMap;
import java.util.Map;

public class Solution {

    public String reformatDate(String date) {
        String[] split = date.split(" ");

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(split[2]);
        stringBuilder.append("-");

        Map<String, String> month = new HashMap<>();

        month.put("Jan", "01");
        month.put("Feb", "02");
        month.put("Mar", "03");
        month.put("Apr", "04");
        month.put("May", "05");
        month.put("Jun", "06");
        month.put("Jul", "07");
        month.put("Aug", "08");
        month.put("Sep", "09");
        month.put("Oct", "10");
        month.put("Nov", "11");
        month.put("Dec", "12");

        stringBuilder.append(month.get(split[1]));
        stringBuilder.append("-");

        if (split[0].length() == 3) {
            stringBuilder.append('0');
            stringBuilder.append(split[0].charAt(0));
        } else {
            stringBuilder.append(split[0].charAt(0));
            stringBuilder.append(split[0].charAt(1));
        }
        return stringBuilder.toString();
    }
}

第 2 题:子数组和排序后的区间和

Java 代码:

import java.util.Arrays;

public class Solution {

    public int rangeSum(int[] nums, int n, int left, int right) {
        int[] preSum = new int[n + 1];
        preSum[0] = 0;

        for (int i = 0; i < n; i++) {
            preSum[i + 1] = preSum[i] + nums[i];
        }

        int[] res = new int[n * (n + 1) / 2];
        int index = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                // 计算区间和
                int sumRange = preSum[j + 1] - preSum[i];
                res[index] = sumRange;
                index++;
            }
        }

        Arrays.sort(res);
        // System.out.println(Arrays.toString(res));

        int sum = 0;
        for (int i = left - 1; i <= right - 1; i++) {
            sum += res[i];
        }
        return sum;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3, 4};
        int n = 4;
        int left = 1;
        int right = 5;

        Solution solution = new Solution();
        int res = solution.rangeSum(nums, n, left, right);
        System.out.println(res);
    }
}

第 3 题:三次操作后最大值与最小值的最小差

Java 代码:

import java.util.Arrays;

public class Solution {

    public int minDifference(int[] nums) {
        int len = nums.length;
        if (len <= 3) {
            return 0;
        }

        Arrays.sort(nums);
        int res = Integer.MAX_VALUE;
        for (int i = 0; i <= 3; i++) {
            int diff = removeFromLeft(nums, len, i);
            res = Math.min(res, diff);
        }
        return res;
    }

    private int removeFromLeft(int[] nums, int len, int leftTimes) {
        int left = leftTimes;
        int right = len - 1 - (3 - left);
        int diff = nums[right] - nums[left];
        return diff;
    }

    public static void main(String[] args) {
        // int[] nums = {6, 6, 0, 1, 1, 4, 6};
        int[] nums = {1, 5, 0, 10, 14};
        Solution solution = new Solution();
        int res = solution.minDifference(nums);
        System.out.println(res);
    }
}

你可能感兴趣的:(力扣)