(Easy) Day of the week - LeetCode

时间:2019-09-09
本文章向大家介绍(Easy) Day of the week - LeetCode,主要包括(Easy) Day of the week - LeetCode使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Description:

Given a date, return the corresponding day of the week for that date.

The input is given as three integers representing the day, month and year respectively.

Return the answer as one of the following values {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}.

Example 1:

Input: day = 31, month = 8, year = 2019
Output: "Saturday"

Example 2:

Input: day = 18, month = 7, year = 1999
Output: "Sunday"

Example 3:

Input: day = 15, month = 8, year = 1993
Output: "Sunday"

Constraints:

  • The given dates are valid dates between the years 1971 and 2100.
Accepted
4,400
Submissions
6,472

Solution:

class Solution {
    public String dayOfTheWeek(int day, int month, int year) {
        
        
        //2019-9-8  Sunday.   
        
        // we need to get the date difference. In that way, we will be able to know what day that day is.
             int num_1 =    GetNum(day,month,year);
             int num_2 =    GetNum(8,9,2019);
             //System.out.println(num_2-num_1);
            int diff = Math.abs(num_2-num_1)%7;
           
           if(diff ==0){
               return "Sunday";
           }
        
        
        if(num_2 - num_1 >0){
            
             if(diff==1){
                
                return "Saturday";
            }
            else if(diff == 2){
                return "Friday";
            }
              else if(diff == 3){
                return "Thursday";
            }
              else if(diff == 4){
                return "Wednesday";
            }
              else if(diff == 5){
                return "Tuesday";
            }  else if(diff == 6){
                return "Monday";
            }
        }
        else{
            
            if(diff==1){
                
                return "Monday";
            }
            else if(diff == 2){
                return "Tuesday";
            }
              else if(diff == 3){
                return "Wednesday";
            }
              else if(diff == 4){
                return "Thursday";
            }
              else if(diff == 5){
                return "Friday";
            }  else if(diff == 6){
                return "Saturday";
            }
            
        }
        
        
            System.out.println(GetNum(3,3,1977));
        
            System.out.println(GetNum(8,9,2019));
        return "";
    }
    /*
                给定一个年份,判断是否是闰年。条件为:
        A:能被4整除,并且不能被100整除。或者
        B:能被400整除。*/
    
    static boolean Leap(int a){
        
        if(a%4==0&& a%100!=0){
            return true;
        }
        else if( a%400 ==0){
            return true;
        }
        else{
            return false;
        }
    }
    
    static int GetNum(int day, int month, int year){
            
           
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        
        //Jan 31 days
        //Feb Leap year or not.  (28/29 days)
        //Mar 31 days
        //Apr 30 days
        //May 31 days
        //Jun 30 days
        //Jul 31 days
        //Aug 31 days
        //Sep 30 days
        //Oct 31 days
        //Nov 30 days
        //Dec 31 days
        
        map.put(1,31);
        map.put(3,31);
        map.put(4,30);
        map.put(5,31);
        map.put(6,30);
        map.put(7,31);
        map.put(8,31);
        map.put(9,30);
        map.put(10,31);
        map.put(11,30);
        map.put(12,31);
        
        
        if(Leap(year)){
            
            map.put(2,29);
        }
        else{
            map.put(2,28);
        }
        
        
          int sum = 0; 
            for(int i = 1977; i< year; i++){
                
                if(Leap(i)){
                     sum = sum + 366;
                }
                else{
                    sum = sum +365;
                }
               
            }
        
            for(int i = 1; i < month; i++){
                
                sum = sum + map.get(i);
                
            }
        
            sum = sum + day;
        
      return sum;
    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11490892.html