当前位置:网站首页>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 :
边栏推荐
- Swift array map/flatmap/compactmap/filter/reduce/chaining Usage Summary
- Yuanqi forest started from 0 sugar and fell at 0 sugar
- What is cloud development? Why cloud development? Talk about our story
- 让UPS“印象派用户”重新认识可靠性
- 重新定义存储架构,华为用了不止5颗“芯”
- An error is reported during SVN uploading -svn sqlite[s13]
- 06. Tencent cloud IOT device side learning - Introduction to basic functions
- Zblog determines whether a plug-in installs the enabled built-in function code
- Object store signature generation
- Markdown syntax -- Formula
猜你喜欢

A survey on model compression for natural language processing (NLP model compression overview)
![[go] concurrent programming channel](/img/6a/d62678467bbc6dfb6a50ae42bacc96.jpg)
[go] concurrent programming channel
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)

A survey on dynamic neural networks for natural language processing, University of California

MySQL learning -- table structure of SQL test questions

A survey of training on graphs: taxonomy, methods, and Applications

Applet wxss
随机推荐
[go] runtime package for concurrent programming and its common methods
Comparison of jmeter/k6/locust pressure measuring tools (not completed yet)
Funny! Pictures and texts give you a comprehensive understanding of the effects of dynamics and mass
How important is it to document the project? I was chosen by the top 100 up leaders and stood up again
Page scrolling effect library, a little skinny
Complete the log service CLS questionnaire in 1 minute and receive the Tencent cloud 30 yuan threshold free voucher ~
How to use the national standard streaming media server to view the video stream of the surveillance camera? How to correctly use UDP and TCP protocols?
Try catch finally implementation mechanism
Principle analysis of robot hardware in the loop system
Druid architecture and Implementation
Video intelligent analysis platform easycvr derivative video management platform menu bar small screen adaptive optimization
Modern finite element analysis can easily achieve accurate results
Let ups "Impressionist users" re understand reliability
[play with Tencent cloud] TSF User Guide
A survey on model compression for natural language processing (NLP model compression overview)
How to get the response body content in gin?
Serial of H3CNE experiment column - VLAN configuration experiment, access and trunk
During JMeter pressure measurement, time_ The number of requests does not go up due to many waits. The problem is solved
集体突破之后,中国公有云的下一步落在哪里?
Abstract factory pattern