Leetcode825. 适龄的朋友

Leetcode825. 适龄的朋友

1. 问题描述

Leetcode825. 适龄的朋友_第1张图片
Leetcode825. 适龄的朋友_第2张图片

2. 思路

  1. 找朋友不找比自己大的
  2. 找朋友不找比自己小的多的

3. 代码

func numFriendRequests(ages []int) int {
    var res int
    sort.Ints(ages)
    for i := 0; i < len(ages); i++ {
        if ages[i] < 15 {
            continue
        }
        index := getMinAgePerson(ages[i], ages)
        j := i
        for j + 1 < len(ages) && ages[j+1] == ages[i] {
            j++
        }
        res += j - index
    }
    return res
}

func getMinAgePerson(age int, ages []int) int {
    i := 0
    // fmt.Printf("ages[i] == %v, age / 2 + 7 == %v\n", ages[i], age / 2 + 7)
    for ages[i] < age && ages[i] <= age / 2 + 7 {
        i++
    }
    return i
}

你可能感兴趣的:(leetcode刷题,leetcode,数据结构,算法)