Skip to content

Instantly share code, notes, and snippets.

@myesn
myesn / animate-on-hover.html
Last active May 31, 2024 08:51
使用 animate.css 在鼠标悬浮至元素时添加动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
<style>
.container {
@myesn
myesn / BinaryTreeTraversal.cs
Created October 16, 2023 07:34
二叉树遍历(层级、前序、中序、后序)
var root = new TreeNode(1)
{
Left = new TreeNode(2)
{
Left = new TreeNode(4),
Right = new TreeNode(5)
},
Right = new TreeNode(3)
{
@myesn
myesn / SearchIn2DMatrix.cs
Created October 15, 2023 06:32
在矩阵(二维数组)中查找目标值的坐标
/*
Q: 给定一个的目标值,在一个横向有序和纵向有序的二维数组(矩阵 matrix)中找到该目标值的坐标
可以从矩阵的右上角或者左下角开始查找。
假设从右上角开始查询:
如果当前元素大于目标值,则可以排除当前元素所在的列,因为这一列都会比目标值大;
如果当前元素小于目标值,则可以排除当前元素所在的行,因为这一行都会比目标值小。
通过这种方式,每次都可以排除一整行或者一整列,大大减少了比较的次数,时间复杂度为 O(m+n)
*/
@myesn
myesn / BinarySearch.cs
Last active October 15, 2023 06:25
在一维数组中使用二分查找算法得到目标值的索引
int[] array = { 1,11,21,31,41 };
foreach(int num in array) {
Console.WriteLine(BinarySearchMatrix(array, num));
}
// 二分查找:在一维数组中找到目标值的索引
int BinarySearch(int[] array,int target)
{
// 初始化指针,定义头指针 left、尾指针 right = 二维数组中一维数组的数量-1=4-1,双闭区间 [0, array.Length - 1]
@myesn
myesn / node-dns-benchmark.js
Created March 19, 2023 06:07
指定 dns 服务器,测试该 dns 服务器的 A 记录每秒解析速率
const { Resolver } = require('node:dns').promises;
const { performance } = require('perf_hooks');
const domain = 'giao.com';
const resolver = new Resolver();
resolver.setServers(['192.168.31.117']);
async function main() {
const numIterations = 1;
@myesn
myesn / logging.middleware.ts
Created December 22, 2022 01:52
NestJS Middleware
// logging.middleware.ts
import { Injectable, NestMiddleware } from '@nestjs/common';
import { NextFunction, Request, Response } from 'express';
@Injectable()
export class LoggingMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
console.log(`start [${req.method}] ${req.url}`);
const oldSend = res.send;
res.send = (body: any): any => {
@myesn
myesn / logto-auth.guard.ts
Created October 20, 2022 08:52
NestJS Logto Auth Guard
import {
Injectable,
CanActivate,
ExecutionContext,
HttpStatus,
BadRequestException,
UnauthorizedException,
} from '@nestjs/common';
import { Request } from 'express';
import { ConfigService } from '@nestjs/config';
<script setup lang="ts">
import { reactive, shallowRef, watchEffect } from "vue";
interface Item {
id: number;
name: string;
}
const name = shallowRef<string>("");
const itemLength = shallowRef<number>(0);
<script setup>
import { ref, watchEffect } from 'vue'
const number = ref(0);
const unwatch = watchEffect((onCleanup) => {
const id = setInterval(() => {
number.value++;
}, 1000);
onCleanup(() => clearInterval(id));
@myesn
myesn / diff-between-datetime-by-rules.js
Created April 21, 2022 14:12
按照指定规则计算 开始日期时间-结束日期时间 之间的时间差
getTimes(val) {
const [startDateRaw, endDateRaw] = val;
const startDate = new Date(startDateRaw);
const endDate = new Date(endDateRaw);
const diffOfMilliseconds = endDate - startDate; // 时间差,单位毫秒
const hoursOfDay = 24; // 一天的小时数
const millisencondsOfMinute = 60 * 1000; // 每分钟的毫秒数
const millisencondsOfHour = 60 * millisencondsOfMinute; // 每小时的毫秒数
const millisencondsOfDay = hoursOfDay * millisencondsOfHour; // 每天的毫秒数