Clean Queue C++ Solution


  • 0
    D

    class Solution {
    public:
    bool checkSubarraySum(vector<int>& nums, int k) {

        int i,size=nums.size(),flag=0,j;
        
        queue<int> q;
        
        q.push(nums[0]);
        
        for(i=1;i<size;i++)
        {
            int qsize=q.size();
            
            for(j=qsize;j>0;j--)
            {
                int num=q.front();
                
                if((num+nums[i])==0 && j>=1 && k==0)
                {
                    flag=1;
                    break;
                }
        
                if(j>=1 && k!=0 && (num+nums[i])%k==0)
                {
                    flag=1;
                    break;
                }
            
                q.pop();
                q.push(num+nums[i]);
            }
            
            q.push(nums[i]);
            
            if(flag==1)
            break;
        }
        
        
        if(flag==1)
        return true;
        else
        return false;
        
    }
    

    };


Log in to reply
 

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