ARTS 第22周

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

ARTS 第22周分享

[TOC]

Algorithm

274. H-Index

[medium]
[题目描述]

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.

According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."

Example 1:
Input: citations = [3,0,6,1,5]
Output: 3 
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had 
             received 3, 0, 6, 1, 5 citations respectively. 
             Since the researcher has 3 papers with at least 3 citations each and the remaining 
             two with no more than 3 citations each, her h-index is 3.
[解题思路]
[参考代码]
type myInts []int

func (is myInts) Len() int {
    return len(is)
}

func (is myInts) Less(i, j int) bool {
    return is[i] > is[j]
}

func (is myInts) Swap(i, j int) {
    is[i], is[j] = is[j], is[i]
}

func HIndex(ci []int) int {
    return hIndex(ci)
}

func hIndex(citations []int) int {
    /*
        // 先对数组进行从大到小排序,
        // 根据index指数的特点,找到最大的索引+1等于本身值的这个值即可
    */

    // 先对数组进行从大到小排序,
    var c myInts
    c = citations
    sort.Sort(c)
    var max int
    if c.Len() >= 1 {
        if c[0] > 0 {
            max = 1
        }
    }
    for i := 0; i < c.Len(); i++ {
        if c[i] >= i+1 {
            max = i+1
        } else {
            return max
        }

    }
    return max
}

Review

Learning to Use Go Reflection: https://medium.com/capital-one-tech/learning-to-use-go-reflection-822a0aed74b7

golang reflect的基本使用方法,以及局限性

Tips

golang类型别名

share

做技术最怕埋头苦干:https://mp.weixin.qq.com/s/dV3Zu2tVV1bP7QwHomTBwQ

webSocket 介绍: https://mp.weixin.qq.com/s/gasQHCRj5RutsBe0zbSTFg

本周阅读

第四周:1, 5, 7
Go Modules踩坑总结: https://mp.weixin.qq.com/s/2v8kGm8T9BQFmpLfQE7-wg
数据库用数值保存时间戳: https://mp.weixin.qq.com/s/2B1VwPn9u6U-GHwWspXj6A
我是如何通过开源项目月入 10 万的?: https://mp.weixin.qq.com/s/Pif-0wJao54Bf5qqRTFVIQ

Nginx 所使用的 epoll 模型是什么  https://mp.weixin.qq.com/s/VzxtmZ3sMiW3ClW5fxrb6g

低效程序员的5个坏习惯: https://mp.weixin.qq.com/s/hM6jY-9v_skXKGF5Y38mtA
做技术最怕埋头苦干: https://mp.weixin.qq.com/s/dV3Zu2tVV1bP7QwHomTBwQ
上一篇下一篇

猜你喜欢

热点阅读