Multidimensional array delivery in the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. In the go language, when a multidimensional array is passed directly as a function argument, it can be very restrictive, for example, except that the other dimensions of the first-dimension array need to be explicitly given; You can use multidimensional slices to pass as parameters:

Type S1 []int

Type S2 []s1

Sword means offer a written question 3:

In a two-dimensional array, each row is sorted from left to right in ascending order, and each column is sorted from top to bottom. Complete a function, enter a two-dimensional array and the number to find, and determine whether the number exists in a two-dimensional array.
Code:


Package Mainimport ("Errors" "FMT" "OS") type S1 []inttype s2 []s1var searcherr error = errors.  New ("Search Error") Func main () {A: = S2{s1{1, 2, 3, 4, 5, 6},s1{7, 8, 9, ten, one, 12},s1{13, +,,, 18},s1{20, 21, 25}}result, Err: = Search (a[:][:], 4, 6, +) if err! = Nil {fmt. PRINTLN (ERR) OS. Exit (1)}if result = = true {fmt. Println ("Key is searched")}}func search (a s2, row, col, Key int) (flag bool, err error) {if row < 0 | | Col < 0 {Retu RN false, Searcherr}flag = Falserownum: = 0colNum: = Col-1for rowNum < row && Colnum >= 0 {if A[rownum][co Lnum] = = key {flag = Truebreak} else if a[rownum][colnum] > key {colnum--} else {Rownum++}}return}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.