golang刷题 10s a day

func moveZeroes(nums []int)  {
    var slow = 0

    for fast := 0; fast < len(nums); fast++ {
        if nums[fast] != 0 {
           nums[slow] = nums[fast]
           slow++
        }

    }

    for  ;slow < len(nums) ; slow++ {
                nums[slow] = 0
    }

}

75. 颜色分类

func swap(nums []int, left int, right int) {
    var tmp = nums[left]
    nums[left] = nums[right]
    nums[right] = tmp
}
func sortColors(nums []int)  {
    var left int = 0
    var right int = len(nums) - 1
    var index int = 0

    for ;index <= right; { 
    // while (index <= right) {
        var cur = nums[index]

        if cur == 0 {
            swap(nums, left, index)
            index++
            left++
        } else if cur == 1 {
            index++
        } else {
            swap(nums, right, index)
            right--
        }
    }
}

88. 合并两个有序数组

func merge(nums1 []int, m int, nums2 []int, n int)  {
    var i int = m - 1
    var j int = n - 1
    var cur = len(nums1) - 1

    for ;j >= 0; {
        if i >= 0 && nums1[i] > nums2[j] {
            nums1[cur] = nums1[i]
            cur--
            i--
        } else {
            nums1[cur] = nums2[j]
            cur--
            j--
        }
    }
}

876. 链表的中间结点

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func middleNode(head *ListNode) *ListNode {
    slow := head
    fast := head

    for ;fast != nil && fast.Next != nil; {
        slow = slow.Next;
        fast = fast.Next.Next;
    }

    return slow
}

21. 合并两个有序链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
    var dummy ListNode
    pre := &dummy

    for ; list1 != nil && list2 != nil; {
        if (list1.Val <= list2.Val) {
            pre.Next = list1
            list1 = list1.Next
        } else {
            pre.Next = list2
            list2 = list2.Next
        }
        pre = pre.Next
    }
    if list1 != nil {
        pre.Next = list1
    }
    if list2 != nil {
        pre.Next = list2
    }
    return dummy.Next
}

206. 反转链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reverseList(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return head
    } 
    cur := reverseList(head.Next)
    head.Next.Next = head
    head.Next = nil
    return cur
}
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reverseList(head *ListNode) *ListNode {
    // if head == nil || head.Next == nil {
    //     return head
    // }
    var pre *ListNode
    for  ;head != nil; {
        next := head.Next
        head.Next = pre
        pre = head
        head = next
    }
    return pre
}

148. 排序链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func sortList(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }
    mid := findMid(head)
    right := sortList(mid.Next)
    mid.Next = nil
    left := sortList(head)
    return mergeTwoLists(left, right)
}

func findMid(head *ListNode) *ListNode {
      if head == nil || head.Next == nil {
        return head
    }
    slow := head
    fast := head.Next

    for fast != nil && fast.Next != nil {
        slow = slow.Next
        fast = fast.Next.Next
    }
    return slow
}

func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
    var dummy ListNode
    pre := &dummy

    for ; list1 != nil && list2 != nil; {
        if (list1.Val <= list2.Val) {
            pre.Next = list1
            list1 = list1.Next
        } else {
            pre.Next = list2
            list2 = list2.Next
        }
        pre = pre.Next
    }
    if list1 != nil {
        pre.Next = list1
    }
    if list2 != nil {
        pre.Next = list2
    }
    return dummy.Next
}

leetcode 143

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func reorderList(head *ListNode)  {
    if head == nil  {
        return 
    }
    mid := findMid(head)
    l1 := head
    l2 := mid.Next
    mid.Next = nil
    l2 = reverse(l2)
    merge(l1, l2)
}

func findMid(head *ListNode) *ListNode {

    slow := head
    fast := head

    for fast.Next != nil && fast.Next.Next != nil {
        slow = slow.Next
        fast = fast.Next.Next
    }
    return slow
}

func reverse(head *ListNode) *ListNode {
    var pre *ListNode
    for head != nil {
        next := head.Next
        head.Next = pre
        pre = head
        head = next
    }
    return pre
}

func merge(l1 *ListNode, l2 *ListNode) {
   var l1Tmp, l2Tmp *ListNode
    for l1 != nil && l2 != nil {
        l1Tmp = l1.Next
        l2Tmp = l2.Next

        l1.Next = l2
        l1 = l1Tmp

        l2.Next = l1
        l2 = l2Tmp
    }

}

61. 旋转链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func rotateRight(head *ListNode, k int) *ListNode {
    if head == nil {
        return head
    }
    len := 0
    tmp := head

    for tmp != nil {
        tmp = tmp.Next
        len++

    }
    k = k % len

    former := head
    latter := head
    for i := 0; i < k; i++ {
        former = former.Next
    }
    for former.Next != nil {
        former = former.Next
        latter = latter.Next
    }
    former.Next = head
    newHead := latter.Next
    latter.Next = nil
    return newHead
}

24. 两两交换链表中的节点

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func swapPairs(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }
    subHead := swapPairs(head.Next.Next)
    headNext := head.Next
    headNext.Next = head
    head.Next = subHead

    return headNext
}

23. 合并K个升序链表

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeKLists(lists []*ListNode) *ListNode {
    length := len(lists)
    if length < 1 {
        return nil
    }
    if length == 1 {
        return lists[0]
    }
    num := length / 2
    left := mergeKLists(lists[:num])
    right := mergeKLists(lists[num:])
    return mergeTwoLists(left, right)
}

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode{
    if l1 == nil {
        return l2
    }
    if l2 == nil {
        return l1
    }
    if l1.Val < l2.Val {
        l1.Next = mergeTwoLists(l1.Next, l2)
        return l1
    }
    l2.Next = mergeTwoLists(l1, l2.Next)
    return l2
}

 

posted @ 2023-03-10 15:56  刷刷题啊呀呀  阅读(18)  评论(0编辑  收藏  举报