Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Number of Days in a Month

Posted on July 22, 2019July 26, 2020 by braindenny

Number of Days in a Month



Similar Problems:

  • LeetCode: Day of the Year
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #leapyear, #math

Given a year Y and a month M, return how many days there are in that month.

Example 1:

Input: Y = 1992, M = 7
Output: 31

Example 2:

Input: Y = 2000, M = 2
Output: 29

Example 3:

Input: Y = 1900, M = 2
Output: 28

Note:

  1. 1583 <= Y <= 2100
  2. 1 <= M <= 12

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/number-of-days-in-a-month
// Basic Ideas: array
//  https://www.timeanddate.com/calendar/months/
// Complexity: Time O(1), Space O(1)
func numberOfDays(Y int, M int) int {
    l := []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    if M == 2 {
        if (Y%4 ==0 && Y%100!=0) || (Y%400 == 0) {
            l[1] = 29
        }
    }
    return l[M-1]
}
linkedin
github
slack

Post Views: 0
Posted in BasicTagged #math, leapyear

Post navigation

LeetCode: Active Businesses
LeetCode: Maximum Average Subtree

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.