zoukankan      html  css  js  c++  java
  • 《程序员代码面试指南》第八章 数组和矩阵问题 不包含本位置值的累乘数组

    题目

    不包含本位置值的累乘数组
    

    java代码

    package com.lizhouwei.chapter8;
    
    /**
     * @Description: 不包含本位置值的累乘数组
     * @Author: lizhouwei
     * @CreateDate: 2018/5/9 21:11
     * @Modify by:
     * @ModifyDate:
     */
    public class Chapter8_22 {
    
        public int[] product(int[] arr) {
            int[] res = new int[arr.length];
            int product = 1;
            for (int i = 0; i < arr.length; i++) {
                res[i] = product * arr[i];
                product = product * arr[i];
            }
            product = 1;
            for (int i = arr.length - 1; i > 0; i--) {
                res[i] = res[i - 1] * product;
                product = product * arr[i];
            }
            res[0] = product;
            return res;
        }
    
        //测试
        public static void main(String[] args) {
            Chapter8_22 chapter = new Chapter8_22();
            int[] arr = {2, 3, 1, 4};
            int[] res = chapter.product(arr);
            for (int i = 0; i < res.length; i++) {
                System.out.print(res[i] + " ");
            }
        }
    }
    
    

    结果

  • 相关阅读:
    iOS App之间跳转
    iOS 编码转换
    iOS文件类型判断
    iOS 运行时
    libqrencode生成二维码
    设置app的启动图
    根据字体计算CGRect
    iOS 英文学习
    libev 中IO事件循环解析
    libev 默认事件循环初始化的解析
  • 原文地址:https://www.cnblogs.com/lizhouwei/p/9016747.html
Copyright © 2011-2022 走看看