Skip to content

Commit 31ae657

Browse files

24 files changed

+426
-276
lines changed

Jf1JuT/index.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
export default function alienOrder(words: string[]): string {
2+
const { edges, indegrees, valid, chars } = createEdgesAndIndegrees(words);
3+
4+
if (!valid) return "";
5+
6+
const order = topologicalsort(edges, indegrees, chars);
7+
8+
return order.length === chars.size ? order.join("") : "";
9+
}
10+
11+
function createEdgesAndIndegrees(words: string[]): {
12+
edges: Map<string, Set<string>>;
13+
indegrees: Map<string, number>;
14+
valid: boolean;
15+
chars: Set<string>;
16+
} {
17+
const chars: Set<string> = new Set();
18+
let valid = true;
19+
20+
const edges = new Map<string, Set<string>>();
21+
22+
const indegrees = new Map<string, number>();
23+
for (const word of words) {
24+
Array.prototype.forEach.call(word, (c: string) => {
25+
chars.add(c);
26+
});
27+
}
28+
for (let i = 0, j = 1; i < words.length && j < words.length; i++, j++) {
29+
const word1 = words[i];
30+
31+
const word2 = words[j];
32+
33+
if (word1.startsWith(word2) && word2.length < word1.length) {
34+
valid = false;
35+
return { valid, edges, indegrees, chars };
36+
}
37+
38+
for (let k = 0; k < word1.length && k < word2.length; k++) {
39+
const char1 = word1[k];
40+
const char2 = word2[k];
41+
42+
if (char1 !== char2) {
43+
edges.set(char1, (edges.get(char1) || new Set()).add(char2));
44+
45+
break;
46+
}
47+
}
48+
}
49+
const parents = new Map<string, Set<string>>();
50+
for (const [parent, children] of edges) {
51+
for (const child of children) {
52+
parents.set(child, (parents.get(child) || new Set()).add(parent));
53+
}
54+
}
55+
parents.forEach((p, child) => {
56+
indegrees.set(child, p.size);
57+
});
58+
return { valid, edges, indegrees, chars };
59+
}
60+
61+
function topologicalsort(
62+
edges: Map<string, Set<string>>,
63+
indegrees: Map<string, number>,
64+
chars: Set<string>,
65+
): string[] {
66+
const order: string[] = [];
67+
68+
let queue = [...chars.keys()].filter((key) => !indegrees.has(key));
69+
70+
while (queue.length) {
71+
const temp: string[] = [];
72+
for (const char of queue) {
73+
order.push(char);
74+
const children = edges.get(char);
75+
if (children) {
76+
for (const child of children) {
77+
const indegree = -1 + (indegrees.get(child) ?? 0);
78+
79+
indegrees.set(child, indegree);
80+
if (indegree === 0) {
81+
temp.push(child);
82+
}
83+
}
84+
}
85+
}
86+
queue = temp;
87+
}
88+
89+
return order;
90+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@ https://leetcode.cn/problems/remove-nth-node-from-end-of-list
586586

587587
https://leetcode.cn/problems/check-if-string-is-a-prefix-of-array/
588588

589+
https://leetcode.cn/problems/Jf1JuT/
590+
591+
https://leetcode.cn/problems/alien-dictionary/
592+
589593
#### 安装教程
590594

591595
1. 安装`deno`

alien-dictionary/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "../Jf1JuT/index.ts";

basic-calculator/BinaryExpression.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Expression } from "./Expression.ts";
2+
3+
export interface BinaryExpression {
4+
type: "BinaryExpression";
5+
operator:
6+
| "+"
7+
| "-"
8+
| "/"
9+
| "%"
10+
| "*"
11+
| "**"
12+
| "&"
13+
| "|"
14+
| ">>"
15+
| ">>>"
16+
| "<<"
17+
| "^"
18+
| "=="
19+
| "==="
20+
| "!="
21+
| "!=="
22+
| "in"
23+
| "instanceof"
24+
| ">"
25+
| "<"
26+
| ">="
27+
| "<="
28+
| "|>";
29+
left: Expression;
30+
right: Expression;
31+
}

basic-calculator/Expression.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { BinaryExpression } from "./BinaryExpression.ts";
2+
import { NumericLiteral } from "./NumericLiteral.ts";
3+
import { ParenthesizedExpression } from "./ParenthesizedExpression.ts";
4+
import { UnaryExpression } from "./UnaryExpression.ts";
5+
6+
export type Expression =
7+
| BinaryExpression
8+
| NumericLiteral
9+
| UnaryExpression
10+
| ParenthesizedExpression;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { BinaryExpression } from "./BinaryExpression.ts";
2+
import { UnaryExpression } from "./UnaryExpression.ts";
3+
4+
export type ExpressionOperator =
5+
| UnaryExpression["operator"]
6+
| BinaryExpression["operator"];

basic-calculator/ExpressionType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Expression } from "./Expression.ts";
2+
3+
export type ExpressionType = Expression["type"];

basic-calculator/NumericLiteral.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface NumericLiteral {
2+
type: "NumericLiteral";
3+
value: number;
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Expression } from "./Expression.ts";
2+
3+
export interface ParenthesizedExpression {
4+
type: "ParenthesizedExpression";
5+
expression: Expression;
6+
}

basic-calculator/State.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const enum State {
2+
"initial",
3+
"unary",
4+
"parentheses",
5+
"number",
6+
"binary",
7+
"unknown",
8+
}

0 commit comments

Comments
 (0)