0

Leetcode 刷题笔记

 2 years ago
source link: https://wushuo.ink/post/leetcode-shua-ti-zhuan-lan/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Leetcode 刷题笔记

· 2021-10-25 ·

使用 Golang 刷 Leetcode 的笔记,包含一部分 Golang 算法和数据结构的内容。


Leetcode

Array Index

寻找中间值

func findMedian(numSorted []int) float64 {
    numSortedLen := len(numSorted)
    slice1 := numSorted[:numSortedLen/2]
    slice2 := numSorted[numSortedLen/2:]
    if (len(slice1) == len(slice2)) {
        return float64((numSorted[numSortedLen/2] + numSorted[numSortedLen/2 - 1])) / 2
    }
    // 其实这里应该写成下面更好
    // return float64((slice1[numSortedLen / 2 - 1] + slice2[0]))
    // 在
    return float64(numSorted[numSortedLen/2])
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK