Compare commits

...

62 Commits

Author SHA1 Message Date
92080a6688 docs: delete 面试/布局题目 2024-03-28 08:42:08 +00:00
a85536ed4c docs: create 面试/布局题目 2024-03-28 08:23:48 +00:00
b9e48e0a95 docs: upload qq截图20240328162259.png 2024-03-28 08:23:16 +00:00
1cff01a350 docs: upload qq截图20240328162120.png 2024-03-28 08:21:45 +00:00
20097818c5 docs: upload 微信图片_20240328160342.jpg 2024-03-28 08:04:09 +00:00
cafc1229d5 docs: create 算法/字符串 2024-03-21 11:16:21 +00:00
c588e3de53 docs: create 算法/数组 2024-03-21 11:15:49 +00:00
581b33846f docs: create 算法/链表 2024-03-21 11:15:21 +00:00
9996f40de3 docs: create 算法/哈希 2024-03-21 11:13:37 +00:00
f3dd953593 docs: create 动漫/top10 2024-03-20 01:34:15 +00:00
f4627ded5b docs: delete new-page 2024-03-20 01:31:22 +00:00
282823cbb9 docs: update new-page 2024-03-20 01:30:52 +00:00
81f242c842 docs: create new-page 2024-03-20 01:29:45 +00:00
2a14acf9b8 docs: update home 2024-03-17 09:03:13 +00:00
5166c61d90 docs: update home 2024-03-16 08:31:35 +00:00
773f3a8443 docs: update home 2024-03-16 08:30:49 +00:00
2384459768 docs: update home 2024-03-16 08:29:59 +00:00
52ec80671a docs: update home 2024-03-16 08:27:23 +00:00
3de9210e9a docs: update home 2024-03-16 08:25:58 +00:00
19c033a85c docs: update home 2024-03-16 08:04:39 +00:00
83f9138bf9 docs: update home 2024-03-16 08:04:14 +00:00
963eb48932 docs: update home 2024-03-16 08:01:28 +00:00
b8317e8e5d docs: update home 2024-03-16 08:00:27 +00:00
b3a51875ae docs: update home 2024-03-16 07:59:00 +00:00
f02df6f3e0 docs: update home 2024-03-16 07:57:53 +00:00
8c8b99d7cb docs: update home 2024-03-16 07:56:39 +00:00
f1822c94f1 docs: update home 2024-03-16 07:54:33 +00:00
4ad16aeec7 docs: update home 2024-03-16 07:52:04 +00:00
91b959b59e docs: update home 2024-03-16 07:49:32 +00:00
5a5fe2fe6a docs: update home 2024-03-16 07:48:52 +00:00
741fc41be6 docs: update home 2024-03-16 07:47:16 +00:00
f10cafb3f8 docs: update home 2024-03-16 07:34:39 +00:00
4143a79472 docs: update home 2024-03-16 07:33:35 +00:00
aab4d40730 docs: update home 2024-03-16 07:31:16 +00:00
915e0597fc docs: update home 2024-03-16 07:30:22 +00:00
ac0228b5e9 docs: create home 2024-03-16 07:26:00 +00:00
5c1337f21a docs: delete home 2024-03-16 07:20:57 +00:00
f2a4633788 docs: update home 2024-03-16 06:40:57 +00:00
98c3cde726 docs: update home 2024-03-16 06:37:41 +00:00
9ed0c2e1f0 docs: update Dockerfile 2024-01-23 03:09:38 +00:00
074e97795b docs: update Dockerfile 2024-01-23 03:02:10 +00:00
dfdd67b3ae docs: update Dockerfile 2024-01-08 14:00:48 +00:00
24a1899cdb docs: update Dockerfile 2024-01-08 12:44:20 +00:00
5f311445a8 docs: update Dockerfile 2024-01-08 06:13:30 +00:00
7de6864c29 docs: update Dockerfile 2024-01-08 03:30:50 +00:00
e4e187e888 docs: update Dockerfile 2024-01-08 03:27:03 +00:00
9c5386f639 docs: update Dockerfile 2024-01-02 12:47:58 +00:00
fe1a9d5aa9 docs: update Dockerfile 2024-01-02 12:30:35 +00:00
undefined
0a9bd493a0 docs: delete 游戏 2023-12-28 02:19:20 +00:00
undefined
72161df1dc docs: delete docker 2023-12-28 02:18:39 +00:00
dc6d69749a docs: rename docker/Dockerfile to Dockerfile 2023-12-28 02:18:28 +00:00
211277197a docs: update docker/Dockerfile 2023-04-10 05:58:58 +00:00
5e0edba77a docs: update docker/Dockerfile 2023-04-07 01:41:35 +00:00
0c7cb4f21d docs: update docker/Dockerfile 2023-04-07 01:40:48 +00:00
a79d826efc docs: update docker/Dockerfile 2023-04-07 01:40:06 +00:00
7c0ee32b2f docs: update docker/Dockerfile 2023-04-07 01:39:21 +00:00
6849e233aa docs: update docker/Dockerfile 2023-04-07 01:33:40 +00:00
5ffdb3a8ff docs: update docker/Dockerfile 2023-04-04 08:53:19 +00:00
59a8475c35 docs: create docker 2023-04-03 09:20:58 +00:00
f5ac514477 docs: update docker/Dockerfile 2023-04-03 09:13:29 +00:00
887b0c9797 docs: update docker/Dockerfile 2023-04-03 09:11:54 +00:00
4383e8ef05 docs: create docker/Dockerfile 2023-04-03 09:10:24 +00:00
11 changed files with 1196 additions and 25 deletions

26
Dockerfile.html Normal file
View File

@ -0,0 +1,26 @@
<!--
title: 我的Dockerfile
description:
published: true
date: 2024-01-23T03:09:37.075Z
tags: docker, 技术, code, 代码
editor: ckeditor
dateCreated: 2023-04-03T09:10:22.169Z
-->
<h1>my-code-server</h1>
<pre><code class="language-plaintext">FROM lscr.io/linuxserver/code-server:latest
RUN apt-get update \
&amp;&amp; apt install -y npm \
&amp;&amp; apt install -y git \
&amp;&amp; npm install -g n \
&amp;&amp; n latest \
&amp;&amp; npm install -g yarn \</code></pre>
<h1>imageAve</h1>
<pre><code class="language-plaintext">FROM golang:latest
RUN git clone https://gitea.ocer.cc/overcast404/imageAve.git \
&amp;&amp; cd imageAve \
&amp;&amp; go mod tidy
CMD go run /imageAve/api/img2color.go</code></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>

View File

@ -1,19 +1,51 @@
<!-- <!--
title: 欢迎 title: 你好
description: description: 这里是吴阴天兴趣使然的档案馆
published: true published: true
date: 2023-03-31T09:45:25.895Z date: 2024-03-17T09:03:09.473Z
tags: tags:
editor: ckeditor editor: code
dateCreated: 2023-02-19T05:55:06.863Z dateCreated: 2024-03-16T07:25:56.956Z
--> -->
<h1>这里存放的是吴阴天可以对外开放的数据</h1> <div class="home-header">
<h2>如果你来错了地方,那你可以试试看:</h2> 也许我们会忘记所读过书的内容,但我们在书中学到的东西早已随我们的血液流淌。
<ol> </div>
<li>可以找到我所有酷酷东西的<a href="https://ocer.cc">主页(维护中)</a></li> <div class="home-container">
<li>含有我对于一切事物思考的<a href="https://world.ocer.cc">我的博客</a></li> <div class="box">
<li>我的一些<a href="https://now.ocer.cc">碎碎念</a></li> <span></span>
<li>可能你想要我得到的一些好看图片?那可以看看<a href="https://chevereto.ocer.cc/">我的图床</a></li> <div class="content">
<li>我的代码都是开源的,如果需要,你可以在我的<a href="https://gitea.ocer.cc/">代码仓库</a>找到。</li> <h2>动漫</h2>
</ol> <p>我曾经是一个标准的二次元。虽然现在更加享受三次元的生活,但独属于二次元的美好永不会忘。</p>
<a href="#">查看我的追番表</a>
</div>
</div>
<div class="box">
<span></span>
<div class="content">
<h2>游戏</h2>
<p>第九艺术。没有什么比游戏更加单纯、更加直接的快乐了。如果有朋友一起,那就更棒了!</p>
<a href="#">浏览我的游戏库</a>
</div>
</div>
<div class="box">
<span></span>
<div class="content">
<h2>影视</h2>
<p>有一些细腻的情感,是动漫和游戏无法展现的。感谢有这些优秀的演员。</p>
<a href="#">和我一起看片儿</a>
</div>
</div>
</div>
<div class="home-footer">
<div>站点导航</div>
<div class="home-footer-list">
<a href="https://ocer.cc" target="_blank">主页</a>
·
<a href="https://world.ocer.cc" target="_blank">博客</a>
·
<a href="https://land.ocer.cc" target="_blank">大陆</a>
</div>
</div>

BIN
qq截图20240328162120.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
qq截图20240328162259.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

11
动漫/top10.html Normal file
View File

@ -0,0 +1,11 @@
<!--
title: 动漫top10
description:
published: true
date: 2024-03-20T01:34:11.783Z
tags:
editor: ckeditor
dateCreated: 2024-03-20T01:34:11.783Z
-->
<h1>动漫top10</h1>

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

View File

@ -1,11 +0,0 @@
<!--
title: 游戏
description: 游戏说明
published: true
date: 2023-03-31T09:13:36.997Z
tags: 游戏
editor: ckeditor
dateCreated: 2023-03-31T09:13:36.997Z
-->
<h1>玩过得游戏存档碎碎念</h1>

331
算法/哈希.md Normal file
View File

@ -0,0 +1,331 @@
---
title: 哈希
description: 哈希算法
published: true
date: 2024-03-21T11:13:33.616Z
tags: 算法
editor: markdown
dateCreated: 2024-03-21T11:13:33.616Z
---
# 有效字母异位词
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1: 输入: s = "anagram", t = "nagaram" 输出: true
示例 2: 输入: s = "rat", t = "car" 输出: false
说明: 你可以假设字符串只包含小写字母。
```ts
function isAnagram(s: string, t: string): boolean {
if (s?.length !== t.length) return false;
let hashArr: number[] = new Array(26).fill(0);
const pivot: number = 'a'.charCodeAt(0);
for (let i = 0; i < s.length; i++) {
hashArr[s.charCodeAt(i) - pivot]++
hashArr[t.charCodeAt(i) - pivot]--
}
return hashArr.every((e: number) => e === 0);
};
```
# 两个数组的交集
给定两个数组,编写一个函数来计算它们的交集。
示例 1
输入nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
示例 2
输入nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[9,4]
解释:[4,9] 也是可通过的
## set
```ts
function intersection(nums1: number[], nums2: number[]): number[] {
const set: Set<number> = new Set();
const ans: Set<number> = new Set();
for (const num1 of nums1) {
set.add(num1);
}
for (const num2 of nums2) {
if (set.has(num2)) {
ans.add(num2);
}
}
return [...ans];
};
```
## filter
```ts
function intersection(nums1: number[], nums2: number[]): number[] {
return [...new Set(nums1.filter(i => nums2.includes(i)))]
};
```
# 开心数
编写一个算法来判断一个数 n 是不是快乐数。
「快乐数」 定义为:
对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
然后重复这个过程直到这个数变为 1也可能是 无限循环 但始终变不到 1。
如果这个过程 结果为 1那么这个数就是快乐数。
如果 n 是 快乐数 就返回 true ;不是,则返回 false 。
示例 1
输入n = 19
输出true
解释:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
示例 2
输入n = 2
输出false
## reduce算和+set判断存在
```ts
function isHappy(n: number): boolean {
const getN = (n) => {
return n?.toString()?.split('')?.reduce((sum: number, cur: number) => Number(sum) + cur ** 2, 0);
}
const Exit = new Set();
while (true) {
n = getN(n);
if (Exit.has(n)) return false;
if (n === 1) return true;
Exit.add(n)
}
};
```
## 余数算和+map判断存在
```ts
function isHappy(n: number): boolean {
const getN = (sum, n) => {
sum += (n % 10) ** 2
n = Math.floor(n / 10)
if (n) {
return getN(sum, n)
} else {
return sum
}
}
const M = new Map();
while (true) {
n = getN(0, n);
if (M.has(n)) return false;
if (n === 1) return true;
M.set(n, 1)
}
};
```
# 两数之和
给定一个整数数组 nums 和一个整数目标值 target请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例 1
输入nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2
输入nums = [3,2,4], target = 6
输出:[1,2]
示例 3
输入nums = [3,3], target = 6
输出:[0,1]
```ts
function twoSum(nums: number[], target: number): number[] {
const M = new Map();
let i = 0;
while (i < nums?.length) {
const val = target - nums[i]
if (M.has(val)) return [M.get(val), i]
M.set(nums[i], i++)
}
return [];
};
```
# 四数之和II
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
为了使问题简单化,所有的 A, B, C, D 具有相同的长度 N且 0 ≤ N ≤ 500 。所有整数的范围在 -2^28 到 2^28 - 1 之间,最终结果不会超过 2^31 - 1 。
例如:
输入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
输出:
2
解释:
两个元组如下:
(0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
(1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
#算法公开课
```ts
function fourSumCount(nums1: number[], nums2: number[], nums3: number[], nums4: number[]): number {
let helperMap: Map<number, number> = new Map();
let resNum: number = 0;
let tempVal: number | undefined;
for (let i of nums1) {
for (let j of nums2) {
tempVal = helperMap.get(i + j);
helperMap.set(i + j, tempVal ? tempVal + 1 : 1);
}
}
for (let k of nums3) {
for (let l of nums4) {
tempVal = helperMap.get(0 - (k + l));
if (tempVal) {
resNum += tempVal;
}
}
}
return resNum;
};
```
# 赎金信
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)
注意:
你可以假设两个字符串均只含有小写字母。
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
```ts
function canConstruct(ransomNote: string, magazine: string): boolean {
let ransomArr = new Array(26)?.fill(0);
ransomNote?.split('')?.forEach((e) => {
ransomArr[e.charCodeAt(0) - 'a'.charCodeAt(0)] += 1
})
magazine?.split('')?.forEach((e) => {
ransomArr[e.charCodeAt(0) - 'a'.charCodeAt(0)] -= 1
})
return !ransomArr.find(e => e > 0)
};
```
# 三数之和
给你一个包含 n 个整数的数组 nums判断 nums 中是否存在三个元素 abc ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
注意: 答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4]
满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]
```ts
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
let length = nums.length;
let left: number = 0,
right: number = length - 1;
let resArr: number[][] = [];
for (let i = 0; i < length; i++) {
if (nums[i]>0) {
return resArr; //nums经过排序后只要nums[i]>0, 此后的nums[i] + nums[left] + nums[right]均大于0,可以提前终止循环。
}
if (i > 0 && nums[i] === nums[i - 1]) {
continue;
}
left = i + 1;
right = length - 1;
while (left < right) {
let total: number = nums[i] + nums[left] + nums[right];
if (total === 0) {
resArr.push([nums[i], nums[left], nums[right]]);
left++;
right--;
while (nums[right] === nums[right + 1]) {
right--;
}
while (nums[left] === nums[left - 1]) {
left++;
}
} else if (total < 0) {
left++;
} else {
right--;
}
}
}
return resArr;
};
```
# 四数之和
题意:给定一个包含 n 个整数的数组 nums 和一个目标值 target判断 nums 中是否存在四个元素 abc 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:
答案中不可以包含重复的四元组。
示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
```ts
function fourSum(nums: number[], target: number): number[][] {
nums.sort((a, b) => a - b);
let first: number = 0,
second: number,
third: number,
fourth: number;
let length: number = nums.length;
let resArr: number[][] = [];
for (; first < length; first++) {
if (first > 0 && nums[first] === nums[first - 1]) {
continue;
}
for (second = first + 1; second < length; second++) {
if ((second - first) > 1 && nums[second] === nums[second - 1]) {
continue;
}
third = second + 1;
fourth = length - 1;
while (third < fourth) {
let total: number = nums[first] + nums[second] + nums[third] + nums[fourth];
if (total === target) {
resArr.push([nums[first], nums[second], nums[third], nums[fourth]]);
third++;
fourth--;
while (nums[third] === nums[third - 1]) third++;
while (nums[fourth] === nums[fourth + 1]) fourth--;
} else if (total < target) {
third++;
} else {
fourth--;
}
}
}
}
return resArr;
};
```

99
算法/字符串.md Normal file
View File

@ -0,0 +1,99 @@
---
title: 字符串
description:
published: true
date: 2024-03-21T11:16:17.692Z
tags: 算法
editor: markdown
dateCreated: 2024-03-21T11:16:17.692Z
---
# 反转字符串
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。
示例 1
输入:["h","e","l","l","o"]
输出:["o","l","l","e","h"]
示例 2
输入:["H","a","n","n","a","h"]
输出:["h","a","n","n","a","H"]
```ts
/**
Do not return anything, modify s in-place instead.
*/
function reverseString(s: string[]): void {
for (let i = 0, j = s.length - 1; i <= j; i++, j--) {
let tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
};
```
# 反转字符串II
给定一个字符串 s 和一个整数 k从字符串开头算起, 每计数至 2k 个字符,就反转这 2k 个字符中的前 k 个字符。
如果剩余字符少于 k 个,则将剩余字符全部反转。
如果剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符,其余字符保持原样。
示例:
输入: s = "abcdefg", k = 2
输出: "bacdfeg"
```ts
function reverseStr(s: string, k: number): string {
const str = s?.split('');
let flag = 0;
function reverseString(i: number, j: number): void {
for (; i <= j; i++, j--) {
let tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
};
while (str[flag]) {
if (str[flag + k - 1]) {
reverseString(flag, flag + k - 1)
} else {
reverseString(flag, str?.length - 1)
}
flag += 2 * k
}
return str.join('');
};
```
# 替换数字
给定一个字符串 s它包含小写字母和数字字符请编写一个函数将字符串中的字母字符保持不变而将每个数字字符替换为number。
例如,对于输入字符串 "a1b2c3",函数应该将其转换为 "anumberbnumbercnumber"。
对于输入字符串 "a5b",函数应该将其转换为 "anumberb"
输入:一个字符串 s,s 仅包含小写字母和数字字符。
输出打印一个新的字符串其中每个数字字符都被替换为了number
样例输入a1b2c3
样例输出anumberbnumbercnumber
数据范围1 <= s.length < 10000
```ts
const replaceNubmer=(s)=>{
let arr = s.split('');
let str = [];
for(let i = 0;i<arr.length;i++){
if(isNaN(arr[i])){
str=[...str,arr[i]]
}else{
str=[...str,'number']
}
}
return str.join('')
}
```

175
算法/数组.md Normal file
View File

@ -0,0 +1,175 @@
---
title: 数组
description:
published: true
date: 2024-03-21T11:15:46.175Z
tags: 算法
editor: markdown
dateCreated: 2024-03-21T11:15:46.175Z
---
# 二分法
```ts
function search(nums: number[], target: number): number {
let left:number = 0;
let right:number = nums.length - 1;
while (left <= right) {
let mid:number = left + ((right - left) >> 1);
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid - 1;
} else {
return mid;
}
}
return -1;
};
```
# 移除元素
给你一个数组 nums 和一个值 val你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
```ts
function removeElement(nums: number[], val: number): number {
let fast: number = 0, slow: number = 0;
while (fast < nums.length) {
if (nums[fast] !== val) {
nums[slow++]=nums[fast]
}
fast++;
}
return slow;
};
```
# 有序数组的平方
给你一个按 非递减顺序 排序的整数数组 nums返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。
示例 1
输入nums = [-4,-1,0,3,10]
输出:[0,1,9,16,100]
解释:平方后,数组变为 [16,1,0,9,100],排序后,数组变为 [0,1,9,16,100]
示例 2
输入nums = [-7,-3,2,3,11]
输出:[4,9,9,49,121]
```ts
function sortedSquares(nums: number[]): number[] {
let resArr: number[] = [];
let i: number = 0, j: number = nums?.length - 1;
while (i <= j) {
if (Math.abs(nums[i]) >= nums[j]) {
resArr.unshift(nums[i] ** 2)
i++;
} else {
resArr.unshift(nums[j] ** 2)
j--;
}
}
return resArr;
};
```
# 长度最小的子数组
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。
示例:
输入s = 7, nums = [2,3,1,2,4,3]
输出2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
```ts
function minSubArrayLen(target: number, nums: number[]): number {
let res: number = nums.length + 1;
let sum: number = 0;
let i: number = 0;
for (let j = 0; j < nums?.length; j++) {
sum += nums[j];
if (sum >= target) {
while (sum - nums[i] >= target) {
sum -= nums[i++];
}
res = Math.min(res, j - i + 1);
}
}
return res === nums.length + 1 ? 0 : res;
};
```
# 螺旋矩阵II
给定一个正整数 n生成一个包含 1 到 n^2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
示例:
输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]
```ts
function generateMatrix(n: number): number[][] {
let di: number = 0;
const res: number[][] = new Array(n).fill(1).map(i => new Array(n));
let num = 1;
while (di <= n >> 1) {
for (let j: number = di; j < n - di - 1; j++) {
res[di][j] = num++;
}
for (let j: number = di; j < n - di - 1; j++) {
res[j][n - di - 1] = num++;
}
for (let j: number = n - di - 1; j > di; j--) {
res[n - di - 1][j] = num++;
}
for (let j: number = n - di - 1; j > di; j--) {
res[j][di] = num++;
}
di++
}
if (n % 2 === 1) {
res[di - 1][di - 1] = num;
}
return res;
};
```
# 三数之和
给你一个包含 n 个整数的数组 nums判断 nums 中是否存在三个元素 abc ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
注意: 答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4]
满足要求的三元组集合为: [ [-1, 0, 1], [-1, -1, 2] ]
```ts
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
let length = nums.length;
let left: number = 0,
right: number = length - 1;
let resArr: number[][] = [];
for (let i = 0; i < length; i++) {
if (nums[i]>0) {
return resArr; //nums经过排序后只要nums[i]>0, 此后的nums[i] + nums[left] + nums[right]均大于0,可以提前终止循环。
}
if (i > 0 && nums[i] === nums[i - 1]) {
continue;
}
left = i + 1;
right = length - 1;
while (left < right) {
let total: number = nums[i] + nums[left] + nums[right];
if (total === 0) {
resArr.push([nums[i], nums[left], nums[right]]);
left++;
right--;
while (nums[right] === nums[right + 1]) {
right--;
}
while (nums[left] === nums[left - 1]) {
left++;
}
} else if (total < 0) {
left++;
} else {
right--;
}
}
}
return resArr;
};
```

508
算法/链表.md Normal file
View File

@ -0,0 +1,508 @@
---
title: 链表
description:
published: true
date: 2024-03-21T11:15:17.596Z
tags: 算法
editor: markdown
dateCreated: 2024-03-21T11:15:17.596Z
---
# 移除链表元素
题意:删除链表中等于给定值 val 的所有节点。
示例 1 输入head = [1,2,6,3,4,5,6], val = 6 输出:[1,2,3,4,5]
示例 2 输入head = [], val = 1 输出:[]
示例 3 输入head = [7,7,7,7], val = 7 输出:[]
```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function removeElements(head: ListNode | null, val: number): ListNode | null {
let now = head;
while (now?.next) {
if (now.next.val === val) {
now.next = now.next.next
}else{
now = now.next
}
}
if (head?.val === val) {
head = head?.next
}
return head;
};
```
# 设计链表
在链表类中实现这些功能:
get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度则不会插入节点。如果index小于0则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
## 我的解法
```ts
// class ListNode {
// public val: number;
// public next: ListNode | null;
// constructor(val?: number, next?: ListNode | null) {
// this.val = val === undefined ? 0 : val;
// this.next = next === undefined ? null : next;
// }
// }
class MyLinkedList {
private head: ListNode | null;
constructor(head?: ListNode | null) {
this.head = head !== undefined ? head : null;
}
get(index: number): number {
let nowIndex = 0;
let now = this.head;
while (nowIndex++ < index) {
if (now.next === null) return -1;
now = now.next;
}
console.log('now', now)
return now?.val !== undefined ? now?.val : -1;
}
addAtHead(val: number): void {
let now = new ListNode();
now.val = val;
now.next = this.head;
this.head = now;
}
addAtTail(val: number): void {
let insertNode = new ListNode();
let virHead = new ListNode();
virHead.next = this.head;
let now = virHead;
while (now.next !== null) {
now = now.next;
}
insertNode.val = val;
now.next = insertNode;
this.head = virHead.next;
}
addAtIndex(index: number, val: number): void {
let nowIndex = -1;
let insertNode = new ListNode();
let virHead = new ListNode();
virHead.next = this.head;
let now = virHead;
while (nowIndex++ < index - 1) {
if (now.next === null) return;
now = now.next;
}
insertNode.next = now?.next || null;
insertNode.val = val;
now.next = insertNode;
this.head = virHead.next;
}
deleteAtIndex(index: number): void {
let nowIndex = -1;
let virHead = new ListNode();
virHead.next = this.head;
let now = virHead;
while (nowIndex++ < index - 1) {
if (now.next === null) return;
now = now.next;
}
now.next = now.next?.next || null;
this.head = virHead.next;
}
}
```
## 带size
```ts
class LinkedNode {
public val: number;
public next: LinkedNode;
constructor(val: number) {
this.val = val;
this.next = null;
}
}
class MyLinkedList {
private size: number
private head: LinkedNode;
constructor() {
this.size = 0;
this.head = new LinkedNode(-1);
}
get(index: number): number {
if (index < 0 || index >= this.size) {
return -1;
}
let curr = this.head;
let i = 0;
while (i < index + 1) {
curr = curr.next;
i++;
}
return curr.val;
}
addAtHead(val: number): void {
this.addAtIndex(0, val);
}
addAtTail(val: number): void {
this.addAtIndex(this.size, val);
}
addAtIndex(index: number, val: number): void {
if (index < 0 || index > this.size ) {
return;
}
let curr = this.head;
let i = 0;
while (i < index) {
curr = curr.next;
i++;
}
const node = new LinkedNode(val);
node.next = curr.next;
curr.next = node;
this.size++;
}
deleteAtIndex(index: number): void {
if (index < 0 || index >= this.size) {
return;
}
let curr = this.head;
let i = 0;
while (i < index) {
curr = curr.next;
i++;
}
curr.next = curr.next.next;
this.size--;
}
}
```
## 带tail
```ts
// class ListNode {
// public val: number;
// public next: ListNode | null;
// constructor(val?: number, next?: ListNode | null) {
// this.val = val === undefined ? 0 : val;
// this.next = next === undefined ? null : next;
// }
// }
class MyLinkedList {
// 记录链表长度
private size: number;
private head: ListNode | null;
private tail: ListNode | null;
constructor() {
this.size = 0;
this.head = null;
this.tail = null;
}
// 获取链表中第 index个节点的值
get(index: number): number {
// 索引无效的情况
if (index < 0 || index >= this.size) {
return -1;
}
let curNode = this.getNode(index);
// 这里在前置条件下,理论上不会出现 null的情况
return curNode.val;
}
// 在链表的第一个元素之前添加一个值为 val的节点。插入后新节点将成为链表的第一个节点。
addAtHead(val: number): void {
let node: ListNode = new ListNode(val, this.head);
this.head = node;
if (!this.tail) {
this.tail = node;
}
this.size++;
}
// 将值为 val 的节点追加到链表的最后一个元素。
addAtTail(val: number): void {
let node: ListNode = new ListNode(val, null);
if (this.tail) {
this.tail.next = node;
} else {
// 还没有尾节点,说明一个节点都还没有
this.head = node;
}
this.tail = node;
this.size++;
}
// 在链表中的第 index个节点之前添加值为 val的节点。
// 如果 index等于链表的长度则该节点将附加到链表的末尾。如果 index大于链表长度则不会插入节点。如果 index小于0则在头部插入节点。
addAtIndex(index: number, val: number): void {
if (index === this.size) {
this.addAtTail(val);
return;
}
if (index > this.size) {
return;
}
// <= 0 的情况都是在头部插入
if (index <= 0) {
this.addAtHead(val);
return;
}
// 正常情况
// 获取插入位置的前一个 node
let curNode = this.getNode(index - 1);
let node: ListNode = new ListNode(val, curNode.next);
curNode.next = node;
this.size++;
}
// 如果索引 index有效则删除链表中的第 index个节点。
deleteAtIndex(index: number): void {
if (index < 0 || index >= this.size) {
return;
}
// 处理头节点
if (index === 0) {
this.head = this.head!.next;
// 如果链表中只有一个元素,删除头节点后,需要处理尾节点
if (index === this.size - 1) {
this.tail = null
}
this.size--;
return;
}
// 索引有效
let curNode: ListNode = this.getNode(index - 1);
curNode.next = curNode.next!.next;
// 处理尾节点
if (index === this.size - 1) {
this.tail = curNode;
}
this.size--;
}
// 获取指定 Node节点
private getNode(index: number): ListNode {
// 这里不存在没办法获取到节点的情况,都已经在前置方法做过判断
// 创建虚拟头节点
let curNode: ListNode = new ListNode(0, this.head);
for (let i = 0; i <= index; i++) {
// 理论上不会出现 null
curNode = curNode.next!;
}
return curNode;
}
}
```
# 翻转链表
## 双指针
```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function reverseList(head: ListNode | null): ListNode | null {
let preNode: ListNode | null = null,
curNode: ListNode | null = head,
tempNode: ListNode | null;
while (curNode) {
tempNode = curNode.next;
curNode.next = preNode;
preNode = curNode;
curNode = tempNode;
}
return preNode;
};
```
## 递归
```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function reverseList(head: ListNode | null): ListNode | null {
function reserve(tail, head) {
if (head === null) {
return tail;
}
let tmp = head.next;
head.next = tail;
tail = head;
return reserve(tail, tmp)
}
return reserve(null, head)
};
```
# 两两交换链表中的节点
示例 1
输入head = [1,2,3,4]
输出:[2,1,4,3]
示例 2
输入head = []
输出:[]
示例 3
输入head = [1]
输出:[1]
```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function swapPairs(head: ListNode | null): ListNode | null {
let virHead = new ListNode();
virHead.next = head;
let cur = virHead;
while (cur.next && cur.next.next) {
let temp = cur.next;
let temp2Next = cur.next.next.next;
cur.next = temp.next;
temp.next.next = temp;
temp.next = temp2Next;
cur = cur.next.next
}
return virHead.next
};
```
# 删除链表的倒数第N个节点
输入head = [1,2,3,4,5], n = 2 输出:[1,2,3,5] 示例 2
输入head = [1], n = 1 输出:[] 示例 3
输入head = [1,2], n = 1 输出:[1]
```ts
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
let times = 0;
const virHead = new ListNode();
virHead.next = head;
let tail = virHead;
let del = virHead;
while (times < n && tail?.next) {
tail = tail.next;
times++
}
if (times < n) return head;
while (tail.next) {
del = del.next;
tail = tail.next;
}
del.next = del.next.next
return virHead.next;
};
```
# 链表相交
![32TO.jpeg](https://img.ocer.cc/images/2024/02/21/32TO.jpeg)
```js
var getIntersectionNode = function (headA, headB) {
let curA = headA;
let curB = headB;
let sizeA = 0, sizeB = 0;
while (curA?.next) {
curA = curA.next;
sizeA++
}
while (curB?.next) {
curB = curB.next;
sizeB++
}
const diffNum = Math.abs(sizeA - sizeB);
curA = headA;
curB = headB;
if (sizeA >= sizeB) {
for (let n = 0; n < diffNum; n++) {
curA = curA.next;
}
} else {
for (let n = 0; n < diffNum; n++) {
curB = curB.next;
}
}
while (curA !== curB) {
curA = curA?.next;
curB = curB?.next;
}
return curA;
};
```
# 环形链表II
![39SV.jpeg](https://img.ocer.cc/images/2024/02/21/39SV.jpeg)
```ts
function detectCycle(head: ListNode | null): ListNode | null {
let fast = head, slow = head;
while (fast !== null && fast.next !== null) {
fast = fast.next.next;
slow = slow.next;
if (fast === slow) {
let index1 = head;
let index2 = fast;
while (index1 !== index2) {
index1 = index1.next;
index2 = index2.next;
}
return index2
}
}
return null;
};
```