当前位置:网站首页>2021-04-02: given a square or rectangular matrix, zigzag printing can be realized.
2021-04-02: given a square or rectangular matrix, zigzag printing can be realized.
2022-06-24 16:56:00 【Fuda scaffold constructor's daily question】
2021-04-02: Given a square or rectangular matrix matrix, Realization zigzag Print .[0,1,2,3,4,5,6,7,8] What is the order of printing 0,1,3,6,4,2,5,7,8.
Fuda answer 2021-04-02:
Two for A nested loop .
The outer loop . First traverse the first column , The traversal does not include the column number as 0 Last line . Every cycle , Modify flag bit .
Inner circulation . According to the flag bit , From bottom left to top right , Or from top right to bottom left .
The code to use golang To write . The code is as follows :
package main import "fmt" func main() { arr := [][]int{ {0, 1, 2}, {3, 4, 5}, {6, 7, 8}} printMatrixZigZag(arr) } func printMatrixZigZag(matrix [][]int) { row := len(matrix) col := len(matrix[0]) fromUp := false // Traverse the first column for i := 0; i < row; i++ { if fromUp { // Find the top right position j := 0 for ; i-j >= 0 && j < col; j++ { } j-- // Top right to bottom left for ; j >= 0; j-- { fmt.Print(matrix[i-j][0+j], " ") } } else { // From bottom left to top right for j := 0; i-j >= 0 && j < col; j++ { fmt.Print(matrix[i-j][0+j], " ") } } fromUp = !fromUp } // Traverse the last line for j := 1; j < col; j++ { if fromUp { // Find the top right position i := 0 for ; row-1-i >= 0 && j+i < col; i++ { } i-- // Top right to bottom left for ; i >= 0; i-- { fmt.Print(matrix[row-1-i][j+i], " ") } } else { // From bottom left to top right for i := 0; row-1-i >= 0 && j+i < col; i++ { fmt.Print(matrix[row-1-i][j+i], " ") } } fromUp = !fromUp } }
The results are as follows :
边栏推荐
- Sigai intelligent container damage identification products are deployed in Rizhao Port and Yingkou Port
- NFT元宇宙源码搭建解析与介绍
- What is Ethernet
- Private domain defense in the cotton Era
- Video structured intelligent analysis platform easycvr video recording plan function optimization / regularly delete expired videos
- [play with Tencent cloud] TSF User Guide
- IBM:以现代化架构支撑AI与多云时代的企业数字化重塑
- Zblog determines whether a plug-in installs the enabled built-in function code
- [go] runtime package for concurrent programming and its common methods
- Several schemes of traffic exposure in kubernetes cluster
猜你喜欢
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)
A survey of training on graphs: taxonomy, methods, and Applications
A survey on dynamic neural networks for natural language processing, University of California
[go] concurrent programming channel
MySQL learning -- table structure of SQL test questions
A survey on model compression for natural language processing (NLP model compression overview)
Applet wxss
Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
随机推荐
未来银行需要用明天的思维,来思考今天架构
How Tencent cloud es achieves cross cluster data copy & lt through reindex; Lower & gt;
Prometheus deployment
Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)
Recent progress of ffmpeg go
Kubernetes 1.20.5 setting up Sentinel
Don't let [mana] destroy your code!
Development of block hash game guessing system (mature code)
Audio knowledge (I)
Analysis and introduction of NFT meta universe source code construction
[tke] nodelocaldnschache is used in IPVS forwarding mode
API documents are simple and beautiful. It only needs three steps to open
What is thermal data detection?
Markdown syntax -- Formula
How to perform concurrent stress testing on RTSP video streams distributed by audio and video streaming servers?
主链系统发展解析
TRTC web end imitation Tencent conference microphone mute detection
AI video structured intelligent security platform easycvr intelligent security monitoring scheme for protecting community residents
What is the reason for the worse website SEO ranking?
Data acquisition and transmission instrument reservoir dam safety monitoring