• [LeetCode]: 238: Product of Array Except Self


    题目:

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

    Solve it without division and in O(n).

    For example, given [1,2,3,4], return [24,12,8,6].

    Follow up:
    Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.)

    思路:

    1. 计算所有数相乘的结果,并记录数组中"0"的个数

    2. 再次逐一便利初始给定的数组,根据其中"0"的个数和出现的位置做处理

    3. 当只有一个0的时候,结果为除掉这个0的剩下的数的乘积

    4. 当有多个0的时候,结果为全零的数组

    代码:

    public static int[] productExceptSelf(int[] nums) {
            int[] arrResult  = new int[nums.length];
            int iTemp =1;
            int i0Counter=0;
            
            for(int i =0;i < nums.length;i++){
                if(nums[i]!= 0){
                    iTemp= iTemp * nums[i];
                }
                else{
                    i0Counter++;
                }
                
            }
            
            if(i0Counter ==0 ){
                for(int i =0;i < nums.length;i++){
    
                    arrResult[i] = iTemp/nums[i];
                    
                }
                return arrResult;
            }
            
            else if(i0Counter ==1 ){
                for(int i =0;i < nums.length;i++){
                    if(nums[i] == 0){
                        arrResult[i] = iTemp;
                    }
                    else{
                        arrResult[i] = 0;
                    }
                }
                return arrResult;
            }
            
            else{
                for(int i =0;i < nums.length;i++){
                    nums[i] =0;
                }
                return arrResult;
            }
        }

    分析:这个解法时间复杂度超了,使用了额外的空间,并且使用了除法

    思路2:遍历累乘的时候,“除去”“当前位置”的元素

    1. 先计算当前位置左侧元素的乘积,记录在输出数组的当前位置上,这样不使用额外的存储空间

    2. 再从右边遍历,记录当前位置右侧的所有元素的乘积,记录在一个额外的位置上

    3. 将两个数组再逐一相乘

    代码如下:

        public static int[] productExceptSelf(int[] nums) {
            int[] arrResult  = new int[nums.length];
            int[] arrRight   = new int[nums.length];
            
            arrResult[0] = 1;
            for(int i = 1;i<nums.length; i++ ){
                arrResult[i] = arrResult[i-1]*nums[i-1] ;
            }
            
            arrRight[nums.length-1] = 1;
            for(int i = nums.length-2; i>= 0; i-- ){
                arrRight[i] = arrRight[i+1]*nums[i+1] ;
            }
            
            for(int i = 0;i<nums.length; i++ ){
                arrResult[i] = arrResult[i]*arrRight[i] ;
            }
            
            return arrResult;
        }
  • 相关阅读:
    案例:推进GTID解决MySQL主主不同步问题
    idea 每次新建项目都需要重新配置maven的解决方案
    eclipse 配置maven
    maven 配置本地仓库、中央仓库、私库
    eclipse 安装lombok插件(详解)
    plsql 将表结构导出到excel中的两种方式
    ThreadPoolExecutor的用法
    MySQL 5.7 的SSL加密方法
    spring @Async 异步执行
    maven setting 文件配置
  • 原文地址:https://www.cnblogs.com/savageclc26/p/4848126.html
Copyright © 2020-2023  润新知