当前位置:网站首页>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 :
边栏推荐
- [playing with Tencent cloud] a solution to the impassability of cross-border access to foreign websites using Tencent cloud CVM
- Coding's first closed door meeting on financial technology exchange was successfully held
- What is cloud development? Why cloud development? Talk about our story
- [idea] dynamic planning (DP)
- Video structured intelligent analysis platform easycvr video recording plan function optimization / regularly delete expired videos
- Zblog determines whether a plug-in installs the enabled built-in function code
- A solution for building live video based on open source real-time audio and video webrtc architecture
- Yuanqi forest started from 0 sugar and fell at 0 sugar
- Serial of H3CNE experiment column - spanning tree STP configuration experiment
- A survey of training on graphs: taxonomy, methods, and Applications
猜你喜欢

MySQL learning -- table structure of SQL test questions

A survey on dynamic neural networks for natural language processing, University of California
![[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)
![[go] concurrent programming channel](/img/6a/d62678467bbc6dfb6a50ae42bacc96.jpg)
[go] concurrent programming channel

A survey on model compression for natural language processing (NLP model compression overview)

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

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

Applet wxss
随机推荐
Video structured intelligent analysis platform easycvr video recording plan function optimization / regularly delete expired videos
Saying "Dharma" Today: the little "secret" of paramter and localparam
A very good educational man and resource center planning scheme, with word file download
IBM:以现代化架构支撑AI与多云时代的企业数字化重塑
TRTC web end imitation Tencent conference microphone mute detection
06. Tencent cloud IOT device side learning - Introduction to basic functions
[web] what happens after entering the URL from the address bar?
If only 2 people are recruited, can the enterprise do a good job in content risk control?
让UPS“印象派用户”重新认识可靠性
Object store signature generation
Future banks need to think about today's structure with tomorrow's thinking
Yuanqi forest started from 0 sugar and fell at 0 sugar
What is zero trust? Three classes will show you how to understand him!
Release! Tencent IOA and Tencent sky screen were selected into the first batch of certified products of domestic digital trusted services
proxy pattern
构建跨公链平台解决DApp开发问题
Ramda's little-known side
zblog系统如何根据用户ID获取用户相关信息的教程
Zblog system realizes the tutorial of the number of articles published on the same day when the foreground calls
Daily algorithm & interview questions, 28 days of special training in large factories - the 15th day (string)