首页 > 其他分享 >leetcode-1154-easy

leetcode-1154-easy

时间:2023-03-04 12:34:50浏览次数:36  
标签:int 1154 31 day split easy year date leetcode

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:

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

Input: date = "2019-02-10"
Output: 41
Constraints:

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

思路一:直接计算

    public int dayOfYear(String date) {
        String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);
        int day = Integer.parseInt(split[2]);
        int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            days[1] = 29;
        }

        int result = 0;
        for (int i = 0; i < month - 1; i++) {
            result += days[i];
        }

        return result + day;
    }

标签:int,1154,31,day,split,easy,year,date,leetcode
From: https://www.cnblogs.com/iyiluo/p/17178066.html

相关文章

  • leetcode-1185-easy
    DayoftheWeekGivenadate,returnthecorrespondingdayoftheweekforthatdate.Theinputisgivenasthreeintegersrepresentingtheday,monthandyea......
  • 汇川AM401(Scanner) 和Easy521 (Adapter)的EIP通信
    1,导出Easy系列的EDS文件,配置Easy521的收发字节大小  2,导入Easy系列的EDS文件,配置AM401的收发字节大小    3,AM401作为Scanner的EIP通讯诊断代码......
  • leetcode-1528-easy
    ShuffleStringYouaregivenastringsandanintegerarrayindicesofthesamelength.Thestringswillbeshuffledsuchthatthecharacterattheithposi......
  • leetcode-1470-easy
    ShuffletheArrayGiventhearraynumsconsistingof2nelementsintheform[x1,x2,...,xn,y1,y2,...,yn].Returnthearrayintheform[x1,y1,x2,y2,...,xn,yn]......
  • easyui在datagrid只想选择一条
    <tableclass=""id="jgrid"data-options="fitColumns:true,rownumbers:true,singleSelect:true">singleSelect:true......
  • LEETCODE 982. 按位与为零的三元组
    这题暴力的话会超时,考虑用哈希表来存储前两位与的结果的数量然后在另一个循环中枚举第三位和哈希表每个下标相与,找到结果为0的,对应的哈希表值加入ans中classSolution{publ......
  • [oeasy]python0099_雅达利大崩溃_IBM的开放架构_兼容机_oem
    雅达利大崩溃回忆上次内容个人计算机浪潮已经来临苹果公司迅速发展微软公司脱离mits准备做纯软件公司IBM用大型机思路制作的5100惨败Commodore64既做......
  • EasyCode mybatis-plus模板 &Live tmpl
    Mapper##导入宏定义$!{define.vm}##设置表后缀(宏定义)#setTableSuffix("Mapper")##保存文件(宏定义)#save("/mapper","Mapper.java")##包路径(宏定义)#setPackageS......
  • 将s3fs挂载到EasyCVR平台后,为何录像无法存储?该如何解决?
    EasyCVR平台基于云边端一体化架构,支持海量视频资源的轻量化接入,可兼容多协议、多类型设备,在视频能力上,可提供视频直播、录像、回放、检索、云存储、级联、告警等功能。我们......
  • RTSP/Onvif安防视频平台EasyNVR内核启动正常,但视频无法播放是什么原因?
    EasyNVR是基于RTSP/Onvif协议的安防视频云服务平台,可实现设备接入、实时直播、录像、检索与回放、云存储、视频分发、级联等视频能力服务,可覆盖全终端平台(电脑、手机、平板......