C++ 0ms Permutation solution


  • 0
    class Solution {
    public:
        vector<string> readBinaryWatch(int num) {
            vector<string>res;
            vector<int>hr({1,2,4,8});
            vector<int>min({1,2,4,8,16,32});
            for(int i=0;i<=num;i++){
                vector<int>vec1=getDigit(hr,i);
                vector<int>vec2=getDigit(min,num-i);
                for(int hour:vec1){
                    if(hour>=12) continue;
                    for(int minute:vec2){
                        if(minute>=60) continue;
                        res.push_back(to_string(hour)+":"+((minute<10)?("0"+to_string(minute)):to_string(minute)));
                    }
                }
            }
            return res;
        }
        
        vector<int> getDigit(vector<int>&nums, int count){
            vector<int>res;
            getDigitHelper(nums,count,0,0,res);
            return res;
        }
        
        void getDigitHelper(vector<int>&nums,int count, int pos, int sum,vector<int>& res){
            if(count==0){
                res.push_back(sum);
                return;
            }
            
            for(int i=pos;i<nums.size();i++){
                getDigitHelper(nums,count-1,i+1,sum+nums[i],res);
            }
        }
    };
    

Log in to reply
 

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