国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

這篇具有很好參考價值的文章主要介紹了Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

目錄

221. 最大正方形 Maximal Square??????

222. 完全二叉樹的節(jié)點個數(shù) Count Complete Tree Nodes??????

?? 每日一練刷題專欄???

Rust每日一練 專欄

Golang每日一練 專欄

Python每日一練 專欄

C/C++每日一練 專欄

Java每日一練 專欄


221. 最大正方形 Maximal Square

在一個由?'0'?和?'1'?組成的二維矩陣內(nèi),找到只包含?'1'?的最大正方形,并返回其面積。

示例 1:

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

輸入:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
輸出:4

示例 2:

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

輸入:matrix = [["0","1"],["1","0"]]
輸出:1

示例 3:

輸入:matrix = [["0"]]
輸出:0

提示:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 300
  • matrix[i][j]?為?'0'?或?'1'

代碼1: 暴力枚舉

package main

import (
	"fmt"
)

func maximalSquare(matrix [][]byte) int {
	m, n := len(matrix), len(matrix[0])
	maxLen := 0
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if matrix[i][j] == '1' {
				curLen := 1
				flag := true
				for k := 1; k+i < m && k+j < n && flag; k++ {
					for l := 0; l <= k; l++ {
						if matrix[i+k][j+l] == '0' || matrix[i+l][j+k] == '0' {
							flag = false
							break
						}
					}
					if flag {
						curLen++
					}
				}
				if curLen > maxLen {
					maxLen = curLen
				}
			}
		}
	}
	return maxLen * maxLen
}

func main() {
	matrix := [][]byte{{'1', '0', '1', '0', '0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}}
	fmt.Println(maximalSquare(matrix))
	matrix = [][]byte{{'0', '1'}, {'1', '0'}}
	fmt.Println(maximalSquare(matrix))
	matrix = [][]byte{{'0'}}
	fmt.Println(maximalSquare(matrix))
}

代碼2:?動態(tài)規(guī)劃

package main

import (
	"fmt"
)

func maximalSquare(matrix [][]byte) int {
	m, n := len(matrix), len(matrix[0])
	maxLen := 0
	dp := make([][]int, m)
	for i := 0; i < m; i++ {
		dp[i] = make([]int, n)
		for j := 0; j < n; j++ {
			if matrix[i][j] == '1' {
				if i == 0 || j == 0 {
					dp[i][j] = 1
				} else {
					dp[i][j] = min(dp[i-1][j], min(dp[i][j-1], dp[i-1][j-1])) + 1
				}
				if dp[i][j] > maxLen {
					maxLen = dp[i][j]
				}
			}
		}
	}
	return maxLen * maxLen
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	matrix := [][]byte{{'1', '0', '1', '0', '0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}}
	fmt.Println(maximalSquare(matrix))
	matrix = [][]byte{{'0', '1'}, {'1', '0'}}
	fmt.Println(maximalSquare(matrix))
	matrix = [][]byte{{'0'}}
	fmt.Println(maximalSquare(matrix))
}

代碼3:?動態(tài)規(guī)劃優(yōu)化

package main

import (
	"fmt"
)

func maximalSquare(matrix [][]byte) int {
	m, n := len(matrix), len(matrix[0])
	maxLen := 0
	cur := make([]int, n)
	pre := make([]int, n)
	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if matrix[i][j] == '1' {
				if i == 0 || j == 0 {
					cur[j] = 1
				} else {
					cur[j] = min(pre[j], min(cur[j-1], pre[j-1])) + 1
				}
				if cur[j] > maxLen {
					maxLen = cur[j]
				}
			} else {
				cur[j] = 0
			}
		}
		cur, pre = pre, cur
	}
	return maxLen * maxLen
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	matrix := [][]byte{{'1', '0', '1', '0', '0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'}, {'1', '0', '0', '1', '0'}}
	fmt.Println(maximalSquare(matrix))
	matrix = [][]byte{{'0', '1'}, {'1', '0'}}
	fmt.Println(maximalSquare(matrix))
	matrix = [][]byte{{'0'}}
	fmt.Println(maximalSquare(matrix))
}

輸出:

4
1
0


222. 完全二叉樹的節(jié)點個數(shù) Count Complete Tree Nodes

給你一棵?完全二叉樹?的根節(jié)點?root?,求出該樹的節(jié)點個數(shù)。

完全二叉樹的定義如下:在完全二叉樹中,除了最底層節(jié)點可能沒填滿外,其余每層節(jié)點數(shù)都達(dá)到最大值,并且最下面一層的節(jié)點都集中在該層最左邊的若干位置。若最底層為第?h?層,則該層包含?1~2^h?個節(jié)點。

示例 1:

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

輸入:root = [1,2,3,4,5,6]
輸出:6

示例 2:

輸入:root = []
輸出:0

示例 3:

輸入:root = [1]
輸出:1

提示:

  • 樹中節(jié)點的數(shù)目范圍是[0, 5 * 10^4]
  • 0 <= Node.val <= 5 * 10^4
  • 題目數(shù)據(jù)保證輸入的樹是?完全二叉樹?

進(jìn)階:遍歷樹來統(tǒng)計節(jié)點是一種時間復(fù)雜度為?O(n)?的簡單解決方案。你可以設(shè)計一個更快的算法嗎?

代碼1: dfs

完全二叉樹,可以通過比較根節(jié)點左子樹高度和右子樹高度來確定它是否可以被視為完全二叉樹。如果左右高度相等,則該樹是滿二叉樹,節(jié)點個數(shù)為?2??12h?1;否則,該樹可以被分成一棵滿二叉樹和一顆子樹,遞歸計算子樹的節(jié)點數(shù)?

package main

import "fmt"

const null = -1 << 31

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func countNodes(root *TreeNode) int {
	if root == nil {
		return 0
	}
	var left, right uint
	left, right = 0, 0
	for node := root; node != nil; node = node.Left {
		left++
	}
	for node := root; node != nil; node = node.Right {
		right++
	}
	if left == right {
		return (1 << left) - 1
	} else {
		return countNodes(root.Left) + countNodes(root.Right) + 1
	}
}

func buildTree(nums []int) *TreeNode {
	if len(nums) == 0 {
		return nil
	}
	root := &TreeNode{Val: nums[0]}
	Queue := []*TreeNode{root}
	idx := 1
	for idx < len(nums) {
		node := Queue[0]
		Queue = Queue[1:]
		if nums[idx] != null {
			node.Left = &TreeNode{Val: nums[idx]}
			Queue = append(Queue, node.Left)
		}
		idx++
		if idx < len(nums) && nums[idx] != null {
			node.Right = &TreeNode{Val: nums[idx]}
			Queue = append(Queue, node.Right)
		}
		idx++
	}
	return root
}

func main() {
	nums := []int{1, 2, 3, 4, 5, 6}
	root := buildTree(nums)
	fmt.Println(countNodes(root))
	nums = []int{}
	root = buildTree(nums)
	fmt.Println(countNodes(root))
	nums = []int{1}
	root = buildTree(nums)
	fmt.Println(countNodes(root))
}

注:位運算符?<<?要求右側(cè)的操作數(shù)必須為無符號整數(shù)類型。在 Go 語言中,如果左側(cè)操作數(shù)不是無符號整數(shù),右側(cè)操作數(shù)必須用無符號整數(shù)類型轉(zhuǎn)換。?

代碼2:bfs

將根節(jié)點放入隊列中,然后對于隊列中的每一個節(jié)點,將它的左右子節(jié)點入隊,統(tǒng)計隊列的大小即為節(jié)點個數(shù)

package main

import "fmt"

const null = -1 << 31

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func countNodes(root *TreeNode) int {
	if root == nil {
		return 0
	}
	q := []*TreeNode{root}
	count := 0
	for len(q) > 0 {
		size := len(q)
		count += size
		for i := 0; i < size; i++ {
			node := q[i]
			if node.Left != nil {
				q = append(q, node.Left)
			}
			if node.Right != nil {
				q = append(q, node.Right)
			}
		}
		q = q[size:]
	}
	return count
}

func buildTree(nums []int) *TreeNode {
	if len(nums) == 0 {
		return nil
	}
	root := &TreeNode{Val: nums[0]}
	Queue := []*TreeNode{root}
	idx := 1
	for idx < len(nums) {
		node := Queue[0]
		Queue = Queue[1:]
		if nums[idx] != null {
			node.Left = &TreeNode{Val: nums[idx]}
			Queue = append(Queue, node.Left)
		}
		idx++
		if idx < len(nums) && nums[idx] != null {
			node.Right = &TreeNode{Val: nums[idx]}
			Queue = append(Queue, node.Right)
		}
		idx++
	}
	return root
}

func main() {
	nums := []int{1, 2, 3, 4, 5, 6}
	root := buildTree(nums)
	fmt.Println(countNodes(root))
	nums = []int{}
	root = buildTree(nums)
	fmt.Println(countNodes(root))
	nums = []int{1}
	root = buildTree(nums)
	fmt.Println(countNodes(root))
}

代碼3:二分法

package main

import "fmt"

const null = -1 << 31

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func countNodes(root *TreeNode) int {
	if root == nil {
		return 0
	}
	left, right := countHeight(root.Left), countHeight(root.Right)
	if left == right {
		return (1 << left) + countNodes(root.Right)
	} else {
		return (1 << right) + countNodes(root.Left)
	}
}

func countHeight(root *TreeNode) uint {
	if root == nil {
		return 0
	}
	return countHeight(root.Left) + 1
}

func buildTree(nums []int) *TreeNode {
	if len(nums) == 0 {
		return nil
	}
	root := &TreeNode{Val: nums[0]}
	Queue := []*TreeNode{root}
	idx := 1
	for idx < len(nums) {
		node := Queue[0]
		Queue = Queue[1:]
		if nums[idx] != null {
			node.Left = &TreeNode{Val: nums[idx]}
			Queue = append(Queue, node.Left)
		}
		idx++
		if idx < len(nums) && nums[idx] != null {
			node.Right = &TreeNode{Val: nums[idx]}
			Queue = append(Queue, node.Right)
		}
		idx++
	}
	return root
}

func main() {
	nums := []int{1, 2, 3, 4, 5, 6}
	root := buildTree(nums)
	fmt.Println(countNodes(root))
	nums = []int{}
	root = buildTree(nums)
	fmt.Println(countNodes(root))
	nums = []int{1}
	root = buildTree(nums)
	fmt.Println(countNodes(root))
}

輸出:

6
0
1


?? 每日一練刷題專欄???

? 持續(xù),努力奮斗做強刷題搬運工!

?? 點贊,你的認(rèn)可是我堅持的動力!?

???收藏,你的青睞是我努力的方向!?

? 評論,你的意見是我進(jìn)步的財富!??

??主頁:https://hannyang.blog.csdn.net/?

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

Rust每日一練 專欄

(2023.5.16~)更新中...

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

Golang每日一練 專欄

(2023.3.11~)更新中...

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

Python每日一練 專欄

(2023.2.18~2023.5.18)暫停更

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

C/C++每日一練 專欄

(2023.2.18~2023.5.18)暫停更

Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)

Java每日一練 專欄

(2023.3.11~2023.5.18)暫停更文章來源地址http://www.zghlxwxcb.cn/news/detail-473424.html

到了這里,關(guān)于Golang每日一練(leetDay0079) 最大正方形、完全二叉樹節(jié)點數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 力扣221.最大正方形(動態(tài)規(guī)劃)

    力扣221.最大正方形(動態(tài)規(guī)劃)

    思路: 思路:從[0,0]元素開始,計算每個元素對應(yīng)其與[0,0]之間矩陣塊中最大正方形邊長 情況:1)matrix [ i , j ] = ‘0’ -- 元素對應(yīng)的最大正方形為0。 情況:2)matrix [ i , j ] = ‘1’ -- max ( matrix [ i-1 , j ] , matrix [ i - 1 , j - 1 ) ,matrix [ i , j - 1 ] ) + 1 具體實現(xiàn):1)先找出第一行和第

    2024年02月13日
    瀏覽(22)
  • 【LeetCode-中等】221. 最大正方形(詳解)

    【LeetCode-中等】221. 最大正方形(詳解)

    在一個由? \\\'0\\\' ?和? \\\'1\\\' ?組成的二維矩陣內(nèi),找到只包含? \\\'1\\\' ?的最大正方形,并返回其面積。 力扣原題鏈接 ? 暴力法一般不是最優(yōu)解,但是可以拿來練手 由于正方形的面積等于邊長的平方,因此要找到最大正方形的面積,首先需要找到最大正方形的邊長,然后計算最大邊

    2024年02月13日
    瀏覽(56)
  • 洛谷 P1387 最大正方形 題解

    通過二維前綴和判定矩形內(nèi)是否全為1,計算和等于長度的平方就判斷為是 復(fù)雜度 (Theta (n^2log{n})) 設(shè)狀態(tài) (f_{i,j}) 為以第 (i) 行 (j) 列為右下角的最大正方形的邊長, (a_{i,j}) 表示輸入矩陣中的數(shù)值,有轉(zhuǎn)移方程: [f_{i,j} = (min(f_{i-1,j},f_{i,j-1},f_{i-1,j-1}) + 1) * a_{i,j}] 解釋:

    2024年02月16日
    瀏覽(22)
  • 最大正方形(力扣)暴力 + 動態(tài)規(guī)劃 JAVA

    最大正方形(力扣)暴力 + 動態(tài)規(guī)劃 JAVA

    在一個由 ‘0’ 和 ‘1’ 組成的二維矩陣內(nèi),找到只包含 ‘1’ 的最大正方形,并返回其面積。 示例 1: 輸入:matrix = [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”,“1”,“1”,“1”],[“1”,“0”,“0”,“1”,“0”]] 輸出:4 示例 2: 輸入:m

    2024年02月15日
    瀏覽(22)
  • LeetCode221.Maximal-Square<最大正方形>

    LeetCode221.Maximal-Square<最大正方形>

    ? ? 這題是動態(tài)規(guī)劃,但是不會寫。想著判斷dp的 上,左,左上? 去了。發(fā)現(xiàn)只能這樣最大只能判斷面積為4的正方形因為只會判斷另外三個方格。而要想判斷更大的正方形那就需要遞歸操作了。那肯定會超時了。 好吧,只能看答案了。 正方形的面積的長乘寬。在例子中我們

    2024年02月15日
    瀏覽(22)
  • 【LeetCode熱題100】打卡第39天:數(shù)組中第K個最大元素&最大正方形

    【LeetCode熱題100】打卡第39天:數(shù)組中第K個最大元素&最大正方形

    大家好,我是知識汲取者,歡迎來到我的LeetCode熱題100刷題專欄! 精選 100 道力扣(LeetCode)上最熱門的題目,適合初識算法與數(shù)據(jù)結(jié)構(gòu)的新手和想要在短時間內(nèi)高效提升的人,熟練掌握這 100 道題,你就已經(jīng)具備了在代碼世界通行的基本能力。在此專欄中,我們將會涵蓋各種

    2024年02月16日
    瀏覽(20)
  • Python-彩色正方形

    Python-彩色正方形

    核心代碼 核心代碼 核心代碼

    2024年02月19日
    瀏覽(19)
  • CSS 3D旋轉(zhuǎn)正方形

    CSS 3D旋轉(zhuǎn)正方形

    2024年01月23日
    瀏覽(23)
  • python繪制螺旋正方形

    python繪制螺旋正方形

    1.首先導(dǎo)入畫圖功能庫turtle 2.設(shè)置畫筆大小,顏色? turtle.pensize()? turtle.pencolor() 3.如何繪制:螺旋正方形一開始外邊有3條長度相同的邊,先勾勒出一條邊循環(huán)3次將外邊構(gòu)出,里邊每勾勒一條循環(huán)兩次,再進(jìn)行循環(huán),即需要一個雙循環(huán),邊每次比前一次縮短。 畫筆前進(jìn)? turt

    2024年04月15日
    瀏覽(51)
  • 將正方形矩陣順時針轉(zhuǎn)動 90°

    【題目】 給定一個 N×N 的矩陣 matrix,把這個矩陣調(diào)整成順時針轉(zhuǎn)動 90°后的形式。 例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 順時針轉(zhuǎn)動 90°后為: 13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4 【要求】 額外空間復(fù)雜度為 O(1)。 思路: 這里使用分圈處理的方式,在矩陣中用左上角的坐標(biāo)(tR,tC)和

    2024年02月13日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包