李恒道
发表于 2024-10-21 19:59:46
https://leetcode.cn/problems/number-of-provinces/submissions/574572111/
一眼并查集
但是读错题意了...
```js
class UnionFind {
//用来解决不同节点间是否拥有相同根的问题
constructor(n) {
this.parent = [] //并查集
this.size = [] //每个节点下拥有的总节点数目
this.count = 0
this.init(n)
}
//初始化一个并查集
init(n) {
this.parent.length = 0
for (let i = 0; i < n; i++) {
this.parent = i
}
this.count = n
this.size = new Array(n).fill(1)
}
// 在并查集中找到节点node的根节点
find(node) {
while (node != this.parent) {
// 路径压缩,每次查找时都将子节点的父节点设置为父节点的父节点。这样能够不停的扁平化查询树。
this.parent = this.parent]
node = this.parent
}
return node
}
//判断两个节点的根节点是不是同一个
same(left, right) {
return this.find(left) == this.find(right)
}
// 将两个节点关联起来,即两个节点共有一个根节点.和并是将两个节点的根节点合并起来
union(left, right) {
let l = this.find(left)
let r = this.find(right)
if (l != r) {
// 左边较小,因此将左边合并到右边的树上
if (this.size < this.size) {
this.parent = r
this.size += this.size
} else {
this.parent = l
this.size += this.size
}
// 连通分量减1
this.count--
return true
}
return false
}
//获取总共分了多少组
getCount() {
return this.count
}
}
var findCircleNum = function (isConnected) {
const union = new UnionFind(isConnected.length);
for (let index = 0; index < isConnected.length; index++) {
for (let indey = 0; indey < isConnected.length; indey++) {
if (isConnected == 1) {
union.union(index, indey)
}
}
}
return union.getCount()
};```
李恒道
发表于 2024-10-21 20:48:35
https://leetcode.cn/problems/valid-anagram/submissions/574588789/?envType=study-plan-v2&envId=programming-skills
简单题
打完收工
```go
func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
var cache = make(mapint32, 0)
for _, v := range s {
cache = cache + 1
}
for _, v := range t {
cache = cache - 1
if cache < 0 {
return false
}
}
return true
}
```
李恒道
发表于 2024-10-22 08:27:54
https://leetcode.cn/problems/count-pairs-that-form-a-complete-day-i/submissions/574677238/
简单题
```js
var countCompleteDayPairs = function (hours) {
const cache = new Map();
let result = 0;
for (let index = 0; index < hours.length; index++) {
const hour = hours % 24;
const target=hour==0?0:24 - hour
if (cache.has(target)) {
result += cache.get(target);
}
cache.set(hour, (cache.get(hour) ?? 0) + 1);
}
return result
};
```
李恒道
发表于 2024-10-22 15:47:11
https://leetcode.cn/problems/count-pairs-that-form-a-complete-day-ii/description/
同样的题
秒了
```js
var countCompleteDayPairs = function (hours) {
const cache = new Map();
let result = 0;
for (let index = 0; index < hours.length; index++) {
const hour = hours % 24;
const target=hour==0?0:24 - hour
if (cache.has(target)) {
result += cache.get(target);
}
cache.set(hour, (cache.get(hour) ?? 0) + 1);
}
return result
};```
李恒道
发表于 2024-10-22 16:03:16
https://leetcode.cn/problems/add-binary/submissions/574803015/?envType=study-plan-v2&envId=top-interview-150
简单题
秒了
```js
var addBinary = function (a, b) {
a = a
.split("")
.reverse()
.map((i) => parseInt(i));
b = b
.split("")
.reverse()
.map((i) => parseInt(i));
let extra = 0;
let len = Math.max(a.length, b.length);
let result = "";
for (let index = 0; index < len; index++) {
const bit1 = a;
const bit2 = b;
const newBit = (bit1 ?? 0) + (bit2 ?? 0) + extra;
result = (newBit % 2) + result;
extra = Math.floor(newBit / 2);
}
if(extra!==0){
result = extra + result;
}
return result;
};
```
李恒道
发表于 2024-10-22 16:13:49
https://leetcode.cn/problems/single-number-iii/submissions/574807196/
这题没想出来
答案真巧妙
```js
var singleNumber = function (nums) {
let total = 0;
for (const num of nums) {
total ^= num;
}
const bit = total & -total;
const ret = ;
for (const num of nums) {
ret[(num & bit) !== 0 ? 1 : 0] ^= num;
}
return ret;
};
```
李恒道
发表于 2024-10-22 16:27:14
https://leetcode.cn/problems/interleaving-string/submissions/574814157/?envType=study-plan-v2&envId=top-interview-150
dfs过99%....
```js
var isInterleave = function (s1, s2, s3) {
const cache = new Map();
const dfs = (p1, p2, p3) => {
if (p1 == s1.length && p2 == s2.length && p3 == s3.length) {
return true;
}
const flag = `${p1} ${p2} ${p3}`;
if (cache.has(flag)) {
return cache.get(flag);
}
if (s1 == s3 && s1 !== undefined) {
//可能是
let result = dfs(p1 + 1, p2, p3 + 1);
if (result == true) {
cache.set(flag, true);
return true;
}
}
if (s2 == s3 && s2 !== undefined) {
//可能是
let result = dfs(p1, p2 + 1, p3 + 1);
if (result == true) {
cache.set(flag, true);
return true;
}
}
cache.set(flag, false);
return false;
};
return dfs(0, 0, 0);
};
```
王一之
发表于 2024-10-22 17:16:19
李恒道 发表于 2024-10-22 16:27
https://leetcode.cn/problems/interleaving-string/submissions/574814157/?envType=study-plan-v2&en ...
多少名了
李恒道
发表于 2024-10-22 19:03:51
王一之 发表于 2024-10-22 17:16
多少名了
还没排名...
今天刚好250题
李恒道
发表于 2024-10-22 19:44:32
王一之 发表于 2024-10-22 17:16
多少名了
说实话逐渐开始感觉有点到瓶颈了
很多题都是知道解法就起手式秒
不知道怎么扣都抠不出来
完全不是让练习的
单点突破问题全tm背板