zoukankan      html  css  js  c++  java
  • LeetCode 238. 除自身以外数组的乘积

    class Solution {
        public int[] productExceptSelf(int[] nums) {
            int[] res = new int[nums.length];
            //left 为该数左边的乘积
            int left = 1;
            //right 为该数右边的乘积
            int right = 1;
            for(int i = 0;i < res.length; i++){
                //此时res数组 存放的是除去当前元素 左边的元素乘积
                res[i] = left;
                left = left * nums[i]; 
            }
    
            for(int i = res.length - 1;i >= 0; i--){
                 // 此时res数组 等于 左边的 * 右边的
                res[i] = res[i] * right; 
                right = right * nums[i];
            }
            return res;
        }
    }
  • 相关阅读:
    网络编程
    模块
    内置函数
    函数应用
    万能参数
    函数
    爬虫
    算法
    Flask
    linux
  • 原文地址:https://www.cnblogs.com/peanut-zh/p/13909583.html
Copyright © 2011-2022 走看看