Clean solution, non-recursion, bit manipulation


  • 1
    C
    class Solution {
    public:
        vector<vector<int>> findSubsequences(vector<int>& nums) {
            int len = nums.size();
            int tot = 1 << len;
            set<vector<int>> s;
            for(int i = 1 ; i <= tot ; i++){
                vector<int> v;
                for(int j = 0 ; j < len ; j++){
                    if((i >> j) & 0x1 && (v.size() == 0 || v.back() <= nums[j]))v.push_back(nums[j]);
                }
                if(v.size() > 1)s.insert(v);
            }
            return vector<vector<int>>(s.begin(),s.end());
        }
    };
    

Log in to reply
 

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