2:算法php/go [二分查找 ;二叉树的层序遍历 ;最长无重复子数组]
生活随笔
收集整理的這篇文章主要介紹了
2:算法php/go [二分查找 ;二叉树的层序遍历 ;最长无重复子数组]
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
22-01-07?二分查找
22-01-10 二叉樹的層序遍歷
22-01-14最長(zhǎng)無重復(fù)子數(shù)組
22-01-07?二分查找
請(qǐng)實(shí)現(xiàn)有重復(fù)數(shù)字的升序數(shù)組的二分查找
給定一個(gè) 元素有序的(升序)長(zhǎng)度為n的整型數(shù)組 nums 和一個(gè)目標(biāo)值 target ?,寫一個(gè)函數(shù)搜索 nums 中的第一個(gè)出現(xiàn)的target,如果目標(biāo)值存在返回下標(biāo),否則返回 -1
?22-01-10 二叉樹的層序遍歷
給定一個(gè)二叉樹,返回該二叉樹層序遍歷的結(jié)果,(從左到右,一層一層地遍歷)
例如:
給定的二叉樹是{3,9,20,#,#,15,7},
該二叉樹層序遍歷的結(jié)果是
[
[3],
[9,20],
[15,7]
]
<?phpfunction levelOrder($root) {$result = [];if (!$root) {return $result;}$stack = [$root];while (count($stack) > 0) {$size = count($stack);$i = 0;$tt = [];while ($i < $size) {$t = array_shift($stack);$tt[] = $t->val;if ($t->left) {array_push($stack, $t->left);}if ($t->right) {array_push($stack, $t->right);}$i++;}$result[] = $tt;}return $result; }class TreeNode{var $val;var $left = NULL;var $right = NULL;function __construct($val){$this->val = $val;} }$rt1 = new TreeNode(1); $rt3 = new TreeNode(3); $rt5 = new TreeNode(5); $rt7 = new TreeNode(7); $rt8 = new TreeNode(8); $rt13 = new TreeNode(13); $rt1->left = $rt3; $rt1->right = $rt5; $rt5->left = $rt7; $rt5->right = $rt8; $rt8->right = $rt13; /*var_dump($rt1); print_r($rt1);*/ $root = levelOrder($rt1); print_r($root); function levelOrder( $root ) {// write code here$queue = new SplQueue();if (!$root) {return [];}$result = [];$queue->push($root);while(!$queue->isEmpty()) {$len = $queue->count();$tmp = [];for($i = 0; $i < $len; $i++) {$current = $queue->shift();$tmp[] = $current->val;if($current->left) $queue->push($current->left);if($current->right) $queue->push($current->right);}$result[] = $tmp;}return $result; } package main import "fmt" type TreeNode struct {Val intLeft *TreeNodeRight *TreeNode} var ret [][]int func levelOrder( root *TreeNode ) [][]int {// write code hereret = [][]int{}dfs(root, 0)return ret }func dfs(root *TreeNode, level int) {if root != nil {if len(ret) == level {ret = append(ret, []int{})}ret[level] = append(ret[level], root.Val)dfs(root.Left, level+1)dfs(root.Right, level+1)} }func main() {root1 := new(TreeNode)root1.Val = 1head2 := new(TreeNode)head2.Val = 2head3 := new(TreeNode)head3.Val = 3head4 := new(TreeNode)head4.Val = 4head5 := new(TreeNode)head5.Val = 5root1.Left = head3root1.Right = head2head2.Left = head5head2.Right = head4fmt.Printf("%+v\n", root1)fmt.Printf("%+v\n", root1.Left)fmt.Printf("%+v\n", root1.Right)rl := levelOrder(root1)//result := fmt.Sprintf("hh%+v" ,*root1)fmt.Println(rl) } func levelOrder( root *TreeNode ) [][]int {// write code hereif nil == root {return [][]int{}}var res [][]intnodes := []*TreeNode{root}for len(nodes)>0{//取出隊(duì)列數(shù)據(jù)level := make([]int,0)currentNodes := nodes[:]nodes = make([]*TreeNode,0)for _,node:=range currentNodes{level = append(level,node.Val )if node.Left!=nil{nodes = append(nodes, node.Left)}if node.Right!=nil{nodes = append(nodes, node.Right)}}res = append(res, level)}return res }? 22-01-14最長(zhǎng)無重復(fù)子數(shù)組
?最長(zhǎng)無重復(fù)子數(shù)組 /*給定一個(gè)長(zhǎng)度為n的數(shù)組arr,返回arr的最長(zhǎng)無重復(fù)元素子數(shù)組的長(zhǎng)度,無重復(fù)指的是所有數(shù)字都不相同。 子數(shù)組是連續(xù)的,比如[1,3,5,7,9]的子數(shù)組有[1,3],[3,5,7]等等,但是[1,3,7]不是子數(shù)組輸入: [2,3,4,5] 返回值: 4 說明: [2,3,4,5]是最長(zhǎng)子數(shù)組 要求:空間復(fù)雜度 O(n)O(n),時(shí)間復(fù)雜度 O(nlogn)O(nlogn)*/ function maxLength1($arr) {$winMap = array();$leftSub = 0;$maxLen = 0;for ($i = 0; $i < count($arr); $i++) {$item = $arr[$i];if (isset($winMap[$item])) {$leftSub = max($leftSub, $winMap[$item] + 1);}$subLen = $i - $leftSub + 1;$maxLen = max($subLen, $maxLen);$winMap[$item] = $i;}return $maxLen; }print_r([maxLength1([1, 3, 4, 5, 5, 7, 8])]);function maxLength($arr) {$maxLength = 0;if (empty($arr)) {return $maxLength;}$zhiZhen = ['left' => 0,'right' => 0,];$map = [];foreach ($arr as $index => $value) {if (isset($map[$value])) {$zhiZhen['left'] = max($zhiZhen['left'], $map[$value] + 1);}$map[$value] = $index;$zhiZhen['right'] = $index;$curLength = $zhiZhen['right'] - $zhiZhen['left'];if ($curLength > $maxLength) {$maxLength = $curLength;}}return $maxLength + 1; }print_r([maxLength([1, 3, 4, 5, 5, 7, 8])]); package mainimport "fmt"func MaxLength1(arr []int) int {res := 0s := make([]int, 100000)j := 0for i := 0 ; i < len(arr); i++ {s[arr[i]]++for j <= i && s[arr[i]] > 1 {s[arr[j]] --j++}if i-j + 1 > res {res = i - j + 1}}return res }func maxLength2(arr []int) int {left ,flag ,res := 0 ,make([]int, 100000) , 0for i:=0 ; i < len(arr) ; i++ {flag[arr[i]]++for left <= i && flag[arr[i]] > 1 {flag[arr[left]] --left ++}if i - left + 1 >res {res = i -left +1}}return res }func main(){var arr1 = []int {1,3,4,5 ,5,6}length1 := MaxLength1(arr1)length2 := maxLength2(arr1)fmt.Println(length1)fmt.Println(length2) }總結(jié)
以上是生活随笔為你收集整理的2:算法php/go [二分查找 ;二叉树的层序遍历 ;最长无重复子数组]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单PS CS6抠图技巧
- 下一篇: 动态规划算法php,php算法学习之动态