题目描述:
    image.png
    解析:因为考虑有0的存在,所以不能除法 解题思路:先从左到右乘一遍,再从右到左乘一遍

    1. class Solution {
    2. public int[] productExceptSelf(int[] nums) {
    3. /**
    4. * 因为考虑有0的存在,所以不能除法
    5. * 解题思路:先从左到右乘一遍,再从右到左乘一遍
    6. */
    7. int[] res=new int[nums.length];
    8. res[0]=1;
    9. for (int i = 1; i < nums.length; i++) {
    10. res[i]=res[i-1]*nums[i-1];
    11. }
    12. int r=1;
    13. for (int j = nums.length-1; j > -1; j--) {
    14. res[j]*=r;
    15. r*=nums[j];
    16. }
    17. return res;
    18. }
    19. }