题目地址:https://leetcode.com/problems/day-of-the-year/

题目描述

Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.

Example 1:

  1. Input: date = "2019-01-09"
  2. Output: 9
  3. Explanation: Given date is the 9th day of the year in 2019.

Example 2:

  1. Input: date = "2019-02-10"
  2. Output: 41

Example 3:

  1. Input: date = "2003-03-01"
  2. Output: 60

Example 4:

  1. Input: date = "2004-03-01"
  2. Output: 61

Constraints:

  1. date.length == 10
  2. date[4] == date[7] == '-', and all other date[i]'s are digits
  3. date represents a calendar date between Jan 1st, 1900 and Dec 31, 2019.

题目大意

判断给出的日期是当月的第多少天。

解题方法

计算1月1号之间天数

这个题和1185. Day of the Week类似。这个题中计算出当年是不是闰年,然后计算和1月1号之间的天数就行了,需要累加每个月,特别注意二月份。

C++代码如下:

  1. class Solution {
  2. public:
  3. int dayOfYear(string date) {
  4. int year = stoi(date.substr(0, 4));
  5. int month = stoi(date.substr(5, 2));
  6. int day = stoi(date.substr(8));
  7. return countDays(day, month, year);
  8. }
  9. bool isLeapYear(int year) {
  10. return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
  11. }
  12. int countDays(int day, int month, int year) {
  13. int res = 0;
  14. while (month > 1) {
  15. res += daysOfMonth[isLeapYear(year)][month - 2];
  16. month--;
  17. }
  18. res += day;
  19. return res;
  20. }
  21. int daysOfMonth[2][12] = {
  22. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  23. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
  24. };
  25. };

日期

2019 年 9 月 18 日 —— 今日又是九一八

【LeetCode】1154. Day of the Year 解题报告(C++)的更多相关文章

  1. 【LeetCode】760. Find Anagram Mappings 解题报告

    [LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ...

  2. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  3. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  4. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  5. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  6. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  7. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  8. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  9. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  10. 【LeetCode】729. My Calendar I 解题报告

    [LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...

随机推荐

  1. android Fragment跳转Fragment

    android Fragment跳转Fragment,最新的android studio3 在系统模板建立的BottomNavigationView 中跳转方式 此版本下不能用FragmentMana ...

  2. LR SP PC

    LR SP PC 深入理解ARM的这三个寄存器,对编程以及操作系统的移植都有很大的裨益. 1.堆栈指针r13(SP):每一种异常模式都有其自己独立的r13,它通常指向异常模式所专用的堆栈,也就是说五种 ...

  3. Google服务器架构图解简析

    无疑是互联网时代最闪亮的明星.截止到今天为止,Google美国主站在Alexa排名已经连续3年第一,Alexa Top100中,各国的Google分站竟然霸占了超过20多个名额,不得不令人感叹Goog ...

  4. 零基础学习java------day18------properties集合,多线程(线程和进程,多线程的实现,线程中的方法,线程的声明周期,线程安全问题,wait/notify.notifyAll,死锁,线程池),

    1.Properties集合 1.1 概述: Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串 一个属性列表可包含另 ...

  5. [php代码审计] 通读审计之shangfancms

    前言 大部分的MVC框架,访问的控制器大部分是由外部参数来决定的,那么本篇文章所通读的MVC框架与之前的一系列MVC框架不太一样,它的路由是由程序本身的路由表来决定的. 源码下载 https://ww ...

  6. C++异常处理(try、catch、throw)

    本文为转载 博主原文连接 我们通常希望自己编写的程序能够在异常的情况下也能作出相应的处理,而不至于程序莫名其妙地中断或者中止运行了.在设计程序时应充分考虑各种异常情况,并加以处理. 在C++中,一个函 ...

  7. Linux系统信息查看命令(ZZ)

    http://hi.baidu.com/thinkdifferent/blog/item/22f4a80161630e011d958384.html转自一个baidu师兄的博客,很好的一个总结,推荐下 ...

  8. java 动态代理—— Mybaties 拦截器链基本原理实现

    1.摘要 Mybaties 中有个分页插件,之前有特意的去了解了一下原理 :https://www.cnblogs.com/jonrain0625/p/11168247.html,从了解中得知分页插件 ...

  9. Selenium之Canvas画布操作

    现在有一个场景是需要进入到 Canvas画布中 进行单击操作,现在使用过如下方法 canvas = driver.find_element_by_xpath("//canvas[@id='# ...

  10. Identity Server 4 从入门到落地(九)—— 客户端User和Role的解析

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...