给你一个按 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. Image Processing and Analysis_8_Edge Detection: Optimal edge detection in two-dimensional images ——1996

    此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...

  2. Django模型层:单表操作,多表操作,常用(非常用)字段和参数,Django-model进阶

    一.web应用 二.模板的导入与继承 三.静态文件相关 四.inclusion_tag:返回html片段 五.模型层 一.web应用 -s包括两个部分:web服务器+application -目前阶段 ...

  3. 微信小程序开发(八)获取手机ip地址

    // succ.wxml <view>手机IP:{{motto.query}}</view> // succ.js var app = getApp() Page({ data ...

  4. E - GCD HDU - 2588

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...

  5. jsp下拉列表

    <c:set var="REPORT_TYPE_NORMAL" value="<%=SysIndexFormTemp.REPORT_TYPE_NORMAL%& ...

  6. redis + boost.asio

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  7. SpringMVC防止重复提交

    作者:mylovepan 推荐:GOODDEEP 问题描述: 现在的网站在注册步骤中,由于后台要处理大量信息,造成响应变慢(测试机器性能差也是造成变慢的一个因素),在前端页面提交信息之前,等待后端响应 ...

  8. 基于STM32调试工具STM-STUDIO-STM32的使用

    手上有stlink下载器,正好看到官网有这个工具,可以在运行中实时查看变量的数据.这一点和ucos的ucprobe很类似. 参考https://mp.weixin.qq.com/s?src=11&am ...

  9. mongodb命令---创建数据库,插入文档,更新文档记录

    创建数据库----基本就是使用隐式创建 例如 use 你定义的数据库名, use dingsmongo  如果你使用的是studio 3T软件,那直接选中右侧的地址栏点击右键选择Add Databas ...

  10. struts表单提交(4)和(3)相关

    实体类 public class Supplier { private int id; private String supplierid; private String suppliernane; ...