個人演算法練習庫-go語言版--1

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package mainimport ("fmt""util/stack")type tree struct {data intl    *treer    *tree}type list struct {data intnext *list}//階乘func fact(n uint32) uint32 {if n == 0 {return 1}return n * fact(n-1)}//二分尋找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] > element {return BSearch(a, element, low, mid-1)} else {return BSearch(a, element, mid+1, height)}}//斐波那契數列--遞迴func f(n int) int {if n < 3 {return 1}return f(n-1) + f(n-2)}//斐波那契數列--非遞迴func f1(n int) int {var s, s1, s2 int = 1, 1, 1for i := 3; i <= n; i++ {s = s1 + s2s2 = s1s1 = s}return s}//冒泡func bubblesort(a []int, n int) {//n個資料需要n-1趟for i := 1; i < n; i++ {//每趟需要比較 元素個數-1 次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}}}}//反轉鏈表func reverseList(head *list) *list {if head == nil {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.nextfor node2 != nil {node2.next = node1node1.next = nilnode1 = node2node2 = node3node3 = node3.next}return node2}//將二叉搜尋樹轉換成雙向鏈表func treeToDoubleList(head, tail *tree, root *tree) {var ltail, rhead *treeif root == nil {head = niltail = nilreturn}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}}//比較兩個樹是否相等func compareTree(t1, t2 *tree) 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}}//在二元樹中找出和為某一值的所有路徑func 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()}//擷取最大子數組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)}//判斷整數序列是不是二元尋找樹的後序遍曆結果func checkPostOrder(a []int, l, h int, b *bool) {if l >= h {return}parent := 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)}}//^-反轉句子裡面單詞的順序func swap(b []byte, begin, end int) {if begin > end {return}for begin <= end {tmp := b[begin]b[begin] = b[end]b[end] = tmpbegin++end--}}func reverseStr(str *string) {begin := 0b := []byte(*str)for i := 0; i < len(*str); i++ {if b[i] == 32 {swap(b, begin, i-1)begin = i + 1}}swap(b, begin, len(*str)-1)//反轉整個字串swap(b, 0, len(*str)-1)fmt.Println(string(b))}//$-反轉句子裡面單詞的順序func main() {/*//var b uint32 = 6//fmt.Println(b, "!=", fact(b))//a := []int{1, 2, 4, 6, 7, 8, 9, 10, 11, 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, 10, -4, 7, 2, -5}//getMaxSubArray(t1, len(t1))t1 := tree{4, nil, nil}t2 := tree{7, nil, nil}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, 11, 20, 1, 8}//t := []int{7, 4, 6, 5}b := truecheckPostOrder(t, 0, 7, &b)fmt.Println(b)*/str := "i am a student."reverseStr(&str)}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.