Personal Algorithm Practice Library-go language edition--1

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT" "util/stack") type tree struct {Data intl *treer *tree}type list struct {data Intnext *list} Factorial func fact (n uint32) UInt32 {if n = = 0 {return 1}return n * Fact (n-1)}//binary find func bsearch (a []int, element, low, height int) int {If low > height {return-1}mid: = (low + height)/2if a[mid] = = element {return 0} else if A[mid] > Eleme  NT {return bsearch (A, element, low, mid-1)} else {return bsearch (A, element, mid+1, height)}}//Fibonacci sequence--recursive func f (n int) int {if n < 3 {return 1}return F (n-1) + f (n-2)}//Fibonacci Sequence--non-recursive func f1 (n int) int {var s, s1, s2 int = 1, 1, 1for I: = 3; I & lt;= N; i++ {s = s1 + s2s2 = S1S1 = S}return s}//bubbling func bubblesort (a []int, n int) {//n data requires n-1 trip for i: = 1; i < n; i++ {//per trip required than Number of elements-1 times for J: = 0; J < N-i; J + + {if a[j] > a[j+1] {temp: = A[j]a[j] = a[j+1]a[j+1] = temp}}}}//Reverse list func reverselist (head *list) *list {if head = = N Il {return head}ph: = Reverselist (head.next) head.next.next = Headhead.next = Nilreturn Ph}func ReverseliSt1 (head *list) *list {if head = = Nil | | head.next = NIL {return head}node1, Node2, node3: = Head, Head.next, HEAD.NEXT.N Extfor Node2! = Nil {Node2.next = Node1node1.next = Nilnode1 = Node2node2 = Node3node3 = Node3.next}return node2}//turns the two-fork search tree Change to doubly linked list Func treetodoublelist (head, tail *tree, root *tree) {var ltail, rhead *treeif root = nil {head = Niltail = Nilretur  N}treetodoublelist (Head, Ltail, ROOT.L) treetodoublelist (Rhead, tail, ROOT.R) if ltail! = Nil {LTAIL.R = ROOTROOT.L = Ltail} else {head = root}if rhead! = Nil {ROOT.R = Rheadrhead.l = root} else {tail = root}}//compare two trees equal func comparetree (T1, T2 *t REE) int {if T1 = nil && t2 = nil {return 1}if t1 = Nil | | t2 = NIL {return-1}if T1.data! = t2.data {return- 1}if Comparetree (T1.L, t2.l) = = 1 && comparetree (T1.R, T2.R) = = 1 {return 1} else {return-1}}//find and all paths for a value in the two-tuple tree F UNC Getvaluepath (root *tree, n int, sum int) {if root = = Nil {return}s.push (root.data) sum + = root.dataif Sum = = n {fmt. Println (s) s.pop () Return}getValuepath (ROOT.L, N, sum) getvaluepath (ROOT.R, N, sum) s.pop ()}//Get maximum sub-array func getmaxsubarray (a []int, n int) {if n < 0 | | A = = nil {Return}var sum, begin, END, Max int = 0, 0, 0, 1 << 31for I: = 0; I < n; i++ {sum + = A[i]if Sum < 0 {sum = 0begin = i + 1}if sum > max {max = Sumend = i}}fmt. PRINTLN (begin, End, Max)}//determines whether the sequence of integers is not a two-dollar lookup tree post-order traversal result func Checkpostorder (a []int, L, h int, b *bool) {if l >= h {return}pare NT: = a[h]//fmt. PRINTLN (Parent, L, h) I: = Lfor; i < h-1; i++ {if a[i] > parent {break}}for J: = i + 1; J < H; J + + {if A[J] <= parent {*b = falsebreak}}if *b {checkpostorder (A, L, I-1, B) Checkpostorder (A, I, h-1, b)}}//^-reverses the words in the sentence Sequential func swap (b []byte, begin, End int) {If begin > end {return}for begin <= End {tmp: = B[begin]b[begin] = B[end]b[en D] = Tmpbegin++end--}}func reversestr (str *string) {begin: = 0b: = []byte (*STR) for I: = 0; i < len (*str); i++ {if b[i] = = + {swap (b, begin, i-1) begin = i + 1}}swap (b, Begin, Len (*STR)-1)//Invert entire string swap (b, 0, Len (*STR)-1) fmt. Println (string (b))}//$-reverses the order of the words inside the sentence func main () {/*//var b uint32 = 6//fmt. Println (b, "! =", fact (b))//a: = []int{1, 2, 4, 6, 7, 8, 9, ten, one, 12}//c: = bsearch (A, 4, 0, 9)//fmt. Println (c)//t: = []int{3, 2, 1, 5, 7, 6, 9, 0}//bubblesort (T, 8)//fmt. Println (t)//t: = []int{1,-2, 3, ten, -4, 7, 2, -5}//getmaxsubarray (T1, Len (t1)) T1: = Tree{4, nil, nil}t2: = tree{7, nil, n IL}T3: = tree{5, &t1, &t2}t4: = tree{12, nil, nil}root: = tree{10, &t3, &t4}getvaluepath (&root, 22, 0) T: = []int{5, 7, 6, 9, one, 1, 8}//t: = []int{7, 4, 6, 5}b: = Truecheckpostorder (t, 0, 7, &b) FMT. Println (b) */str: = "I am a student." Reversestr (&AMP;STR)}


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.