2020-12-12

题目描述

LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。

这道题首先思考是分五种情况讨论
即0的个数分别为 0 1 2 3 4
随即放弃,过于繁琐

看了一下提示
用hashset完成

import java.util.HashSet;

public class Solution {
    public boolean isContinuous(int [] numbers) {
         if (numbers.length==0){
                    return false;
                }
                HashSet<Integer> set =new HashSet<Integer>();
                int max =0;
                int min =14;
                for (int number : numbers) {
                    if (number>0){
                        if (set.add(number)==false){
                            return false;
                        }
                        set.add(number);
                        max =Math.max(max,number);
                        min =Math.min(min,number);
                    }
                }
                return max-min<5;
    }
}
要将**时间戳**(timestamp)格式化为 `2020-12-12 12:12:00` 这种可读的时间格式,你可以使用 JavaScript 原生方法或借助如 `moment.js`、`dayjs` 等库。下面我先用 **原生 JS 实现**,再推荐更优雅的方案。 --- ### ✅ 方法一:使用 JavaScript 原生 Date API ```js function formatTimestamp(timestamp) { const date = new Date(timestamp) // 获取年月日时分秒 const year = date.getFullYear() const month = String(date.getMonth() + 1).padStart(2, '0') // 月份从0开始 const day = String(date.getDate()).padStart(2, '0') const hour = String(date.getHours()).padStart(2, '0') const minute = String(date.getMinutes()).padStart(2, '0') const second = String(date.getSeconds()).padStart(2, '0') return `${year}-${month}-${day} ${hour}:${minute}:${second}` } ``` #### 🔧 使用示例: ```js const timestamp = 1607755920000 // 对应 2020-12-12 12:12:00 console.log(formatTimestamp(timestamp)) // 输出:2020-12-12 12:12:00 ``` > ✅ 支持毫秒级时间戳(13位),如果是秒级时间戳(10位),需要乘以 1000: > > ```js > const msTimestamp = secondsTimestamp * 1000 > ``` --- ### 🔍 代码解释 | 方法 | 说明 | |------|------| | `new Date(timestamp)` | 创建日期对象 | | `.getMonth() + 1` | 月份是从 0 开始的,所以要加 1 | | `.padStart(2, '0')` | 补零,确保 `9` → `09` | --- ### ✅ 方法二:封装成通用工具函数(支持字符串/数字时间戳) ```js function formatTime(time, format = 'YYYY-MM-DD HH:mm:ss') { const date = typeof time === 'string' || typeof time === 'number' ? new Date(time) : time if (isNaN(date.getTime())) { throw new Error('Invalid date or timestamp') } const year = date.getFullYear() const month = String(date.getMonth() + 1).padStart(2, '0') const day = String(date.getDate()).padStart(2, '0') const hour = String(date.getHours()).padStart(2, '0') const minute = String(date.getMinutes()).padStart(2, '0') const second = String(date.getSeconds()).padStart(2, '0') return format .replace('YYYY', year) .replace('MM', month) .replace('DD', day) .replace('HH', hour) .replace('mm', minute) .replace('ss', second) } ``` #### 🧪 示例使用: ```js console.log(formatTime(1607755920000)) // 2020-12-12 12:12:00 console.log(formatTime('2020-12-12T12:12:00')) // 2020-12-12 12:12:00 ``` --- ### ✅ 方法三:使用轻量级库 [Day.js](https://day.js.org/)(推荐) 安装: ```bash npm install dayjs ``` 使用: ```js import dayjs from 'dayjs' const formatted = dayjs(1607755920000).format('YYYY-MM-DD HH:mm:ss') console.log(formatted) // 2020-12-12 12:12:00 ``` > 💡 Day.js 体积小、API 类似 moment.js、性能好,非常适合前端项目。 --- ### ⚠️ 注意事项 - 时间戳单位是 **毫秒**(JavaScript 默认) - 如果你拿到的是 **秒级时间戳**(例如后端返回的 Unix 时间戳),记得 ×1000: ```js const ms = seconds => seconds * 1000 dayjs(ms(1607755920)).format('YYYY-MM-DD HH:mm:ss') ``` - 浏览器显示时间基于本地时区,若需 UTC 时间,请使用 `.utc()` 或手动调整。 --- ### ✅ 扩展:UTC 时间处理(避免时区偏差) ```js function formatTimestampUTC(timestamp) { const date = new Date(timestamp) return ( date.getUTCFullYear() + '-' + String(date.getUTCMonth() + 1).padStart(2, '0') + '-' + String(date.getUTCDate()).padStart(2, '0') + ' ' + String(date.getUTCHours()).padStart(2, '0') + ':' + String(date.getUTCMinutes()).padStart(2, '0') + ':' + String(date.getUTCSeconds()).padStart(2, '0') ) } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值