当前位置:网站首页>go语言学习笔记四
go语言学习笔记四
2022-07-30 04:45:00 【N. LAWLIET】
go语言结构体的定义
type student struct{
name string
age int
}go语言的结构体是面向对象方法,没有this指针,没有方法复写也没有extends
结构体是值传递,拷贝也是浅拷贝。
结构体可以实现很多种数据结构,可以作为list
比如
type node struct{
val string
next *node
}queue,stack,tree等都可以用结构体实现。
结构体指针声名如下
list := new(node)//生命结构体方法一
(*list).val = "123"
(*list).next = &new(node)
fmt.println((*list))
list2 := &(node("456",&new(node)))
fmt.println(list2)结构体还有切片映射的属性,地址为首字段地址
算法题目:如何根据搜索二叉树先序遍历还原二叉树
package main
import (
"fmt"
)
var array [10000]int
type bt struct {
val int
left *bt
right *bt
}
var index int = 0
func preOrder(root *bt) [10000]int {
if root != nil {
array[index] = root.val
index += 1
preOrder(root.left)
preOrder(root.right)
}
return array
}
func treeMain() {
var root bt
root.val = 10
var left bt
var right bt
left.val = 5
right.val = 15
root.left = &left
root.right = &right
var arr [10000]int = preOrder(&root)
var ans bt = reTree(&arr)
fmt.Println(ans.left.val)
}
func reTree(preArr *[10000]int) bt {
if len(preArr) <= 0 {
var nilBoot bt
return nilBoot
}
var root bt
process1(preArr, 0, 1, 2, &root)
return root
}
func process1(arrays *[10000]int, index int, L int, R int, ansTree *bt) {
if L > len(array)-1 || R > len(array)-1 {
return
}
ansTree.val = arrays[index]
if arrays[L] < arrays[index] {
var left bt
ansTree.left = &left
process1(arrays, L, L+1, R, &left)
}
if arrays[R] > arrays[index] {
var right bt
ansTree.right = &right
process1(arrays, R, L, R+1, &right)
}
}
边栏推荐
- Protobuf compound data types, speaking, reading and writing
- Introduction to database - MySQL simple introduction
- Discourse Custom Header Links
- 模拟问题(中)
- 2.5 Quick Sort
- 模拟问题(上)
- Discourse 自定义头部链接(Custom Header Links)
- Android Studio implements login registration - source code (connecting to MySql database)
- 动态规划问题(完结篇)
- Unity3D Application simulation enters the front and background and pauses
猜你喜欢
![handler+message [message mechanism]](/img/4b/981d5eb2f1afc98a4ceab38c505325.png)
handler+message [message mechanism]

斐波那契数列的递归优化《备忘录递归》

nSoftware.PowerShell.Server.2020

Machine Learning: Knowing the Dimensionality Reduction Process Through Low Variance Filtering

Small programs use npm packages to customize global styles

How to use labelme
![[Redis Master Cultivation Road] Jedis - the basic use of Jedis](/img/e3/0c6efd03432a01f857796f0bf648ef.png)
[Redis Master Cultivation Road] Jedis - the basic use of Jedis

Plan for many situations in the processing chain

swagger usage tutorial - quick use of swagger

protobuf 中复合数据类型的读写
随机推荐
Some understanding of YOLOv7
Perspective transformation matrix of image perspective correction should be matrix (single)/findHomography with getPerspectiveTransformd difference
DAY17: weak password detection and test
webService interface
String problem (below)
protobuf 中复合数据类型的读写
[Awards every week] The "Edge Containers" track of the Cloud Native Programming Challenge invites you to fight!
Requirements design document and the changing role of the product manager
mysql隔离级别
山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
C# One Week Introductory "C# - Classes and Objects" Day Six
双指针问题(下)
软件测试员必看!数据库知识mysql查询语句大全
The Complete Go Books - Beginner to Advanced and Web Development
深圳见!云原生加速应用构建专场:来看云原生 FinOps、SRE、高性能计算场景最佳实践
Usage of EFR32 as sniffer for Zigbee/Thread
The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Part of the WP (9)
js operation to add or subtract from the current date (day, week, month, year)
Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
LeetCode Algorithm 2326. Spiral Matrix IV