Bit computing a subset of moderate difficulty

How will a single word in a string stored in a word array?

#include <iostream>
#include <cstdio>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	string str[100];
	string str1 = "i love coding";
	stringstream str2(str1);
	int i = 0;
	string temp;
	while (str2 >> temp)
	{
		str[i++] = temp;
	}
	for (int j = 0;j < i;j++)
		cout << str1[j] << endl;
	return 0;
}

 

 

Use bit computing method exemplified subset of the set? Suppose the right n elements

class Solution {
 public : 
    Vector <Vector < int >> Subsets (Vector < int > & the nums) {
         int length = nums.size (); 
        Vector <Vector < int >> RES;
         for ( int I = 0 ; I <( . 1 << length); I ++) // mathematical know a set of n elements a total power of 2 ^ n subsets, now successively sentence 
        { 
            Vector < int > TEMP;
             for ( int J = 0 ; J <length; J ++ ) //Detecting whether the j-th element in the example of 
            {
                 IF ((I >> j) & . 1 == . 1 ) 
                    temp.push_back (the nums [j]); 
            } 
            res.push_back (TEMP); 
        } 
        return RES; 
    } 
} ;

 

Guess you like

Origin www.cnblogs.com/yaggy/p/11332694.html