LeetCode 第 153 场周赛

一、公交站间的距离(LeetCode-5181)

1.1 题目描述

LeetCode 第 153 场周赛_第1张图片

1.2 解题思路

比较简单的一题,顺时针、逆时针两次遍历,就能解决。

1.3 解题代码


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;
    }
}

二、 一周中的第几天(LeetCode-5183)

2.1 题目描述

LeetCode 第 153 场周赛_第2张图片

2.2 解题思路

调用工具类水过。。。。

2.3 解题代码


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 "";
    }
}

转载于:https://www.cnblogs.com/fonxian/p/11489396.html

你可能感兴趣的:(数据结构与算法,java)