Programmer's Code interview guide eighth array and matrix problems do not include the multiplier group for this position value

Source: Internet
Author: User

Topic
不包含本位置值的累乘数组
Java code
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] + " ");        }    }}
Results

Programmer Code Interview guide eighth. Array and matrix problems do not include the multiplier group for the value of this location

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.