Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 44c60a5

Browse files
feat: add go solution to lcof2 problem: No.114 (doocs#3582)
1 parent fc9ce90 commit 44c60a5

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

‎lcof2/剑指 Offer II 114. 外星文字典/README.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,68 @@ public:
287287
};
288288
```
289289
290+
#### Go
291+
292+
```go
293+
func alienOrder(words []string) string {
294+
n := len(words)
295+
if n == 0 {
296+
return ""
297+
}
298+
if n == 1 {
299+
return words[0]
300+
}
301+
inDegree := make(map[byte]int)
302+
graph := make(map[byte][]byte)
303+
for _, word := range words {
304+
for i := 0; i < len(word); i++ {
305+
inDegree[word[i]] = 0
306+
}
307+
}
308+
for i := 0; i < n-1; i++ {
309+
w1, w2 := words[i], words[i+1]
310+
minLen := len(w1)
311+
if len(w2) < minLen {
312+
minLen = len(w2)
313+
}
314+
foundDifference := false
315+
for j := 0; j < minLen; j++ {
316+
if w1[j] != w2[j] {
317+
inDegree[w2[j]]++
318+
graph[w1[j]] = append(graph[w1[j]], w2[j])
319+
foundDifference = true
320+
break
321+
}
322+
}
323+
if !foundDifference && len(w1) > len(w2) {
324+
return ""
325+
}
326+
}
327+
queue := make([]byte, 0)
328+
for k := range inDegree {
329+
if inDegree[k] == 0 {
330+
queue = append(queue, k)
331+
}
332+
}
333+
res := make([]byte, 0)
334+
for len(queue) > 0 {
335+
node := queue[0]
336+
queue = queue[1:]
337+
res = append(res, node)
338+
for _, next := range graph[node] {
339+
inDegree[next]--
340+
if inDegree[next] == 0 {
341+
queue = append(queue, next)
342+
}
343+
}
344+
}
345+
if len(res) != len(inDegree) {
346+
return ""
347+
}
348+
return string(res)
349+
}
350+
```
351+
290352
<!-- tabs:end -->
291353

292354
<!-- solution:end -->
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
func alienOrder(words []string) string {
2+
n := len(words)
3+
if n == 0 {
4+
return ""
5+
}
6+
if n == 1 {
7+
return words[0]
8+
}
9+
inDegree := make(map[byte]int)
10+
graph := make(map[byte][]byte)
11+
for _, word := range words {
12+
for i := 0; i < len(word); i++ {
13+
inDegree[word[i]] = 0
14+
}
15+
}
16+
for i := 0; i < n-1; i++ {
17+
w1, w2 := words[i], words[i+1]
18+
minLen := len(w1)
19+
if len(w2) < minLen {
20+
minLen = len(w2)
21+
}
22+
foundDifference := false
23+
for j := 0; j < minLen; j++ {
24+
if w1[j] != w2[j] {
25+
inDegree[w2[j]]++
26+
graph[w1[j]] = append(graph[w1[j]], w2[j])
27+
foundDifference = true
28+
break
29+
}
30+
}
31+
if !foundDifference && len(w1) > len(w2) {
32+
return ""
33+
}
34+
}
35+
queue := make([]byte, 0)
36+
for k := range inDegree {
37+
if inDegree[k] == 0 {
38+
queue = append(queue, k)
39+
}
40+
}
41+
res := make([]byte, 0)
42+
for len(queue) > 0 {
43+
node := queue[0]
44+
queue = queue[1:]
45+
res = append(res, node)
46+
for _, next := range graph[node] {
47+
inDegree[next]--
48+
if inDegree[next] == 0 {
49+
queue = append(queue, next)
50+
}
51+
}
52+
}
53+
if len(res) != len(inDegree) {
54+
return ""
55+
}
56+
return string(res)
57+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /