LeetCode Weekly Contest 177

1360. Days Between Dates

Please write a program to calculate the number of days between two dates.

The date is given as a string in the format  YYYY-MM-DD, as shown in the example.

示例 1:

输入:date1 = "2019-06-29", date2 = "2019-06-30"
输出:1

        This question is lazy and solves it directly using Java library functions. .

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

class Solution {
    public int daysBetweenDates(String date1, String date2) {
        LocalDate startDate = LocalDate.parse(date1);
        LocalDate endDate = LocalDate.parse(date2);
        long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
        return Math.abs((int)daysDiff);
    }
}

 

1361. Verify Binary Tree

There are n nodes on the binary tree, numbered from 0 to n-1, where the two child nodes of node i are leftChild[i] and rightChild[i].

If only all nodes can form and only form a valid binary tree, return true; otherwise, return false.

If node i has no left child, then leftChild[i] is equal to -1. The right child node also complies with this rule.

Note: The node has no value, only the node number is used in this question.

示例 1:

输入:n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
输出:true

note:

Sufficient conditions of binary tree:

1) The root node cannot be accessed

2) All other nodes are visited and only visited once

class Solution {
    public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
        int[] cnt = new int[n];
        for(int i=0; i<n; i++) {
            if(leftChild[i] >= 0) {
                cnt[leftChild[i]] += 1;
            }
            if(rightChild[i] >= 0) {
                cnt[rightChild[i]] += 1;
            }
        }
        if(cnt[0] != 0) {
            return false;
        }
        for(int i=1; i<n; i++) {
            if(cnt[i] != 1) {
                return false;
            }
        }
        return true;
    }
}

 

1362. The closest factor

Given an integer num, please find two integers that satisfy all the following requirements at the same time:

The product of two numbers is equal to num + 1 or num + 2
to measure the absolute difference, and the two numbers are closest in size.
You can return these two integers in any order.

示例 1:

输入:num = 8
输出:[3,3]
解释:对于 num + 1 = 9,最接近的两个因数是 3 & 3;对于 num + 2 = 10, 最接近的两个因数是 2 & 5,因此返回 3 & 3 。

        Tips: From sqrt traversal to 1, the difference must be gradually larger, so the first difference encountered must be the smallest.

class Solution {
    public int[] closestDivisors(int num) {
        int[] r = new int[2];
        int sqrt = (int) Math.sqrt(num + 2);  //平方根
        for(int i = sqrt; i >= 1; i--){
            if((num + 1) % i == 0){
                r[0] = i;
                r[1] = (num + 1) / i;
                return r;
            }
            if((num + 2) % i == 0){
                r[0] = i;
                r[1] = (num + 2) / i;
                return r;
            }
        }
        return r;
    }
}

 

1363. Form the largest multiple of three

Give you an integer array digits, you can form multiples of 3 by concatenating some numbers in any order, please return the largest  multiple of 3 you can get  .

Since the answer may not be within the range of the integer data type, please return the answer as a string.

If you cannot get the answer, please return an empty string.

示例 1:

输入:digits = [8,1,9]
输出:"981"

analysis:

For a number divisible by 3, the sum of the numbers in each digit can also be divisible by 3.

  • Calculate the sum of the digit array.
  • Divide the numbers into 3i, 3i+1, 3i+2.
  • If sum% 3 == 1, just delete one 3i+1; if there is no such number, delete two 3i+2 (it can be proved that there are at least two 3i+2 at this time); if sum% 3 == 2 Same reason.

The observation data scale is only 0-9, and the number of times each number appears can be counted.

class Solution {
    private int[] num;

    private boolean delete(int k) {
        for (int i = k; i < 10; i = i + 3) {
            if (num[i] > 0) {
                num[i]--;
                return true;
            }
        }
        return false;
    }
    private String makeString() {
        StringBuilder ans = new StringBuilder("");
        for (int i = 9; i >= 0; i--)
            while (num[i]-- > 0)
                ans.append((char)(i + '0'));
        int index = 0;
        while (index < ans.length() - 1) {
            if (ans.charAt(index) != '0') break;
            index++;
        }
        return ans.delete(0, index).toString();
    }
    public String largestMultipleOfThree(int[] digits) {
        num = new int[10];
        int sum = 0;
        for (int value : digits) {
            num[value]++;
            sum += value;
        }
        int k = sum % 3;
        if (k > 0) {
            if (!delete(k)) {
                delete(3 - k);
                delete(3 - k);
            }
        }
        return makeString();
    }
}

 

Guess you like

Origin blog.csdn.net/qq_34519487/article/details/104742252