小算法练习 发表于 2017-04-09 | 分类于 Algorithm 链表翻转1234567891011121314node *head;node *h=head;node *hn=head->next;node *hnn=hn->next;head->next =NULL;while(hn != NULL){ hn->next =h; h= hn; hn = hnn; hnn = hnn->next;} 求集合的所有子集123456789101112131415161718192021222324252627282930/** * 解题思路: 个数为n的集合,子集的个数为(2^n-1)个. * 以[1...(2^n)]的二进制数表示是否选取集合的元素:1为选取,0为不选. * 例子: * set = [a,b,c] , 子集个数为 2^3-1 = 7 * [1...7]的二进制表示分别为: * 001,010,011,100,101,110,111 * 001 --> [a] * 010 --> [b] * 011 --> [a,b] * 100 --> [c] * .... * 111 --> [a,b,c] */public static void getSubset(char set[]) { int length = set.length; int i; for (i = 1; i < (1 << length); i++) { System.out.print("["); for (int j = 0; j < length; j++) { if ((i & (1 << j)) != 0) { System.out.print(set[j]); } } System.out.print("] "); }} x&(x-1)始终为零, 求x是哪类数12345// x 是 2^nfor (int j = 0; j < 31; j++) { int r = (1<<j) & (1<<j -1); System.out.println(r);} 赏 微信打赏