给你一个按 YYYY-MM-DD 格式表示日期的字符串 date,请你计算并返回该日期是当年的第几天。

通常情况下,我们认为 1 月 1 日是每年的第 1 天,1 月 2 日是每年的第 2 天,依此类推。每个月的天数与现行公元纪年法(格里高利历)一致。

示例1:

输入:date = "2019-01-09"
输出:

示例2:

输入:date = "2019-02-10"
输出:

示例3:

输入:date = "2003-03-01"
输出:

示例4:

输入:date = "2004-03-01"
输出:

思路:先判断在月份,将当前月份之前的月份天数和加起来,再加上当前月的日期也就是天数,再根绝闰年判断2月份的天数。

月份加和时,可以按照倒序将一年中的月份的天数排列在数组中,找到相应的月份,依次加和,2月份为28天,再根据闰年判断是否加1。

class Solution {
public:
int calcOfDay(int year,int month,int day){
int mon[]={,,,,,,,,,,,};//日期按照倒序从11月份排列,12月份加上当月日期就好
int res=;
for(int i=-month;i<;i++)
res+=mon[i];
if((month>)&&((year%==&&year%!=)||year%==))//当月份超过两个月且为闰年时,日期加一
res++;
res+=day;
return res;
} int dayOfYear(string date) {
int year, month, day;
string years = date.substr(, );
string months = date.substr(, );
string days = date.substr(, );
stringstream ss;
ss << years;
ss >> year;
ss.clear();
ss << months;
ss >> month;
ss.clear();
ss << days;
ss >> day;
int res = calcOfDay(year, month, day);
return res;
}
};

string的substr用法sbustr(int index,int len);从index起始位置截取长度为len的字符串。

LeetCode 1154. 一年中的第几天的更多相关文章

  1. LeetCode.1154-一年中的第几天(Day of the Year)

    这是小川的第410次更新,第442篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第261题(顺位题号是1154).给定表示格式为YYYY-MM-DD的公历日期的字符串日期,返回 ...

  2. 【LeetCode】1154. Day of the Year 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计算1月1号之间天数 日期 题目地址:https:// ...

  3. 【leetcode】1154. Day of the Year

    题目如下: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the ...

  4. 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)

    Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...

  5. mysql学习 | LeetCode数据库简单查询练习

    力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...

  6. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  9. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

随机推荐

  1. 2.Bacula Server端安装配置

    1.  Bacula Server端安装配置 1.1.  Bacula Server端安装 1.1.1.  安装bacula依赖包 For Centos6: yum install -y mysql ...

  2. C# TabControl 带删除

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostic ...

  3. 大幅提升Delphi Datasnap数据传输效率的方法

    方法一:增加TCP读写缓存的大小       DataSnap Server中负责TCP/IP通讯的组件是TDSTCPServerTransport,它默认的TCP/IP读写缓冲区的大小为32KB,由 ...

  4. VS---《在VS2010中 使用C++创建和使用DLL》(002)

    VS---<在VS2010中 使用C++创建和使用DLL>(002) 前面初认识了创建和调用DLL,在VS工程下可以通过在 同一工程.不同工程 下创建和调用DLL.现在,同一工程下创建和调 ...

  5. ubuntu系统---切换Py2.X与Py3.X版本

    ubuntu系统---切换Python2.X与Python3.X版本 Python3.X将成为以后的趋势,Python2.X当前用的稍多的版本,但现在不再更新了.因此,小主电脑里也安装了好两个版本的p ...

  6. D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】

    Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per te ...

  7. Java8-Stream-No.12

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.UUID; i ...

  8. MySQL剖析单条查询

    使用SHOW PROFILE SHOW PROFILE命令默认是禁用的,可以通过以下命令修改 SET profiling=1; 当一条查询提交给服务器时,,此工具会记录剖析信息到一张临时表,并且给查询 ...

  9. 【leetcode】1289. Minimum Falling Path Sum II

    题目如下: Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactl ...

  10. uni-app引入css动画库

    引入Animate动画库 Animate中文网地址:http://www.animate.net.cn/ Animate下载地址:https://daneden.github.io/animate.c ...