leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

sign-of-productV2.dart (717B)


      1 //Find the sign of the product of a list.
      2 //To do this I checked if values were negative. If they are
      3 //then multiple the "total" variable that tracks the sign of the list.
      4 //If the list contains a 0 then it automatically returns 0.
      5 //The time complexity of this code is O(n) where n is the length of the list.
      6 //Time: 237ms Beats: 100%
      7 //Memory: 141.6MB Beats: 100%
      8 
      9 class Solution {
     10   int arraySign(List<int> nums) {
     11       int total = 1;
     12       for(int i = 0 ; i < nums.length ; ++i){
     13     
     14           if(nums[i] < 0){
     15               total *= -1;
     16           }
     17           else if(nums[i] == 0){
     18               return 0;
     19           }
     20       }
     21       if(total == total.abs()){
     22           return 1;
     23       }
     24       return -1;
     25   }
     26 }