#50 Product of Array Exclude Itself

解决一个整数数组中每个元素的乘积问题,不使用除法操作。通过预先计算左右累积乘积来高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

Given an integers array A.

Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate BWITHOUT divide operation.

Example

For A = [1, 2, 3], return [6, 3, 2].

题目思路:

这题用一个left数组去记录i往左所有数的乘积;right数组去记录i往右所有数的乘积。然后重新遍历一遍数组,每个i的答案就是left[i - 1] * right[i + 1]。

Mycode(AC = 12ms):

class Solution {
public:
    /**
     * @param A: Given an integers array A
     * @return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
     */
    vector<long long> productExcludeItself(vector<int> &nums) {
        // write your code here
        vector<long long> ans;
        if (nums.size() == 0) return ans;
        else if (nums.size() == 1) {
            ans.push_back(1);
            return ans;
        }
        
        ans.resize(nums.size(), 1);
        vector<long long> left(nums.size(), nums[0]), right(nums.size(), nums[nums.size() - 1]);
        
        // left[i] denote the product of all numbers
        // from 0...i
        for (int i = 1; i < nums.size(); i++) {
            left[i] = left[i - 1] * nums[i];
        }
        
        // right[i] denote the product of all numbers
        // from i...nums.size() - 1
        for (int i = nums.size() - 2; i >= 0; i--) {
            right[i] = right[i + 1] * nums[i];
        }
        
        for (int i = 0; i < nums.size(); i++) {
            if (i == 0) {
                ans[i] = right[i + 1];
            }
            else if (i == nums.size() - 1) {
                ans[i] = left[i - 1];
            }
            else {
                ans[i] = right[i + 1] * left[i - 1];
            }
        }
        
        return ans;
    }
};


# # collect interval interval = 15 [[instances]] # # append some labels for series # labels = { region="cloud", product="n9e" } # # interval = global.interval * interval_times # interval_times = 1 ## Docker Endpoint ## To use TCP, set endpoint = "tcp://[ip]:[port]" ## To use environment variables (ie, docker-machine), set endpoint = "ENV" # endpoint = "unix:///var/run/docker.sock" endpoint = "unix:///var/run/docker.sock" labeled_containers = true ## Set to true to collect Swarm metrics(desired_replicas, running_replicas) gather_services = false gather_extend_memstats = false container_id_label_enable = true container_id_label_short_style = true ## Containers to include and exclude. Globs accepted. ## Note that an empty array for both will include all containers container_name_include = [] container_name_exclude = [] ## Container states to include and exclude. Globs accepted. ## When empty only containers in the "running" state will be captured. ## example: container_state_include = ["created", "restarting", "running", "removing", "paused", "exited", "dead"] ## example: container_state_exclude = ["created", "restarting", "running", "removing", "paused", "exited", "dead"] # container_state_include = [] # container_state_exclude = [] ## Timeout for docker list, info, and stats commands timeout = "5s" ## Specifies for which classes a per-device metric should be issued ## Possible values are &#39;cpu&#39; (cpu0, cpu1, ...), &#39;blkio&#39; (8:0, 8:1, ...) and &#39;network&#39; (eth0, eth1, ...) ## Please note that this setting has no effect if &#39;perdevice&#39; is set to &#39;true&#39; perdevice_include = [] ## Specifies for which classes a total metric should be issued. Total is an aggregated of the &#39;perdevice&#39; values. ## Possible values are &#39;cpu&#39;, &#39;blkio&#39; and &#39;network&#39; ## Total &#39;cpu&#39; is reported directly by Docker daemon, and &#39;network&#39; and &#39;blkio&#39; totals are aggregated by this plugin. ## Please note that this setting has no effect if &#39;total&#39; is set to &#39;false&#39; total_include = ["cpu", "blkio", "network"] ## Which environment variables should we use as a tag ##tag_env = ["JAVA_HOME", "HEAP_SIZE"] ## docker labels to include and exclude as tags. Globs accepted. ## Note that an empty array for both will include all labels as tags docker_label_include = [] docker_label_exclude = ["annotation*", "io.kubernetes*", "*description*", "*maintainer*", "*hash", "*author*", "*org_*", "*date*", "*ur l*", "*docker_compose*"] ## Optional TLS Config # use_tls = false # tls_ca = "/etc/categraf/ca.pem" # tls_cert = "/etc/categraf/cert.pem" # tls_key = "/etc/categraf/key.pem" ## Use TLS but skip chain & host verification # insecure_skip_verify = false
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值