LeetCode 153 Race Course Week

A distance between the bus station ( LeetCode 5181- )

1.1 Title Description

1.2 Problem-solving ideas

A relatively simple question, clockwise, counterclockwise twice traversal, can be solved.

1.3 solving Code


class Solution {
    public int distanceBetweenBusStops(int[] distance, int start, int destination) {
        int res = 0;
        int len = distance.length;

        int i = start;
        do {
            res += distance[i];
            i++;
            if (i > len - 1) {
                i = 0;
            }
        } while (i != destination);


        int tmp = 0;
        int j = destination;
        do {
            tmp += distance[j];
            j++;
            if (j > len - 1) {
                j = 0;
            }
        } while (j != start && j != destination);


        return res < tmp ? res : tmp;
    }
}

Second, the day of the week ( LeetCode 5183- )

2.1 Title Description

2.2 Problem-solving ideas

Tools like water off a call. . . .

2.3 Code Solving


import java.util.Calendar;
class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 1:
                return "Sunday";
            case 2:
                return "Monday";
            case 3:
                return "Tuesday";
            case 4:
                return "Wednesday";
            case 5:
                return "Thursday";
            case 6:
                return "Friday";
            case 7:
                return "Saturday";
        }
        return "";
    }
}

Guess you like

Origin www.cnblogs.com/fonxian/p/11489396.html