classSolution{publicint[]productExceptSelf(int[] nums){int[] res =newint[nums.length];int[] preL =newint[nums.length];int[] preR =newint[nums.length];
preL[0]=1;
preR[nums.length-1]=1;for(int i =1; i < nums.length;i++){
preL[i]= preL[i-1]*nums[i-1];}for(int i = nums.length-2; i >=0;i--){
preR[i]= preR[i+1]*nums[i+1];}for(int i =0; i < nums.length;i++){
res[i]= preL[i]* preR[i];}return res;}}
3- ACM 实现
publicclass productExceptSelf {publicstaticint[]product(int[] nums){int[] res =newint[nums.length];int[] preL =newint[nums.length];int[] preR =newint[nums.length];// 求左前缀
preL[0]=1;for(int i =1;i<nums.length;i++){
preL[i]= preL[i-1]*nums[i-1];}
preR[nums.length-1]=1;for(int i = nums.length-2;i>=0;i--){
preR[i]= preR[i+1]*nums[i+1];}for(int i =0;i < nums.length;i++){
res[i]= preL[i]*preR[i];}return res;}publicstaticvoidmain(String[] args){System.out.println("输入数组长度");Scanner sc =newScanner(System.in);int n = sc.nextInt();int[] nums =newint[n];for(int i =0; i < n;i++){
nums[i]= sc.nextInt();}int[] res =product(nums);for(int i : res){System.out.print(i+" ");}}}