Product Except Self (Java)


  • 0
    V

    Product of Array Except Self
    Time Complexity = O(n)

    class Solution {
    
    	public int[] productExceptSelf1(int[] nums) {
    
    		int[] t1 = new int[nums.length];
    		int[] t2 = new int[nums.length];
    
    		t1[0] = 1;
    		t2[nums.length - 1] = 1;
    
    	for (int i = 0; i < nums.length - 1; i++) {
    			t1[i + 1] = t1[i] * nums[i];
    	}
    
    	for (int i = nums.length - 1; i > 0; i--) {
    			t2[i - 1] = t2[i] * nums[i];
    		}
    
    	for (int i = 0; i < t2.length; i++) {
    			nums[i] = t1[i] * t2[i];
    		}
    		return nums;
    	}
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.