007 Golang-channel-practice 打印水分子

对应leetcode 1117

https://leetcode.cn/problems/building-h2o/description/

题目大意:在三个为一组的字符串中,打印两个H,一个O,顺序不限。

这在go里面很容易实现。只需要在每个函数前加上一个go关键词,就可以轻松实现并发了。直接看代码!

Go 复制代码
package main

import (
	"fmt"
	"sync"
)

func main() {
	for {
		var wg sync.WaitGroup
		wg.Add(3)
		go H1(&wg)
		go H2(&wg)
		go O1(&wg)
		wg.Wait()
		fmt.Println()
	}
}

func H1(wg *sync.WaitGroup) {
	defer wg.Done()
	fmt.Print("H")
}

func H2(wg *sync.WaitGroup) {
	defer wg.Done()
	fmt.Print("H")
}

func O1(wg *sync.WaitGroup) {
	defer wg.Done()
	fmt.Print("O")
}

打印效果:

相关推荐
小红帽2.01 天前
从零构建一款开源在线客服系统:我的Go语言实战之旅
开发语言·golang·开源
slim~1 天前
Java基础第9天总结(可变参数、Collections、斗地主)
java·开发语言
ComputerInBook1 天前
C++编程语言:标准库:第37章——正则表达式(Bjarne Stroustrup)
开发语言·c++·正则表达式
A尘埃1 天前
智能工单路由系统(Java)
java·开发语言·智能工单
Source.Liu1 天前
【Python基础】 13 Rust 与 Python 注释对比笔记
开发语言·笔记·python·rust
qq_195551691 天前
代码随想录70期day3
开发语言·python
XXYBMOOO1 天前
Qt UDP 通信类详解与实现
开发语言·网络·c++·qt·网络协议·ui·udp
pusue_the_sun1 天前
C语言强化训练(12)
c语言·开发语言·算法
counting money1 天前
JAVA泛型基础
java·开发语言·eclipse
007php0071 天前
Go语言面试:传值与传引用的区别及选择指南
java·开发语言·后端·算法·面试·golang·xcode