Skip to content

Commit d14ad49

Browse files
author
chen-shiwei
committed
Merge branch 'master' of github.com:chen-shiwei/leetcode
2 parents 32bb718 + 1a9f667 commit d14ad49

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package _322_重新安排行程
2+
3+
import (
4+
"sort"
5+
)
6+
7+
type Airport struct {
8+
airport string
9+
visited bool
10+
}
11+
12+
type Airports []Airport
13+
14+
func (a Airports) Len() int {
15+
return len(a)
16+
}
17+
18+
func (a Airports) Swap(i, j int) {
19+
a[i], a[j] = a[j], a[i]
20+
return
21+
}
22+
func (a Airports) Less(i, j int) bool {
23+
return a[i].airport < a[j].airport
24+
}
25+
26+
func findItinerary(tickets [][]string) []string {
27+
var (
28+
path []string
29+
fn func(curTicket string, ticketN int) bool
30+
targets = make(map[string]Airports)
31+
)
32+
33+
for _, ticket := range tickets {
34+
if _, ok := targets[ticket[0]]; !ok {
35+
targets[ticket[0]] = make(Airports, 0)
36+
}
37+
targets[ticket[0]] = append(targets[ticket[0]], Airport{
38+
airport: ticket[1],
39+
visited: false,
40+
})
41+
}
42+
43+
for _, target := range targets {
44+
sort.Sort(target)
45+
}
46+
47+
path = append(path, "JFK")
48+
49+
fn = func(curTicket string, ticketN int) bool {
50+
if ticketN == len(tickets) {
51+
return true
52+
}
53+
for i, target := range targets[curTicket] {
54+
if target.visited {
55+
continue
56+
}
57+
path = append(path, target.airport)
58+
targets[curTicket][i].visited = true
59+
if fn(target.airport, ticketN+1) {
60+
return true
61+
}
62+
targets[curTicket][i].visited = false
63+
path = path[:len(path)-1]
64+
}
65+
66+
return false
67+
}
68+
69+
fn("JFK", 0)
70+
71+
return path
72+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package _322_重新安排行程
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_findItinerary(t *testing.T) {
9+
type args struct {
10+
tickets [][]string
11+
}
12+
tests := []struct {
13+
name string
14+
args args
15+
want []string
16+
}{
17+
{name: `[["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]`, args: args{tickets: [][]string{{"JFK", "SFO"}, {"JFK", "ATL"}, {"SFO", "ATL"}, {"ATL", "JFK"}, {"ATL", "SFO"}}}, want: []string{"JFK", "ATL", "JFK", "SFO", "ATL", "SFO"}},
18+
}
19+
for _, tt := range tests {
20+
t.Run(tt.name, func(t *testing.T) {
21+
if got := findItinerary(tt.args.tickets); !reflect.DeepEqual(got, tt.want) {
22+
t.Errorf("findItinerary() = %v, want %v", got, tt.want)
23+
}
24+
})
25+
}
26+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package _1_N皇后_回溯
2+
3+
import "fmt"
4+
5+
func solveNQueens(n int) [][]string {
6+
var (
7+
paths [][]string
8+
path = make([][]byte, n)
9+
fn func(col, row int)
10+
)
11+
12+
b := make([]byte, n)
13+
for i := 0; i < n; i++ {
14+
b[i] = '.'
15+
}
16+
17+
for i := range path {
18+
c := make([]byte, n)
19+
copy(c, b)
20+
path[i] = c
21+
}
22+
23+
fn = func(col, row int) {
24+
if row == n {
25+
var tmp []string
26+
for _, v := range path {
27+
tmp = append(tmp, string(v))
28+
}
29+
paths = append(paths, tmp)
30+
return
31+
}
32+
33+
for i := 0; i < n; i++ {
34+
if IsValid(path, row, i, n) {
35+
path[row][i] = 'Q'
36+
fn(i, row+1)
37+
path[row][i] = '.'
38+
}
39+
}
40+
}
41+
fn(0, 0)
42+
43+
return paths
44+
}
45+
46+
func IsValid(chessboard [][]byte, row, col int, n int) bool {
47+
// 列
48+
for i := 0; i < n; i++ {
49+
if chessboard[i][col] == 'Q' {
50+
return false
51+
}
52+
}
53+
// 左斜线
54+
i := row - 1
55+
j := col - 1
56+
for i >= 0 && j >= 0 {
57+
fmt.Println(i, j)
58+
if chessboard[i][j] == 'Q' {
59+
return false
60+
}
61+
i--
62+
j--
63+
}
64+
65+
a := row - 1
66+
b := col + 1
67+
68+
for a >= 0 && b < n {
69+
if chessboard[a][b] == 'Q' {
70+
return false
71+
}
72+
a--
73+
b++
74+
}
75+
76+
return true
77+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package _1_N皇后_回溯
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestIsValid(t *testing.T) {
9+
type args struct {
10+
chessboard [][]byte
11+
row int
12+
col int
13+
n int
14+
}
15+
tests := []struct {
16+
name string
17+
args args
18+
want bool
19+
}{
20+
{name: "", args: args{
21+
chessboard: [][]byte{
22+
{'.', '.', '.', '.'},
23+
{'.', 'Q', '.', '.'},
24+
{'Q', '.', '.', '.'},
25+
{'Q', '.', '.', '.'},
26+
},
27+
row: 2,
28+
col: 2,
29+
n: 4,
30+
}, want: false},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
if got := IsValid(tt.args.chessboard, tt.args.row, tt.args.col, tt.args.n); got != tt.want {
35+
t.Errorf("IsValid() = %v, want %v", got, tt.want)
36+
}
37+
})
38+
}
39+
}
40+
41+
func Test_solveNQueens(t *testing.T) {
42+
type args struct {
43+
n int
44+
}
45+
tests := []struct {
46+
name string
47+
args args
48+
want [][]string
49+
}{
50+
{name: `输入:n = 4
51+
输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]`, args: args{n: 4}, want: [][]string{{".Q..", "...Q", "Q...", "..Q."}, {"..Q.", "Q...", "...Q", ".Q.."}}},
52+
}
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
if got := solveNQueens(tt.args.n); !reflect.DeepEqual(got, tt.want) {
56+
t.Errorf("solveNQueens() = %v, want %v", got, tt.want)
57+
}
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)