LeetCode 1154. 一年中的第几天
给你一个按 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. 一年中的第几天的更多相关文章
- LeetCode.1154-一年中的第几天(Day of the Year)
这是小川的第410次更新,第442篇原创 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第261题(顺位题号是1154).给定表示格式为YYYY-MM-DD的公历日期的字符串日期,返回 ...
- 【LeetCode】1154. Day of the Year 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计算1月1号之间天数 日期 题目地址:https:// ...
- 【leetcode】1154. Day of the Year
题目如下: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
- mysql学习 | LeetCode数据库简单查询练习
力扣:https://leetcode-cn.com/ 力扣网数据库练习:https://leetcode-cn.com/problemset/database/ 文章目录 175. 组合两个表 题解 ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
随机推荐
- 二十:强类型HTML辅助方法
1. 强类型HTML辅助方法的使用 1. HTML辅助方法 例如,要输出一个文本框 @Html.TextBox("email") 2.强类型HTML辅助方法 命名规则是: HTML ...
- mysql中的分区
第18章:分区 目录 18.1. MySQL中的分区概述 18.2. 分区类型 18.2.1. RANGE分区 18.2.2. LIST分区 18.2.3. HASH分区 18.2.4. KEY分区 ...
- AtCoder Beginner Contest 132 F Small Products
Small Products 思路: 整除分块+dp 打表发现,按整除分块后转移方向如下图所示,上面的块的前缀转移到下面的块 代码: #pragma GCC optimize(2) #pragma G ...
- FasterRunner (httptunner+django)搭建以及小功能补充
配置 下载地址https://github.com/httprunner/FasterRunner 后端配置https://www.jianshu.com/p/e26ccc21ddf2 前端配置htt ...
- PAT乙级1042
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805280817135616 题解 用数组count存储字母出现 ...
- Java冒泡排序,二分查找法
冒泡排序 int[] arr = {1,7,6,2,8,4}; int temp ; //只需 运行 5次 for (int i = 0; i < arr.length - 1; i++) { ...
- kickstart批量装机脚本
#!/bin/bash #安装必备的软件 yum -y install dhcp tftp-server tftp xinetd syslinux vsftpd yum -y install *kic ...
- axios请求提交的form data格式 明明是JSON格式的参数却转成了字符串格式
问题:传的参数成为了字符数格式 解决:把参数的格式转换 const params = new URLSearchParams() params.append('USER_LOGIN', 'admin' ...
- input 设置长度限制 去除默认样式
1. <input id="mobile" maxlength="11> 2. input type 为 number 时,maxlength 失效 < ...
- 小米oj 找小"3"(数位dp)
找小"3" 序号:#40难度:困难时间限制:1000ms内存限制:10M 描述 给定一个奇数n,可得到一个由从1到n的所有奇数所组成的数列,求这一数列中数字3所出现的总次数.例如 ...