第六届字节跳动青训营第九课 | 豆包MarsCode AI 刷题

69 阅读3分钟

对于给定的数字 **n**,从 **1****n**,对于每个 **i**,将数字 **n****i** 逆序拼接,直到 **i** 等于 **n** 为止。最终输出这个拼接后的数组。

例如:

  • **n = 3** 时,拼接后的数组是 **[3, 2, 1, 3, 2, 3]**
  • **n = 4** 时,拼接后的数组是 **[4, 3, 2, 1, 4, 3, 2, 4, 3, 4]**
  • **n = 5** 时,拼接后的数组是 **[5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5]**

:::success 模拟即可

:::

def solution(n: int) -> list:
    # write code here
    res = []
    for i in range(1, n+1):
        res += [j for j in range(n, i-1, -1)]

    # print(res)
    return res


if __name__ == '__main__':
    print(solution(3) == [3, 2, 1, 3, 2, 3])
    print(solution(4) == [4, 3, 2, 1, 4, 3, 2, 4, 3, 4])
    print(solution(5) == [5, 4, 3, 2, 1, 5, 4, 3, 2, 5, 4, 3, 5, 4, 5])

Gorn + Kitex + Hertz

go package main import ( "gorm.io/gorm" "gorm.io/driver/sqlite" ) type Product struct { gorm.Model Code string Price uint } func main() { db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) if err != nil { panic("failed to connect database") } // 迁移 schema db.AutoMigrate(&Product{}) // Create db.Create(&Product{Code: "D42", Price: 100}) // Read var product Product db.First(&product, 1) // 根据整型主键查找 db.First(&product, "code = ?", "D42") // 查找 code 字段值为 D42 的记录 // Update - 将 product 的 price 更新为 200 db.Model(&product).Update("Price", 200) // Update - 更新多个字段 db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // 仅更新非零值字段 db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"}) // Delete - 删除 product db.Delete(&product, 1) }

First 使用的踩坑

使用First时, 需要注意查询不到数据会返回 ErrRecordNotFoud

使用Find查询多条数据,查询不到数据不会返回错误。

使用结构体作为查询条件

当使用结构体作为条件查询时,GORM 只会查询非零值字段。这意味着如果您的的字段值为0、”“、false或其他零值,改字段不会被用于构建查询条件,使用Map来构建查询条件。

游戏队友搜索

在一款多人游戏中,每局比赛需要多个玩家参与。如果发现两名玩家至少一起玩过两局比赛,则可以认为这两名玩家互为队友。现在你有一份玩家(通过玩家ID标识)和比赛局次(通过比赛ID标识)的历史记录表,目标是帮助某位指定玩家找到所有符合条件的队友。

测试样例

样例1:

输入:**id = 1, num = 10, array = [[1,1], [1,2], [1,3], [2,1], [2,4], [3,2], [4,1], [4,2], [5,2], [5,3]]**
输出:**[4, 5]**

样例2:

输入:**id = 2, num = 6, array = [[2,1], [2,3], [1,1], [1,2], [3,1], [4,3]]**
输出:**[]**

样例3:

输入:**id = 3, num = 8, array = [[3,1], [3,2], [3,3], [4,1], [5,2], [6,3], [7,1], [7,2]]**
输出:**[7]**

def solution(id, num, array):
    # Edit your code here
    temp = {}
    # temp = [[] for i in range(num+1)]
    for a in array:
        t = temp.get(a[1], [])
        t.append(a[0])
        temp[a[1]] = t

    # print(temp)
    m = {}
    for t in temp.values():
        if id in t:
            for x in t:
                if x != id:
                    m[x] = m.get(x, 0) + 1
    res = []
    for x, v in m.items():
        if v >= 2:
            res.append(x)
    res.sort()
    return res


if __name__ == "__main__":
    # Add your test cases here

    print(
        solution(
            1,
            10,
            [
                [1, 1],
                [1, 2],
                [1, 3],
                [2, 1],
                [2, 4],
                [3, 2],
                [4, 1],
                [4, 2],
                [5, 2],
                [5, 3],
            ],
        )
        == [4, 5]
    )