Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
Example:
1 2
Input: [1,2,3,4] Output: [24,12,8,6]
Constraint: It’s guaranteed that the product of the elements of any prefix or suffix of the array (including the whole array) fits in a 32 bit integer.
Note: Please solve it without division and in O(n).
Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
//3ms class Solution { public int[] productExceptSelf(int[] nums) { int sum =1; int hasZero =0; for(int num :nums){ if(num!=0){ sum*=num; }else{ hasZero++; } }
//1ms class Solution { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] left = new int[n]; left[0] = 1; for (int i = 1; i < n; i++) { left[i] = left[i-1] * nums[i-1]; } int product = 1; for (int i = n - 1; i >= 0; i--) { left[i] *= product; product *= nums[i]; } return left; } }
Problem-678Valid Parenthesis String
Medium
Given a string containing only three types of characters: ‘(‘, ‘)’ and ‘*’, write a function to check whether this string is valid. We define the validity of a string by these rules:
Any left parenthesis '(' must have a corresponding right parenthesis ')'.
Any right parenthesis ')' must have a corresponding left parenthesis '('.
Left parenthesis '(' must go before the corresponding right parenthesis ')'.
'*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.