题解 | #乳牛各类产奶统计#

乳牛各类产奶统计

https://www.nowcoder.com/practice/4e4c1e24208e44a8a9b8c7dd5f829017?tpId=354&tqId=10588090&ru=/exam/company&qru=/ta/interview-202-top/question-ranking&sourceUrl=%2Fexam%2Fcompany

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param milk_amount int整型一维数组
     * @return int整型一维数组
     */
    public int[] product_except_self (int[] milk_amount) {
        // write code here
        int n = milk_amount.length;
        int[] prefixProduct = new int[n];
        int[] suffixProduct = new int[n];
        int[] others = new int[n];

        // Calculate prefix product
        int prefix = 1;
        for (int i = 0; i < n; i++) {
            prefixProduct[i] = prefix;
            prefix *= milk_amount[i];
        }

        // Calculate suffix product
        int suffix = 1;
        for (int i = n - 1; i >= 0; i--) {
            suffixProduct[i] = suffix;
            suffix *= milk_amount[i];
        }

        // Calculate others array
        for (int i = 0; i < n; i++) {
            others[i] = prefixProduct[i] * suffixProduct[i];
        }

        return others;
    }
}

考察的知识点:

  1. 数组操作:计算前缀乘积和后缀乘积。
  2. 循环遍历:遍历数组来计算前缀乘积和后缀乘积。

解题思路:

这个问题可以使用前缀和和后缀和的方法来解决。首先,我们可以计算数组中每个位置的前缀乘积和后缀乘积,然后将它们相乘即可得到其他品种的牛产奶量的乘积。

全部评论

相关推荐

11-28 16:00
已编辑
武汉理工大学 Java
Tom哥981:这份简历是“短期项目硬堆中大型系统技术”的“技术炫技式造假模板”,槽点密集到能当反面教材: ### 1. 「项目时长」和「技术密度」严重脱节,造假痕迹焊死在简历上 两个项目时长分别是**3个月、2个月**,但堆了Spring AI、Elasticsearch、MinIO、Kafka、ShardingSphere、Docker、Sentinel等近20个中大型项目才用的技术——正常情况下,光把这些中间件的文档看完+环境搭好,3个月都不够,更别说实现“AI多轮对话、分库分表、RBAC权限、大模型调用”这些功能。 说白了:你这不是“做项目”,是把“后端技术栈清单”往项目里硬塞,明摆着“只调用了API,没碰过核心逻辑”。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务