Week 12 of Arts | leetcode 54 rotating printing matrix | go benchmark



Arts is an activity launched by Chen Hao in the geek time column. The aim is to keep learning through sharing.

Each person writes arts once a week: algorithm is an algorithm problem, review is to read an English article, technique / tips is to share a small technology, share is to share a point of view.

This week’s content


This week’s algorithm is leetcode 54. Spiral matrix rotation printing matrix (binary array).

There is no complicated algorithm for this problem. The most difficult part is to find out the end condition of printing and to judge whether the printing process should “turn around”.

func spiralOrder(matrix [][]int) []int {
    if len(matrix) == 0 || len(matrix[0]) == 0 {
        return nil

    var ans []int
    lr, lc := len(matrix), len(matrix[0])
    for start := 0; lc > 2*start && lr > 2*start; start++ {
        ans = append(ans, printInCircle(matrix, lr, lc, start)...)
    return ans

// nums[Y][X]
func printInCircle(nums [][]int, lr, lc, start int) []int {
    var ret []int
    endX, endY := lc-1-start, lr-1-start

    for i := start; i <= endX; i++ {
        ret = append(ret, nums[start][i])

    if start < endY {
        for i := start + 1; i <= endY; i++ {
            ret = append(ret, nums[i][endX])

    if start < endY && start < endX {
        for i := endX - 1; i >= start; i-- {
            ret = append(ret, nums[endY][i])

    if start < endX && start < endY-1 {
        for i := endY - 1; i > start; i-- {
            ret = append(ret, nums[i][start])

    return ret

Review article recommendation

I haven’t read any good articles this week. Come on next week!

Tip programming skills

This week’s programming tip is about benchmarking tools in go test. Using this tool, you can do some simple unit tests and benchmark tests, which is very suitable for functional self-test.

If you don’t know enough about go test itself or go benchmark benchmark tools, you can take a look at these two articles: the usage of go test from the official website introduces test flags and how to write benchmarks in go by Dave, a former go official developer

Here are the common flags of benchmark

withgo test -run YourBenchmarkTestFunctionName -bench . -count 1 -timeout 1h -benchtime 1h -failfastThis order, for example.

-countThe total number of times the benchmark is executed. The “once” in the “times” here is not the detection time represented by B.N, but refers to the completion of the overall detection process.

-timeoutRepresents the total time limit for the benchmark.

-benchtimeIt represents the time of a benchmark detection process, which is the upper limit of the time consumed by B. n probe completion.

-failfastIf the flag is added, it means that the benchmark test will end this test as long as it fails once. If the count is greater than 1, it will only end the current test process, which is the next time.

Share flash

Recently, I have become more and more aware that the saying “keep up with the wind, decline again, and exhaust after three times” is actually saying that “it’s really hard to persist”. It’s human nature not to persist until the end.

So, if conditions permit, try to do it overnight.

Reading list of the week

  • Geek time go language core 36
    Three tests
  • Go official website
    Test Flags
  • Dave Cheney blog
    How to write benchmarks in Go
  • Design and implementation of go language
    Network poller

Recommended Today

asp.net Application of regular expression

1. Balanced group / recursive matching (?’ Group ‘), which is called the corresponding content of group, and counts it on the stack;(?’- Group ‘), and count the corresponding content named group out of the stack(?!) Zero width negative look ahead assertion. Since there is no suffix expression, attempts to match always failRegular example:,{0,1}”5″:\[[^\[\]]*(((?’Open’\[)[^\[\]]*)+((?’-Open’\])[^\[\]]*)+)*(?(Open)(?!))\],{0,1} Test […]