Posted Golang鎶€鏈垎浜?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鏈変竴澶╋紝濠峰┓鍘昏秴瓒呯殑澶у锛屾兂缁欏紓鍦板凡涔呯殑瓒呰秴涓€涓儕鍠溿€備絾鏄柟鍚戞劅涓嶅ソ鐨勫┓濠峰緢蹇氨鍦ㄥぇ瀛﹁糠璺簡锛岃秴瓒呭緱鐭ュ悗渚垮幓瀵绘壘鎬濇亱宸蹭箙鐨勫┓濠枫€傛鏃惰秴瓒呯殑濡堝涔熷湪鏉ュ鏍$殑璺笂锛岀幇鍦ㄨ秴瓒呰浠ユ渶蹇殑閫熷害鍘诲鎵惧┓濠凤紙涓嶇劧鍙兘浼氬嚭鐜版垜鍜屼綘濡堝悓鏃惰糠璺綘鍏堟晳璋佺殑闂浜嗮煂濓級銆傞偅涔堬紝闂鏉ヤ簡锛屼綘鍙互鍒跺畾涓€绉嶇瓥鐣ュ府瓒呰秴灏藉揩鎵惧埌濠峰┓鍚楋紵
鎹鐪嬪畬杩欑瘒鏂囩珷鐨勪汉閮借兘甯姪瓒呰秴瑙e喅闂鐨勫悓鏃讹紝鍐嶉『鎵嬫嬁涓猳ffer锛?/span>
涓嬮潰灏嗕緷娆′粙缁嶅箍搴︿紭鍏堟悳绱紙鏈枃绠€绉颁负骞挎悳锛夌殑瀹氫箟锛岀畻娉曟ā鏉匡紝淇╃甯歌鐨勯鍨嬶紝浠ュ強甯歌鐨勫嚭棰樼偣浜庢槗閿欑偣銆?/span>
涓€銆佷粈涔堟槸骞挎悳
骞挎悳瀹氫箟
鍙堣瘧浣滃搴︿紭鍏堟悳绱紝鎴栨í鍚戜紭鍏堟悳绱紝鏄竴绉嶅浘褰㈡悳绱㈡柟娉曘€傜畝鍗曠殑璇达紝骞垮害浼樺厛鎼滅储锛圔readth-First-Search锛孊FS锛夋槸浠庢牴鑺傜偣寮€濮嬶紝娌跨潃鏍戠殑瀹藉害閬嶅巻鏍戠殑鑺傜偣銆傚鏋滄墍鏈夎妭鐐瑰潎琚闂紝鍒欑畻娉曠粓姝€?/span>
骞挎悳姝ラ
浠ラ亶鍘嗕笅闈㈣繖妫垫爲涓轰緥
瀹氫竴涓嚭鍙戠偣锛氬亣璁炬垜浠粠v1鍑哄彂鏉ラ亶鍘嗚繖妫垫爲锛屽厛灏唙1瀛樺埌闃熷垪que涓?/span>
鎸夊眰閬嶅巻锛氫粠闃熷垪涓彇鍑簐1锛岀劧鍚庡皢v1鑳芥悳绱㈠埌鐨剉2锛寁3瀛樺埌闃熷垪que涓?/span>
浠g爜濡備笅
1//鏍戠殑鑺傜偣
2type TreeNode struct {
3 Val int
4 Left *TreeNode
5 Right *TreeNode
6}
7
8func bfs(root *TreeNode) {
9 //鍒濆鍖栭槦鍒?/span>
10 que := make([]*TreeNode, 0, 100)
11 que = append(que, root)
12
13 //寰幆闃熷垪鎵€鏈夊厓绱?/span>
14 for len(que) != 0 {
15 curSize := len(que)
16
17 //寰幆閬嶅巻姣忓眰鑺傜偣
18 for i := 0; i < curSize; i++ {
19 //鍙栧嚭闃熷垪涓妭鐐?/span>
20 tnode := que[0]
21 que = que[1:]
22
23 //姹傚嚭璇ヨ妭鐐规墍鑳藉埌杈剧殑鑺傜偣锛岃繘琛屽叆闃熷垪鎿嶄綔
24 if tnode.Left != nil {
25 que = append(que, tnode.Left)
26 }
27 if tnode.Right != nil {
28 que = append(que, tnode.Right)
29 }
30 }
31 }
32}
鎶借薄涓烘ā鏉?/span>
1 func bfs(root *TreeNode) int {
2 //鍒濆鍖栭槦鍒?/span>
3 que := make([]*TreeNode, 0, 100)
4 que = append(que, root)
5 dep := 1
6
7 //寰幆闃熷垪鎵€鏈夎妭鐐?/span>
8 for len(que) != 0 {
9 curSize := len(que)
10
11 //寰幆閬嶅巻姣忓眰鑺傜偣
12 for i := 0; i < curSize; i++ {
13 //鍙栧嚭闃熷垪棣栬妭鐐?/span>
14 tnode := que[0]
15 que = que[1:]
16
17 for 褰撳墠鑺傜偣鎵€鏈夋柟鍚憑
18 //瀵圭Щ鍔ㄥ悗鐨勮妭鐐瑰仛鍧愭爣鍊煎彉鍖?/span>
19
20 //鏃犳硶鍒拌揪鐐癸紝鏄惁涓鸿竟鐣岋紝闅滅鐗?/span>
21 ...
22 //缁堢偣鍒ゆ柇锛岄鐩墍姹傜殑鐐?/span>
23 ...
24 //鍏ラ槦鍒?/span>
25 ...
26 }
27 }
28 //娣卞害鍙樺寲锛堝眰鏁版垨姝ユ暟锛?/span>
29 dep++
30 }
31 return 0
32 }
鏄撻敊鐐?/strong>
閬嶅巻姣忓眰鑺傜偣鏃讹紝鍏堟眰鍑哄綋鍓嶉槦鍒楅暱搴urSize鑰屼笉鏄敤len(que)浣滀负璇ュ眰闃熷垪鐨勯暱搴︼紝濡傛灉鐢╨en(que)鍚庨潰鍏ラ槦鍒楁搷浣滐紝浼氫娇len(que)涓€鐩村湪鍙樺寲
杈圭晫鍊笺€佺粨鏋滃€煎垽鏂紱鍏ラ槦鍒楃殑鍒ゆ柇椤哄簭锛氬叆闃熷垪涓€瀹氭槸鍦ㄦ渶鍚庯紝濡傛灉鏀惧湪鍓嶉潰浼氬皢鏃犳硶鍒拌揪鐐规斁鍏ラ槦鍒?/span>
甯﹀蹇樺綍鐨勫箍鎼?/strong>
鍦ㄦ爲涓妭鐐逛箣闂存槸鏈夋柟鍚戠殑锛屾垜浠彧鑳戒粠鐖惰妭鐐归亶鍘嗗埌瀛愯妭鐐癸紝鑰屾棤娉曚粠瀛愯妭鐐归亶鍘嗗埌鐖惰妭鐐癸紝浣嗘槸鍦ㄥ浘鐨勯亶鍘嗕腑寰€寰€瀛愯妭鐐逛篃鑳介亶鍘嗗埌鐖惰妭鐐广€備緥濡備笅鍥句腑锛屾瘡涓妭鐐瑰彲浠ユ湁涓婁笅宸﹀彸锛屼互鍙婂乏涓婏紝宸︿笅锛屽彸涓婏紝鍙充笅鍏釜鏂瑰悜鍙互绉诲姩锛屼粠A1鍑哄彂鍙互閬嶅巻鍒癆2锛孊1锛孊2鐐癸紝褰揂2鍑洪槦鍒楁椂锛屼粬鍚屾牱鍙互閬嶅巻鍒癆1锛岃繖鏍稟1灏变細琚噸澶嶉亶鍘嗭紝鍥犳闇€瑕佸畾涔変竴涓獀isited鍒囩墖鏍囪褰撳墠鍝簺鑺傜偣琚亶鍘嗚繃銆?/span>
浜屻€佸父瑙侀鍨?/strong>
鍥句腑鏈€鐭矾寰?/span>
涓嬮潰鏉ョ湅鐪嬪箍鎼滄€庝箞瑙e喅瓒呰秴鐨勯棶棰橈紝鐢?琛ㄧず绌哄湴锛?琛ㄧず闅滅鐗╋紝鍋囪瓒呰秴鍦ㄥ湴鍥惧乏涓婅锛屽┓濠峰湪鍦板浘鐨勫彸涓嬭锛岃秴瓒呮瘡涓€姝ュ彲浠ユ部鐫€鍏釜鏂瑰悜绉诲姩銆傞偅涔堝湴鍥惧彲浠ユ娊璞′负涓嬪浘鎵€绀猴紝瓒呰秴甯屾湜鐨勫氨鏄敤鏈€灏戠殑绉诲姩姝ユ暟浠庡乏涓婅绉诲姩鍒板彸涓嬭銆?/span>
鍒嗘瀽锛氳秴瓒呯殑闂鏄爣鍑嗙殑鍥句腑姹備咯鐐归棿鏈€鐭矾寰勯棶棰橈紝鏄剧劧鍙互鍒╃敤骞挎悳浠庡乏涓婅鍑哄彂锛屼竴灞傚眰鐨勯亶鍘嗕笅鍘荤洿鍒伴亶鍘嗗埌鍙充笅瑙掞紝涓轰簡蹇€熷府瓒呰秴瑙e喅闂锛屼笅闈㈢洿鎺ヤ笂妯℃澘
1func bfs(root *TreeNode) int {
2 //鍒濆鍖栭槦鍒?/span>
3 que := make([]*TreeNode, 0, 100)
4 que = append(que, root)
5 dep := 1
6
7 //寰幆闃熷垪鎵€鏈夎妭鐐?/span>
8 for len(que) != 0 {
9 curSize := len(que)
10
11 //寰幆閬嶅巻姣忓眰鑺傜偣
12 for i := 0; i < curSize; i++ {
13
14 //鍙栧嚭闃熷垪鑺傜偣
15 tnode := que[0]
16 que = que[1:]
17
18 for 褰撳墠鑺傜偣鎵€鏈夋柟鍚憑
19 //瀵圭Щ鍔ㄥ悗鐨勮妭鐐瑰仛鍧愭爣鍊煎彉鍖?/span>
20
21 //鐗规畩鍊硷紝鏄惁涓鸿竟鐣岋紝闅滅鐗?/span>
22 ...
23 //缁撴灉鍊煎垽鏂紝鏄惁鍒拌揪棰樼洰瑕佹眰鐨勭洰鏍囩偣
24 ...
25 //鍏ラ槦鍒?/span>
26 ...
27 }
28 }
29 //娣卞害鍙樺寲锛堟鏁帮級
30 dep++
31 }
32 return 0
33}
鏄惁闇€瑕乿isited鏍囪瘑锛氬湪鎼滅储鏃舵瘡涓妭鐐归兘浼氬悜鍏釜鏂瑰悜鎼滅储锛孉鍚戝彸绉诲姩鍒癇鑺傜偣鍏ラ槦鍒楀悗锛屽悜鍚庨亶鍘嗕笅涓€灞傛椂B杩樹細鍚戝乏鎼滅储鍒癆锛屽洜姝ら渶瑕佺敤visited杩涜鏍囪瘑璇ヨ妭鐐规槸鍚﹁閬嶅巻杩?/span>
纭畾绉诲姩鏂瑰悜锛氳秴瓒呬竴鍏辨湁鍏釜绉诲姩鏂瑰悜锛屽彲浠ョ敤鏁扮粍next := [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}琛ㄧず绉诲姩鏂瑰悜锛屼咯涓淮搴﹀垎鍒〃绀哄潗鏍噚鍜寉鐨勫彉鍖?/span>
閬嶅巻鐨勭壒娈婂€煎垽鏂細杩欓鏈夎竟鐣屽€煎锛岄殰纰嶇墿锛屼互鍙婄粓鐐?/span>
瀹屾暣浠g爜濡備笅
1func shortestPathBinaryMatrix(grid [][]int) int {
2 width := len(grid)
3 //鑻ヨ捣鐐瑰拰缁堢偣鏄殰纰嶇墿鍒欏湴鍥炬湁闂
4 if grid[0][0] == 1 || grid[width-1][width-1] == 1 {
5 return -1
6 }
7 //鍦板浘涓捣鐐瑰拰缁堢偣鏄竴涓偣
8 if width == 1 {
9 return 1
10 }
11
12 //鍒濆鍖栭槦鍒楀拰visited
13 que := make([]*Node, 0, width*width)
14 visited := make([][]bool, width)
15 for i := 0; i < len(visited); i++ {
16 visited[i] = make([]bool, width)
17 }
18 dep := 1
19 //姣忎釜鑺傜偣鎵€鑳借蛋鐨勫叓涓柟鍚?/span>
20 next := [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
21 que = append(que, &Node{0, 0})
22 visited[0][0] = true
23
24 //寮€濮嬫悳绱?/span>
25 for len(que) != 0 {
26 current := len(que)
27 //鎸夊眰閬嶅巻
28 for i := 0; i < current; i++ {
29 top := que[0]
30 que = que[1:]
31 //瀵归槦鍒椾腑鐨勮妭鐐瑰仛鍏釜鏂瑰悜鐨勫潗鏍囧彉鍖?/span>
32 for i := 0; i < 8; i++ {
33 var temp *Node = new(Node)
34 temp.x = top.x + next[i][0]
35 temp.y = top.y + next[i][1]
36 //绉诲姩鍚庡潗鏍囪秺鐣?/span>
37 if temp.x >= width || temp.x < 0 || temp.y >= width || temp.y < 0 {
38 continue
39 }
40 //鎵惧埌缁堢偣锛堝彸涓嬭锛?/span>
41 if temp.x == width-1 && temp.y == width-1 {
42 return dep + 1
43 }
44 //涓嶆槸闅滅鐗╁苟涓旀病鏈夎閬嶅巻杩囧叆闃熷垪
45 if grid[temp.x][temp.y] != 1 && !visited[temp.x][temp.y] {
46 que = append(que, temp)
47 visited[temp.x][temp.y] = true
48 }
49 }
50 }
51 //灞傛暟鍔?
52 dep++
53 }
54 return -1
55}
瀛楃涓叉渶鐭彉鍖栨鏁?/span>
甯姪瀹岃秴瓒呬箣鍚庯紝涔熻涓烘垜浠殑offer濂藉ソ鍑嗗涓嬩簡锛岄潰璇曠畻娉曞緢灏戜細鐩存帴鍑轰竴涓浘鐨刡fs棰樺瀷锛屾瘮杈冨父瑙佺殑鏄繖绉嶉鍨嬨€傝繖棰樼湅浼煎拰bfs娌℃湁鍏崇郴锛屼絾鏄垜浠◢浣滄娊璞″氨鍙互鑱旂郴鍦ㄤ竴璧蜂簡锛岃捣鐐筯it鑳藉尮閰嶇殑瀛楃涓蹭负*it锛宧 * t锛宧i*锛?span class="mq-440">涓巜orldList涓彲浠ュ尮閰嶇殑鑺傜偣灏辨槸浠巋it鍑哄彂鑳藉埌杈剧殑鑺傜偣浜?/strong>锛岃繖鏍蜂緷娆¤繛鎺ヤ笅鍘绘槸涓嶆槸灏卞彲浠ヨ繛鎴愬浘浜嗭紒杩欓涔熷氨鍙樻垚浜嗘眰hit鍒癱og鐨勬渶鐭窛绂汇€?/span>
鍒嗘瀽锛?/span>
鏄惁闇€瑕乿isited鏍囪瘑锛氳繖棰樼殑visited鏈変咯涓惈涔夛紝涓€涓槸鍒ゆ柇褰撳墠map涓槸鍚︽湁鍖归厤鐨剋ord锛屼簩鏄仛澶囧繕褰曪紝褰撲粠hit鍑哄彂灏唄ot鍔犲叆闃熷垪鍚庯紝闃叉hot閬嶅巻鍏跺彲鍒拌揪鐨勭偣鏃跺張灏唄it鍔犲叆闃熷垪涓?/span>
纭畾绉诲姩鏂瑰悜锛氫互hit涓轰緥锛屽彧瑕佹弧瓒?it锛宧 * t锛宧i*锛岄兘涓哄彲绉诲姩鏂瑰悜锛屼篃灏辨槸寰幆hit涓瘡涓€涓瓧绗︼紝浠?a'鍒?z'閬嶅巻锛屾牴鎹畍isited鍒ゆ柇瀛楀吀涓槸鍚︽湁璇ュ瓧绗︿覆
閬嶅巻鐨勭壒娈婂€煎垽鏂細缁堢偣锛屾槸鍚﹁閬嶅巻杩?/span>
浠g爜
1func ladderLength(beginWord string, endWord string, wordList []string) int {
2 //瀹氫箟骞跺垵濮嬪寲visited
3 visited := map[string]bool{}
4 for _, w := range wordList {
5 visited[w] = false
6 }
7 que := []string{beginWord}
8 dep := 1
9 //閬嶅巻鎵€鏈夊厓绱?/span>
10 for len(que) != 0 {
11 curSize := len(que)
12 for i := 0; i < curSize; i++ {
13 top := que[0]
14 que = que[1:]
15 if top == endWord {
16 return dep
17 }
18 //鍒ゆ柇鑳借繛鎺ョ殑瀛楃涓?/span>
19 for c := 0; c < len(top); c++ {
20 for j := 'a'; j <= 'z'; j++ {
21 newWord := top[:c] + string(j) + top[c+1:]
22 if visited[newWord] == false {
23 que = append(que, newWord)
24 visited[newWord] = true
25 }
26 }
27 }
28 }
29 dep++
30 }
31 return 0
32}
涓夈€佹€荤粨
鍑洪鐐?/span>锛?/span>BFS鐨勬櫘閫傞鍨嬩竴鍏辨槸涓夌锛氭爲鐨勫眰娆¢亶鍘嗭紝鍥剧殑鏈€鐭矾寰勶紝瀛楃涓插尮閰嶆渶灏戞鏁般€傛壘鍑嗗叧閿瓧鈥滄渶鐭矾寰勨€濓紝鈥滃眰娆¢亶鍘嗏€濄€?/span>
妯℃澘璁板繂锛?/span>姘寸汗娉㈠姩鏈夎寰嬶紝灞傛閬嶅巻灞傚眰娓咃紙cursize锛寁isited锛夛紝鎵惧噯鐗规畩浣嶇疆鐐癸紙澧欙紝缁堢偣锛屾槸鍚﹂亶鍘嗚繃锛夛紝鍒ゆ柇鏉′欢鏈夊厛鍚庯紙鐗规畩鐐瑰垽鏂厛鍚庨『搴忥級銆?/span>
鎬濊矾锛?/span>鎷垮埌棰樼洰锛屾垜浠鍏堝彲浠ユ崟鎹夊叧閿瘝锛屼竴鏃︾湅鍒版湁鈥滄渶鐭矾寰勨€濄€佲€滃眰娆¢亶鍘嗏€濊繖绉嶈瘝璇紝閭d箞鎴戜滑搴旇鎯冲埌BFS銆備絾鏄湁鏃跺€欓鐩細闅愯棌杩欑鏆撮湶璇彞锛屼緥濡傚崟璇嶆帴榫欎竴鏍凤紝杩欐椂鎴戜滑闇€瑕佹娊璞″畠鐨勬湰璐紝鑳藉惁灏嗘瘡涓€涓瓧绗︿覆鐪嬩綔涓€涓妭鐐硅繘琛岃繛鎺ャ€傞渶瑕佹敞鎰忕殑鏄紝鍦ㄩ亶鍘嗘椂鎴栬浼氭湁閲嶅璺緞鐨勫彲鑳斤紝杩欐椂涓嶈蹇樹簡璁緑isted鐨勫蹇樺綍銆傚悓鏃讹紝鎴戜滑闇€瑕佹敞鎰忛殰纰嶇墿锛岃竟鐣屽€硷紝缁堢偣浣嶇疆鐨勫厛鍚庡垽鏂€?/span>
鏄撻敊鐐?/span>锛?/span>寰幆闃熷垪涓妭鐐圭敤curSize璁板綍褰撳墠闃熷垪闀垮害锛岃€屼笉鏄敤len(que)銆?/span>
鎰熻阿浣犵殑鐐硅禐鍜?span>鍦ㄧ湅鍝
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章