Skip to content

Commit 068c061

Browse files
committed
不知道是些什么代码了
1 parent 49c4998 commit 068c061

File tree

6 files changed

+161
-1
lines changed

6 files changed

+161
-1
lines changed

BinaryTree/sortedListToBST.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
class TreeNode {
10+
val: number;
11+
left: TreeNode | null;
12+
right: TreeNode | null;
13+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
14+
this.val = val === undefined ? 0 : val;
15+
this.left = left === undefined ? null : left;
16+
this.right = right === undefined ? null : right;
17+
}
18+
}
19+
20+
function sortedListToBST(head: ListNode | null): TreeNode | null {
21+
if (!head) {
22+
return null;
23+
}
24+
if (!head.next) {
25+
return new TreeNode(head.val);
26+
}
27+
if (!head.next.next) {
28+
let t = new TreeNode(head.val);
29+
t.left = new TreeNode(head.next.val);
30+
return t;
31+
}
32+
let pre: ListNode = head;
33+
let p: ListNode = pre.next;
34+
let q = p.next;
35+
// 一次跳两个元素,找到链表的中间元素p
36+
while (p && q) {
37+
pre = pre.next;
38+
p = p.next;
39+
q = q.next.next;
40+
}
41+
console.log(p);
42+
//
43+
pre.next = null;
44+
let root = new TreeNode(p.val);
45+
root.left = sortedListToBST(head);
46+
root.right = sortedListToBST(p.next);
47+
return root;
48+
}
49+
50+
const LL = {
51+
val: -10,
52+
next: {
53+
val: -3,
54+
next: {
55+
val: 0,
56+
next: {
57+
val: 5,
58+
next: {
59+
val: 9,
60+
next: null
61+
}
62+
}
63+
}
64+
}
65+
}
66+
console.log(JSON.stringify(sortedListToBST(LL)));

LinkedList.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,5 @@ const LinkedList = function () {
171171
this.getHead = function () {
172172
return head;
173173
};
174-
};
174+
};
175+
module.exports = LinkedList;

huawei/intersection.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const source = [
2+
[0, 3],
3+
[1, 3],
4+
[3, 5],
5+
[3, 6]
6+
];
7+
source.sort((a, b) => a[0] > b[0]);
8+
function Intersection(arr) {
9+
const len = arr.length;
10+
let res = [];
11+
let flag = true;
12+
for (let i = 0; i < len - 1; i++) {
13+
for (let j = i + 1; j < len; j++) {
14+
if (arr[i][1] >= arr[j][0]) {
15+
res.push([arr[j][0], arr[i][1]]);
16+
flag = false;
17+
}
18+
}
19+
}
20+
// 处理没有交集的情况
21+
if (flag) {
22+
return 'None';
23+
}
24+
// 去除重复的交集
25+
res = filter(res);
26+
// 结果取并集,这里做错了
27+
return merge(res);
28+
}
29+
function filter(arr) {
30+
const len = arr.length;
31+
if (len === 0) {
32+
return arr;
33+
}
34+
const newArr = [];
35+
const strArr = [];
36+
newArr.push(arr[0]);
37+
strArr.push(JSON.stringify(arr[0]));
38+
for (let i = 1; i < len; i++) {
39+
if (strArr.indexOf(JSON.stringify(arr[i])) === -1) {
40+
newArr.push(arr[i]);
41+
strArr.push(JSON.stringify(arr[i]));
42+
}
43+
}
44+
return newArr;
45+
}
46+
47+
// var merge = function (intervals) {
48+
// if (!intervals.length) return []
49+
// if (intervals.length === 1) return intervals
50+
// intervals.sort((a, b) => a[0] - b[0])
51+
// let start = intervals[0][0], end = intervals[0][1], res = []
52+
// for (let interval of intervals) {
53+
// if (interval[0] <= end) {
54+
// end = Math.max(end, interval[1])
55+
// } else {
56+
// res.push([start, end])
57+
// start = interval[0]
58+
// end = interval[1]
59+
// }
60+
// }
61+
// res.push([start, end])
62+
// return res
63+
// };
64+
65+
const result = Intersection(source);
66+
result.sort((a, b) => a[0] > b[0]);
67+
if (result !== 'None') {
68+
for (const item of result) {
69+
console.log(item.join(' '));
70+
}
71+
} else {
72+
console.log('None');
73+
}

strings/countSubstrings.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function countSubstrings(s: string): number {
2+
let count = 0;
3+
const dp: number[] = [];
4+
for (let i = 0; i < s.length; i++) {
5+
dp[i] = 1;
6+
// 每一个
7+
count ++;
8+
for (let j = 0; j < i; j++) {
9+
if (s[j] === s[i] && dp[i] === 1) {
10+
dp[j] = 1;
11+
count++;
12+
} else {
13+
dp[j] = 0;
14+
}
15+
}
16+
console.log(dp);
17+
}
18+
return count;
19+
};
20+
console.log(countSubstrings('asddsaasd'));
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)