ARTS 第19周

2019-08-11  本文已影响0人  陈卧虫

ARTS 第19周分享

[TOC]

Algorithm

75. Sort Colors

[medium]
[题目描述]

Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Example 1:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
[解题思路]
[参考代码]
func sortColors(nums []int) {
    // 使用三个指针p0, p2, current,分别指向0的最右边界,2的最左边界 和当前遍历到的元素
    // 将两头的元素排列好,中间的元素自然就只剩下了1

    p0 := 0
    cur := 0
    p2 := len(nums) - 1

    for cur <= p2 {
        if nums[cur] == 0 {
            nums[p0], nums[cur] = nums[cur], nums[p0]
            p0++
            cur++
        } else if nums[cur] == 2 {
            nums[p2], nums[cur] = nums[cur], nums[p2]
            p2--
        } else {
            cur++
        }
    }
}

147. Insertion Sort List

[medium]
[题目描述]

Sort a linked list using insertion sort.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4
[解题思路]
[参考代码]
func insertionSortList(head *ListNode) *ListNode {
    // 新建一个全新的列表,将旧列表中的每一元素插入到新列表中

    // 极端情况判断
    if head == nil {
        return nil
    } else if head.Next == nil {
        return head
    }

    // first
    // 新列表的第一个元素直接为旧列表的第一个元素
    sortedList := &ListNode{Val: head.Val}

    for head.Next != nil {

        cur := head.Next           // 待插入元素
        head.Next = head.Next.Next // 从旧列表中取出一个元素

        // 插入新列表中
        preTmp := sortedList
        tmp := sortedList
        for tmp != nil { // 遍历的最后一个元素,同时待插入元素的值小于等于遍历的元素则停止
            if cur.Val <= tmp.Val {
                break
            }
            preTmp = tmp
            tmp = tmp.Next
        }

        if tmp == sortedList {
            cur.Next = sortedList
            sortedList = cur
        } else {
            cur.Next = tmp
            preTmp.Next = cur
        }
    }

    return sortedList
}
func insertionSortList(head *ListNode) *ListNode {
    /*
        假设开头第一个元素为有序表,遍历后面的所有元素,将每一个元素插入到前面有序表的合适位置。
    */

    if head == nil {
        return nil
    } else if head.Next == nil {
        return head
    }

    // first
    // curVal := head.Val
    preCur := head     // 指向当前待插入的元素的前一个元素
    cur := preCur.Next // 指向当前待插入的元素

    preTmp := head // 指向当前遍历的元素的前一个元素
    tmp := head    // 指向当前遍历的元素

    for cur != nil {
        // 找到要插入的位置
        for tmp != cur && cur.Val > tmp.Val {
            preTmp = tmp
            tmp = tmp.Next
        }

        // 插入
        if tmp == head { // 判断插入的位置
            preCur.Next = cur.Next // 将插入元素取出
            cur.Next = head
            head = cur
            cur = preCur.Next
        } else if tmp == cur {
            preCur = preCur.Next
            cur = cur.Next
        } else {
            preCur.Next = cur.Next // 将插入元素取出
            cur.Next = tmp
            preTmp.Next = cur
            cur = preCur.Next
        }

        // tmp重置为head位置
        tmp = head
    }

    return head
}

Review

Go and JSON: https://eager.io/blog/go-and-json/

介绍了关于golang中json的具体使用方式以及注意事项:

Tips

分享一些对git分支操作的方式:

share

GitHub 技巧: https://mp.weixin.qq.com/s/1daFgoZjSARpDL2BU1q2qg

本周阅读

第一周:1, 5, 7
什么是CDN?https://mp.weixin.qq.com/s/K3zqr8Sjt00L9Hgip_7f7A
Go语言interface底层实现  https://mp.weixin.qq.com/s/v6W2Y24l9ghieAxiAdyc_A

红黑树到底是什么: https://mp.weixin.qq.com/s/MSB-vFGqNWB26kPydBJQmQ
最常见的Git错误都有哪些: https://mp.weixin.qq.com/s/apo4Io7xQU9hbw7yeS_LzQ
Golang 中的标签(Tags in Golang): https://mp.weixin.qq.com/s/zSCNodT64z7OkOkzOxk5DA

 GitHub 技巧:https://mp.weixin.qq.com/s/1daFgoZjSARpDL2BU1q2qg
漫画:什么是微服务?https://mp.weixin.qq.com/s/FRVOYlgZCO524KwzQRohLA
漫画:什么是ZooKeeper? https://mp.weixin.qq.com/s/Gs4rrF8wwRzF6EvyrF_o4A
漫画:什么是优先队列?https://mp.weixin.qq.com/s/4hXBw7sZ-NKs_asOQxS7gA
漫画:什么是分布式事务?https://mp.weixin.qq.com/s/oKOzvN49zOhl8cwliy3SEg
上一篇 下一篇

猜你喜欢

热点阅读